0

How to retrieve similar values inside of object that inside ArrayList of objects.

public class People{ private String name; private String email; private String accountnum; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getAccountnum() { return accountnum; } public void setAccountnum(String accountnum) { this.accountnum= accountnum; } public People(String name, String email, String accountnum) { this.name = name; this.email = email; this.accountnum= accountnum; } } I have a ArrayList of objects that created from this class People peop1 = new People("john", "john@gmail.com", "56hk"); People peop2 = new People("Rose", "rose@gmail.com", "5689hk"); People peop3 = new People("john", "john@gmail.com", "5676hk"); List<People> peoplelist = new ArrayList<People>(); peoplelist .add(peop1); peoplelist.add(peop2 ); peoplelist .add(peop3 ); What i want to do is to retrieve the email and its accountnums. like this john@gmail.com = {56hk,5676hk} I know i can loop through and return a map or something like that. Are there any other way to do that?

16th Feb 2020, 7:43 AM
Duvindu pamaljith
Duvindu pamaljith - avatar
3 Respostas
+ 4
Duvindu pamaljith Use lambda or enhanced loop.
16th Feb 2020, 8:26 AM
A͢J
A͢J - avatar
+ 3
If you want to search for people with similar names and return their record then this is a simple way- peoplelist.stream() .filter(f-> "john".equals(f.getName())) .forEach(s-> System.out.println(s)); This was just a suggestion and there exists lot of other ways as well.
16th Feb 2020, 9:07 AM
Avinesh
Avinesh - avatar