Java 8 Lambda Create List of Strings from List of Objects

Java 8 lambda create list of Strings from list of objects

You need to collect your Stream into a List:

List<String> adresses = users.stream()
.map(User::getAdress)
.collect(Collectors.toList());

For more information on the different Collectors visit the documentation.

User::getAdress is just another form of writing (User user) -> user.getAdress() which could as well be written as user -> user.getAdress() (because the type User will be inferred by the compiler)

Converting from List of Object to List of String using streams

Because Query.getResultList() returns a raw type List it will break stream pipeline which is heavily based on generic type information. Raw type are effectively removing all information about generic types when used so stream collector returns Object.

You can work around it by manually introducing the generic type with a cast to List<?>:

List<String> collect = ((List<?>) query.getResultList()).stream()
.map(Object::toString)
.collect(Collectors.toList());

java 8, lambda , Objects copying: Creating a new list of Normalized objects

Stream solution:

public class Normalize {

public static void main(String[] args) {
List<Employee> employees = new ArrayList<>();
employees.add(new Employee(1, "Emp1", "Address 1"));
employees.add(new Employee(1, "Emp1", "Address 2"));
employees.add(new Employee(2, "Emp2", "Address 3"));
List<EmployeeNormalized> employeeNormalizedList = employees.stream()
.map(new Function<Employee, EmployeeNormalized>() {

private final Map<Integer, EmployeeNormalized> employeeIdMap = new HashMap<>();

@Override
public EmployeeNormalized apply(Employee employee) {
EmployeeNormalized normalized = this.employeeIdMap.computeIfAbsent(employee.getEmpId(),
key -> new EmployeeNormalized(employee.getEmpId(), employee.getName(), new ArrayList<>()));
normalized.getAddresses().add(employee.getAddress());
return normalized;
}
})
.distinct()
.collect(Collectors.toList());
employeeNormalizedList.forEach(System.out::println);
}
}

Quite complex, need to call distinct to get rid of duplicating instances.

I would go for simple loop:

public class Normalize {

public static void main(String[] args) {
List<Employee> employees = new ArrayList<>();
employees.add(new Employee(1, "Emp1", "Address 1"));
employees.add(new Employee(1, "Emp1", "Address 2"));
employees.add(new Employee(2, "Emp2", "Address 3"));

Map<Integer, EmployeeNormalized> employeeIdMap = new HashMap<>();
for (Employee employee : employees) {
EmployeeNormalized normalized = employeeIdMap.computeIfAbsent(employee.getEmpId(), key -> new EmployeeNormalized(employee.getEmpId(), employee.getName(), new ArrayList<>()));
normalized.getAddresses().add(employee.getAddress());
}
List<EmployeeNormalized> employeeNormalizedList = new ArrayList<>(employeeIdMap.values());
employeeNormalizedList.forEach(System.out::println);
}
}

Basically both solutions use employee id as unique identifer, and map instances to id. If id is met for the first time, create instance and add address, if there is already instances for this id, get the instance and add address.

Edit:
Since computeIfAbsent is undesireable due to many properties, you can add no argument constructor and transfer property values with setters. The best option would be to use mapping library, then you could do it with computeIfAbsent as well.

public class Normalize {

public static void main(String[] args) {
List<Employee> employees = new ArrayList<>();
employees.add(new Employee(1, "Emp1", "Address 1"));
employees.add(new Employee(1, "Emp1", "Address 2"));
employees.add(new Employee(2, "Emp2", "Address 3"));

Map<Integer, EmployeeNormalized> employeeIdMap2 = new HashMap<>();
for (Employee employee : employees) {
int id = employee.getEmpId();
EmployeeNormalized normalized = employeeIdMap2.get(id);
if (normalized == null) {
normalized = new EmployeeNormalized();
normalized.setEmpId(id);
normalized.setName(employee.getName());
normalized.setAddresses(new ArrayList<>());
//set other properties
//or even better use mapping library to create normalized and transfer property values
employeeIdMap2.put(id, normalized);
}
normalized.getAddresses().add(employee.getAddress());
}
List<EmployeeNormalized> employeeNormalizedList2 = new ArrayList<>(employeeIdMap2.values());
employeeNormalizedList2.forEach(System.out::println);
}
}

Convert List<Document> to Map<String, List<Document>> grouped-by a variable in the Document object with Java 8 streams (lamda) in one line

Try this:

Map<String, List<Document>> map = 
repo.getAllDocuments()
.stream()
.collect(Collectors.groupingBy(doc -> doc.getFor().getOpp().getId()));


Related Topics



Leave a reply



Submit