Java8 CODING Questions Only
Java8 CODING Questions Only
import java.util.*;
import java.util.stream.*;
Output:
10, 8, 98, 32
import java.util.*;
import java.util.stream.*;
Output:
10, 15
Output:
98, 15
4. Given the list of integers, find the first element of the list
using Stream functions?
import java.util.*;
import java.util.stream.*;
Output:
10
import java.util.*;
import java.util.stream.*;
Output:
9
import java.util.*;
import java.util.stream.*;
Output:
98
import java.util.*;
import java.util.stream.*;
import java.util.function.Function;
Output:
j
import java.util.*;
import java.util.stream.*;
import java.util.function.Function;
.collect(Collectors.groupingBy(Function.identity(), LinkedHashMap::new,
Collectors.counting())) //Store the chars in map with count
.entrySet()
.stream()
.filter(entry -> entry.getValue() > 1L)
.map(entry -> entry.getKey())
.findFirst()
.get();
System.out.println(result);
}
}
Output:
a
myList.stream()
.sorted()
.forEach(System.out::println);
}
}
Output:
8
10
15
15
25
32
49
98
98
import java.util.*;
import java.util.stream.*;
import java.util.function.Function;
myList.stream()
.sorted(Collections.reverseOrder())
.forEach(System.out::println);
}
}
Output:
98
98
49
32
25
15
15
10
8
11. Given an integer array nums, return true if any value appears at
least twice in the array, and return false if every element is
distinct.
12. How will you get the current date and time using Java 8
Date and Time API?
class Java8 {
public static void main(String[] args) {
System.out.println("Current Local Date: " +
java.time.LocalDate.now());
//Used LocalDate API to get the date
System.out.println("Current Local Time: " +
java.time.LocalTime.now());
//Used LocalTime API to get the time
System.out.println("Current Local Date and Time: " +
java.time.LocalDateTime.now());
//Used LocalDateTime API to get both date and time
}
}
13. Write a Java 8 program to concatenate two Streams?
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;
}
}
import java.util.*;
Output:
64
125
216
343
15. Write a Java 8 program to sort an array and then
convert the sorted array into Stream?
import java.util.Arrays;
output:
AA, BB, CC, DD
.comparingLong(Notes::getTagId)
.reversed()) // sorting is
based on TagId 55,44,33,22,11
.collect(Collectors.toMap
(Notes::getTagName,
Notes::getTagId,
(oldValue, newValue) ->
oldValue,LinkedHashMap::new));
// consider old value 44 for dupilcate key
// it keeps order
System.out.println("Notes : " + notesRecords);
}
}
Output:
{CC=1, BB=1, AA=2}
19. How to find only duplicate elements with its count from
the String ArrayList in Java8?
Output:
{AA=2}
Optional.ofNullable(noteLst)
.orElseGet(Collections::emptyList) // creates empty immutable
list: [] in case noteLst is null
.stream().filter(Objects::nonNull) //loop throgh each object
and consider non null objects
.map(note -> Notes::getTagName) // method reference, consider
only tag name
.forEach(System.out::println); // it will print tag names
Input: 12,19,20,88,00,9
output: 88
22. Write a program to print the count of each character in a String?