Search Interview Questions | ![]() ![]() Click here and help us by providing the answer. ![]() Click Correct / Improve and please let us know. |
|
| ||||
Core Java - Interview Questions and Answers for 'Collectors' - 6 question(s) found - Order By Newest | ||||
| ||||
Ans. public class BuggyBread1 { public static void main(String args[]) { // Declare and Initialize the Collection Set<Integer> intSet = new HashSet<Integer>(); // Add Elements intSet.add(1); intSet.add(2); intSet.add(3); intSet.add(4); intSet.add(5); double averageOfNonPrimeFactorials = intSet.stream().filter(p->checkIfPrime(p)).collect(Collectors.averagingInt(p->calculateFactorial(p))); System.out.println(averageOfNonPrimeFactorials ); } static private boolean checkIfPrime(int num){ for(int count=2;count < num;count++){ if(num % count == 0){ return false; } } return true; } static private int calculateFactorial(int num){ int factorial = 1; for(int count=num;count > 0;count--){ factorial = factorial * count; } return factorial; } } | ||||
![]() | ||||
![]() ![]() ![]() | ||||
| ||||
Ans. public class Class{ public static void main(String[] args){ List<Integer> collector = new ArrayList(); Scanner scanner = new Scanner(System.in); int x = scanner.nextInt(); while(x != 0){ collector.add(x); x = scanner.nextInt(); } System.out.println(collector.stream().collect(Collectors.averagingInt(p->((Integer)p)))); } } | ||||
![]() | ||||
![]() ![]() ![]() ![]() | ||||
| ||||
Ans. We are averaging elements of a collection by element1 grouped by element1 * list is the reference of collection * element1 is the member element returned by getElement1 method of ClassA * element2 is the member element returned by getElement2 method of ClassA | ||||
![]() | ||||
![]() ![]() ![]() | ||||
| ||||
Ans. String str = "I had been saying that he had been there"; Map<String,Long> countWords = Arrays.asList(str.split(" ")).stream().filter(p->p.length() = 5).collect(Collectors.groupingBy(p->p,Collectors.counting())); System.out.println(countWords); | ||||
![]() | ||||
![]() ![]() ![]() | ||||
| ||||
![]() | ||||
![]() | ||||
![]() ![]() ![]() | ||||
| ||||
![]() | ||||
![]() | ||||
![]() ![]() ![]() | ||||