Java 8 Problems and Solutions
Java 8 Problems and Solutions
Arrays.stream(s.split("")).distinct().forEach(System.out::print);
2) Given a sentence find the word that has the highest length. The solution is:
3) Given a sentence find the word that has the 2nd (Nth) highest length.
String a =
Arrays.stream(s.split("")).sorted(Comparator.comparing(String::length).reversed()).
skip(0).findFirst().get();
System.out.println(a);
This question asked in SNP. They will tweak the same question with list of Employee
Objects. But this is the base logic.
Q5). Find the 2nd highest length word in the given sentence
Solution:
String input = "the quick brown fox jumps right over the little lazy dog little";
Map<String, Long> collect = Arrays.stream(input.split(" "))
.collect(Collectors.groupingBy(Function.identity(),
Collectors.counting()));
System.out.println(collect);
Q8) There is a list of Employees and Employee object has a field called e-mail.
Find the list of domains ( gmail.com, yahoo.com..) and the no of occurrences for
each domain.
Q9) Given a string, find the words with the maximum number of vowels.
"The quick brown fox jumps right over the little lazy dog."
Maximum Number of Vowels: 2
output Words: quick, over, little ( because each word has maximum of 2
vowels)
Solution is:
String s = "The quick brown fox jumps right over the little lazy dog.";
Arrays.stream(s.split(" "))
.filter(e -> e.replaceAll("[^aeiouAEIOU]", "").length() == 2)
.forEach(System.out::println);
Q10) Reverse a string with speical characters and Speical characters position
shouldn't be changed".
Solution is:
IntStream.range(0, input.length()-1)
.filter(i->arr[i].matches(regex))
.forEach(i->reversed.insert(i, arr[i]));
System.out.println(reversed);
}
Q11) Given a list of integers, divide into two lists one having even numbers and
other having odd numbers.
Solutions:
System.out.println(lists);
2. intList.stream()
.collect(Collectors.partitioningBy(integerValue->integerValue
%2==0))
.entrySet().stream().map(mapValue-
>mapValue.getValue()).collect(Collectors.toList());
Q12) Given an array of integers (2, 34, 54, 23, 33, 20, 59, 11, 19, 37 ) group the
numbers by the range they belong to. The put put should be {0=[2], 50=[54,59],
20=[23,20], 10=[11,19], 30=[34,33,37]}
Solution is:
Map<Integer, List<Integer>> map =
Stream.of(2, 34, 54, 23, 33, 20, 59, 11, 19,37).collect(Collectors.groupingBy (i -
> i/10 * 10 ));
System.out.println(map);
Q13) Given a List of Strings ["as", "123", "32", "2as"], create another Integer
list that contains only integers. The output shoul be: List<Integer> iList =
[123,32]
Solution:
listOfStrings.stream().filter( ss -> ss.matches("[0-
9]*")).map(Integer::valueOf).collect(Collectors.toList());
Q14) Given an array of integers arr = {5,6,7,8,5,5,8,8,7) find the sum of the
unique elements. The output should be in this case is: 26.
Solution : Arrays.stream(arr).distinct().sum();
Q15 ) Given a numeric array , re arrange the elements to form a smallest possible
value.
input is: int arr[] = {1, 34, 3, 98, 9, 76, 45, 4};
Q16) Given a numeric array , re arrange the elements to form a highest possible
value.
input is: int arr[] = {1, 34, 3, 98, 9, 76, 45, 4};
2) Arrays.stream(arr12).mapToObj(i->
i+"").sorted(Collections.reverseOrder()).forEach(System.out::print);
Q17) Given a String = The quick brown fox jumps over the lazy dog, find the first
non repeated character. (Google interview)