Core Java - Interview Questions and Answers for 'Collector' | Search Java Interview Question - javasearch.buggybread.com
Javasearch.buggybread.com
Share

Search Interview Questions


 2802 questions in repository.
 There are more than 200 unanswered questions.
Click here and help us by providing the answer.
 Have a video suggestion.
Click Correct / Improve and please let us know.
Label / Company      Label / Company / Text

   



Core Java - Interview Questions and Answers for 'Collector' - 6 question(s) found - Order By Newest

 Q1. Write a Program that gets a set of numbers , filters out the non prime numbers , calculate the factorial of each prime number and then finds the average of all factorials using Lambda expressions2017-05-14 14:02:57

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;
   }
}

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     java 8  lambda expressions  lambda  filter  Collectors  factorial  prime number


 Q2. Write a class / program that takes few numbers from the input and then output the average ?Core Java2017-01-27 14:26:00

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     Raytheon


 Q3. What are we doing here

list.stream().collect(Collectors.groupingBy(ClassA::getElement1,
               Collectors.averagingDouble(ClassA::getelement2)));
Core Java2018-04-10 14:58:24

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


 Q4. Write code to get count of every 5 letter word in a string using lambda expression

Core Java2018-04-18 13:06:13

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


 Q5. Can you explain filter and collectors in java 8 ?Core Java2018-05-03 08:27:26

 This question is still unanswered. Can you please provide an answer.


 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     collector  filter


 Q6. Can you explain filter and collectors in java 8 ?Core Java2018-05-03 08:42:37

 This question is still unanswered. Can you please provide an answer.


 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     collectors  filters  java 8




Subscribe to Java News and Posts. Get latest updates and posts on Java from Buggybread.com
Enter your email address:
Delivered by FeedBurner



comments powered by Disqus
 

Help us and Others Improve. Please let us know the questions asked in any of your previous interview.

Any input from you will be highly appreciated and It will unlock the application for 10 more requests.

Company Name:
Questions Asked:
         

X Close this

X Close this

Help Us Improve.
Please share your
interview experience.

Company Name:   


Questions Asked: