Search Interview Questions | More than 3000 questions in repository. There are more than 900 unanswered questions. Click here and help us by providing the answer. Have a video suggestion. Click Correct / Improve and please let us know. |
|
| ||||
Core Java - Interview Questions and Answers for 'Collectors' - 5 question(s) found - Order By Newest | ||||
| ||||
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)))); } } | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  coding  code  collector  streams  Collectors.average Asked in 1 Companies | ||||
| ||||
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 | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java 8  collections  java 8 streams  collectors | ||||
| ||||
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); | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  Collectors  lambda  filter  coding  java 8 | ||||
| ||||
Ans. Filters are used to filter object in stream on certail criteria. And collector is used to collect outcome of the stream. Collector is a terminal operation. List numbers = List.of(1,2,3,4,5,6,7); List evenNumbers = numbers.stream().filter(n -> n%2==0).collect(Collectors.toList()); The above code will filter out odd numbers and return the list of even number. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  collector  filter | ||||
| ||||
Ans. Filters are used to filter object in stream on certain criteria. Collector is used to collect outcome of the stream. Collector is a terminal operation. List numbers = List.of(1,2,3,4,5,6,7); List evenNumbers = numbers.stream().filter(n -> n%2==0).collect(Collectors.toList()); The above code will filter out odd numbers and return the list of even number. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  collectors  filters  java 8 | ||||