[Solved] Flattening nested list in java [closed]

It seems that a raw list/array needs to be created including fields from Employee and Address (assuming appropriate getters exist in these classes): List<List<Object>> flattened = list.stream() .flatMap(emp -> emp.getAddresses().stream() .map(addr -> Arrays.asList( emp.getName(), emp.getId(), addr.getAddress(), addr.getPostalCode() )) ) .collect(Collectors.toList()); flattened.forEach(System.out::println); // should print desired result solved Flattening nested list in java [closed]

[Solved] Java 8 – collecting into a map from a list of objects with nested list

Assuming you want a Map<User.userId, List<UserData.customerId>> you can use this: Map<Long, List<Long>> result = users.stream() .collect(Collectors.toMap( User::getUserId, u -> u.getUserData().stream() .map(UserData::getCustomerId) .collect(Collectors.toList()) )); 3 solved Java 8 – collecting into a map from a list of objects with nested list

[Solved] Filtering on unrelated variable in java streams

A one possible solution is to define a custom predicate like this: Predicate<Item> myPredicate = item -> randomVar == 42; Then you can use the predicate in your stream: items .stream() .filter(myPredicate) .forEach(System.out::println); solved Filtering on unrelated variable in java streams

[Solved] Java 8 stream api code to add a element to a List based on condition keeping the list size same

You can achieve this by doing the following: alist.stream() .flatMap(i -> i == 0 ? Stream.of(i, 0) : Stream.of(i)) .limit(alist.size()) .collect(Collectors.toList()); This basically: flatmaps your integer to a stream of itself if non-zero, and a stream of itself and an additional zero if equal to zero limits the size of your list to the original … Read more

[Solved] Comparing two list of objects and forming a new list by comparing one property of two list in java 8 [closed]

You want intersection of the list while saving of first list type. Get the common field values in the Set. valuesToCheck=secondList.stream().map(SecondListObject::commonFiled).collect(Collectors.toList); ”’ Apply a stream on the first while filtering based on the matching common field value in the set built in the previous step. firstList.stream().filter(x->valuesToCheck.contains(x.getCommonFiled)).collect(toList) You got the gist. 1 solved Comparing two list … Read more

[Solved] Java stream reduce to map

Thanks @HadiJ for answer Map<String,Integer> result = Arrays.stream(str) .reduce(new HashMap<>(), (hashMap, e) -> { hashMap.merge(e, 1, Integer::sum); return hashMap; }, (m, m2) -> { m.putAll(m2); return m; } ); solved Java stream reduce to map