Java 8 threads do something with an element that has a logical field

I want to create streams from a list and do something with an object if a specific field is true.

Let me show you what I have done so far:

List<UserData> userDataList = new ArrayList<>(); phoneNumbers.forEach(phoneNumber -> { if (phoneNumber.getPrimary()) { UserData userData = new UserData(); userData.setDescription(phoneNumber.getType()); userData.setValue(phoneNumber.getNumber); userDataList.add(userData); } }); 

So, if phoneNumber.getPrimary() == true will perform the following task, if not, ignore it.

+5
source share
2 answers

Yes, you can combine filter with map to convert the List matching elements to Stream from UserData , and then assemble them into List :

 List<UserData> userDataList = phoneNumbers.stream() .filter(p -> p.getPrimary()) // or filter(ClassName::getPrimary) .map(p -> { UserData userData = new UserData(); userData.setDescription(p.getType()); userData.setValue(p.getNumber()); return userData; }) .collect(Collectors.toList()); 

And if UserData has a constructor that takes a type and a number, you can make the code shorter:

 List<UserData> userDataList = phoneNumbers.stream() .filter(p -> p.getPrimary()) // or filter(ClassName::getPrimary) .map(p -> new UserData(p.getType(),p.getNumber())) .collect(Collectors.toList()); 
+9
source

The accepted answer is already accepted. but I'm going to add an alternative solution with one string code;

  List<UserData> userDataList = phoneNumberList.stream().filter(PhoneNumber::isPrimary) .map(UserData::new).collect(Collectors.toList()); 

Let's say your phone number class has several variables;

  public class PhoneNumber { private String type; private String number; private boolean isPrimary; public PhoneNumber(String type, String number,boolean isPrimary) { this.isPrimary = isPrimary; this.type = type; this.number = number; } // getter setters } 

Add your UserData I added a constructor with the Phonenumber class.which it will be easy to add your data. since I'm going to use (: :) Method Reference.

  public class UserData { private String description; private String value; public UserData() { } //Constructor with PhoneNumber class public UserData(PhoneNumber phoneNumber) { this.description = phoneNumber.getType(); this.value = phoneNumber.getNumber(); } // getter setter } 

So let there be a list of phonenumers itearaion with one line.

  List<PhoneNumber> phoneNumberList = new ArrayList<>(); phoneNumberList.add(new PhoneNumber("Landline","40054638",false)); phoneNumberList.add(new PhoneNumber("Work","90054638",true)); phoneNumberList.add(new PhoneNumber("Office","7891432654",false)); phoneNumberList.add(new PhoneNumber("Personal","40054638",true)); //PhoneNumber::isPrimary this will check only primary //UserData::new will set values Phonenumber to UserData List<UserData> userDataList = phoneNumberList.stream().filter(PhoneNumber::isPrimary) .map(UserData::new).collect(Collectors.toList()); System.out.println(userDataList); System.out.println(userDataList.size()); 
+4
source

Source: https://habr.com/ru/post/1269520/


All Articles