Win a copy of Serverless Applications with Node.js this week in the NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

How to write the function equivalent of static reference code  RSS feed

 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I am practising the examples from Kathy Sierra on Jdk 1.8 for OCJP. I could able to get the things done with methods reference type but i am getting compile time error when replaced with lambda equivalent code for groupingBy. Could any one please do let me know how to write the lambda equivalent of code which is commented below. The program is as follows:



Thanks

Swapna
 
Sheriff
Posts: 24295
55
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Swapna latha wrote:i am getting compile time error



You know, when you get an error, compiile-time or run-time, it really helps to read the error message. Normally error messages are designed to tell you something about the problem. But you haven't let us do that.

So could you help yourself and us by posting the message?
 
Swapna latha
Ranch Hand
Posts: 101
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:

Swapna latha wrote:i am getting compile time error



You know, when you get an error, compiile-time or run-time, it really helps to read the error message. Normally error messages are designed to tell you something about the problem. But you haven't let us do that.

So could you help yourself and us by posting the message?



Hi Paul, i am sorry for not pasting the error message. I have solved the problem and please find the code below:

package com.izo809.streams;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class PersonStreamTest {
   public static void main(String[] args){
       Person beth = new Person("Beth",30);
       Person eric = new Person("Eric",31);
       Person deb = new Person("Deb",31);
       Person liz = new Person("Liz",30);
       Person wendi = new Person("Wendi",34);
       Person kathy = new Person("Liz",35);
       Person bert = new Person("Liz",32);
       Person bill = new Person("Liz",34);
       Person robert = new Person("Liz",38);
       List<Person> people = new ArrayList<>();
       people.add(beth);
       people.add(eric);
       people.add(deb);
       people.add(liz);
       people.add(wendi);
       people.add(kathy);
       people.add(bert);
       people.add(bill);
       people.add(robert);

       List<Person> people34 = people.stream()
               .filter(p->p.getAge()==34)
               .collect(Collectors.toList());

       System.out.println("people aged 34 = " + people34);

       List<Person> peopleAge34 = people.stream()
               .filter(p->p.getAge()==34)
//                .collect(Collectors.toCollection(()->new ArrayList<>())); //With lambda
               .collect(Collectors.toCollection(ArrayList::new));//With constructor reference

       System.out.println("peopleAge34 = " + peopleAge34);

       //Grouping people by Age
       Map<Integer,List<Person>> peopleByAge = people.stream().
//                collect(Collectors.groupingBy(Person::getAge));
       collect(Collectors.groupingBy((person)->{return person.getAge();}));
       System.out.println("peopleByAge = " + peopleByAge);

       Map<Integer,Long> noOfPeopleWithAge = people.stream().
               collect(Collectors.groupingBy(Person::getAge,Collectors.counting()));
       System.out.println("noOfPeopleWithAge = " + noOfPeopleWithAge);


   }
}
 
Marshal
Posts: 63842
209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Swapna latha wrote:. . . I have solved the problem and please find the code below: . . .

That doesn't help anybody; it would be necessary to go through the whole of your code to work out the changes, and even then they might not make the code correct.
 
I love a good mentalist. And so does this tiny ad:
global solutions you can do at home or in your backyard
https://www.kickstarter.com/projects/paulwheaton/better-world-boo
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!