Group objects by multiple attributes with Java 8 stream API - java

Given we have a list of Bank, each Bank have multiple offices,
public class Bank {
private String name;
private List<String> branches;
public String getName(){
return name;
}
public List<String> getBranches(){
return branches;
}
}
For example:
Bank "Mizuho": branches=["London", "New York"]
Bank "Goldman": branches = ["London", "Toronto"]
Given a list of banks, I would have a map of bank representation for each city. In the example above, I need a result of
Map["London"] == ["Mizuho", "Goldman"]
Map["New York"] == ["Mizuho"]
Map["Toronto"] == ["Goldman"]
How can I achieve that result using Java 8 API? Using pre-Java8 is easy, but verbose.
Thank you.

Map<String, Set<Bank>> result = new HashMap<>();
for (Bank bank : banks) {
for (String branch : bank.getBranches()) {
result.computeIfAbsent(branch, b -> new HashSet<Bank>()).add(bank);
}
}

banks.flatMap(bank -> bank.getBranches()
.stream()
.map(branch -> new AbstractMap.SimpleEntry<>(branch, bank)))
.collect(Collectors.groupingBy(
Entry::getKey,
Collectors.mapping(Entry::getValue, Collectors.toList())));
Result would be:
{London=[Mizuho, Goldman], NewYork=[Mizuho], Toronto=[Goldman]}

You could do it using the version of Stream.collect that accepts a supplier, an accumulator function and a combiner function, as follows:
Map<String, List<Bank>> result = banks.stream()
.collect(
HashMap::new,
(map, bank) -> bank.getBranches().forEach(branch ->
map.computeIfAbsent(branch, k -> new ArrayList<>()).add(bank)),
(map1, map2) -> map2.forEach((k, v) -> map1.merge(k, v, (l1, l2) -> {
l1.addAll(l2);
return l1;
})));

I think solution provided by #JB Nizet is one of the most simple/efficient solutions. it can also be rewritten by forEach
banks.forEach(b -> b.getBranches().forEach(ch -> result.computeIfAbsent(ch, k -> new ArrayList<>()).add(b)));
Another short solution by Stream with abacus-common
Map<String, List<Bank>> res = Stream.of(banks)
.flatMap(e -> Stream.of(e.getBranches()).map(b -> Pair.of(b, e)))
.collect(Collectors.toMap2());

Related

Bad return type in lambda expression:

List<CategoryWiseEarnings> data = tripEarnings.getOrders()
.stream()
.flatMap(getCategoryRulesEarnedList -> getCategoryRulesEarnedList.getCategoryRulesEarnedList().stream())
.collect(Collectors.groupingBy(foo -> foo.getCategoryId()))
.entrySet()
.stream()
.map(e -> e.getValue()
.stream()
.reduce((c,c2) -> new CategoryWiseEarnings(
new CategoryWise(
c.getCategoryName(),
c.getCategoryId()
),
c.getBonus()
))
)
.map(f -> f.get())
.collect(Collectors.toList());
Getting Exception as
:Bad return type in lambda expression: CategoryWiseEarnings cannot be converted to CategoryWise
public class CategoryWiseEarnings {
#JsonProperty("category")
private CategoryWise earnings;
#JsonProperty("total_amount")
private String totalAmount;
}
public class CategoryWise {
#JsonProperty("category_id")
Long categoryId;
#JsonProperty("category_name")
String categoryName;
public CategoryWise(String categoryName, Long categoryId) {
this.categoryName = categoryName;
this.categoryId = categoryId;
}
}
This is my code which I want to write using streams and lambda function it is working fine if I write like this:
for (Trips tripsOrders : tripEarnings.getOrders()) {
if (!tripsOrders.getCategoryRulesEarnedList().isEmpty()) {
for (CategoryWise c : tripsOrders.getCategoryRulesEarnedList()) {
if (hashMapCategory.containsKey(c.getCategoryId())) {
// hashmapk.put(c.getCategoryId(),new CategoryWiseEarnings(new CategoryWise(c.getCategoryName(),c.getCategoryId()),c.getBonus()+hashmapk.get(c.getCategoryId()).getTotalAmount()));
CategoryWiseEarnings categoryObject = hashMapCategory.get(c.getCategoryId());
categoryObject.setTotalAmount(Double.toString(
Double.parseDouble(c.getBonus())
+ Double.parseDouble(categoryObject.getTotalAmount())
));
hashMapCategory.put(c.getCategoryId(), categoryObject);
} else {
hashMapCategory.put(c.getCategoryId(), new CategoryWiseEarnings(new CategoryWise(c.getCategoryName(), c.getCategoryId()), c.getBonus()));
}
}
}
}
List<CategoryWiseEarnings> list = new ArrayList<CategoryWiseEarnings>(hashMapCategory.values());
Stream::reduce(BinaryOperator) expects a BiFunction<T, T, T> while in the OP's code this contract is broken: (CategoryWise c, CategoryWiseEarnings c2) -> new CategoryWiseEarnings()
Also, the data model seems to be improper: in CategoryWiseEarnings field total should be Double to avoid redundant conversions, class CategoryWise is missing bonus field.
So, the solution could be to use Collectors.groupingBy and Collectors.summingDouble together to calculate total values in a map, and then re-map the map entries into CategoryWiseEarnings:
List<CategoryWiseEarnings> result = tripEarnings.getOrders()
.stream() // Stream<Trips>
.flatMap(trips -> trips.getCategoryRulesEarnedList().stream()) // Stream<CategoryWise>
.collect(Collectors.groupingBy(
cw -> Arrays.asList(cw.getCategoryId(), cw.getCategoryName()) // key -> List<Object>
LinkedHashMap::new, // keep insertion order
Collectors.summingDouble(cw -> Double.parseDouble(cw.getBonus()))
)) // Map<List<Id, Name>, Double>
.entrySet()
.stream()
.map(e -> new CategoryWiseEarnings(
new CategoryWise(e.getKey().get(0), e.getKey().get(1)),
String.valueOf(e.getValue()) // assuming the total is String
))
.collect(Collectors.toList());

creating Map from List is not giving expected result

I have the two list objects as shown below, from which i'm creating the map object.
List<Class1> list1;
List<Class2> list2;
HashMap<String,String> map = new HashMap<>();
for(Class1 one : list1){
if(one.isStatus()){
map.put(one.getID(),one.getName());
}
}
//iterating second list
for(Class2 two : list2){
if(two.isPerformed()){
map.put(two.getID(),two.getName());
}
}
The above code works fine , want the above to be written using streams.
Below is the sample code using streams().
map = list1.stream().filter(one.isStatus()).collect(toMap(lst1 -> lst1.getID(), lst1.getName());
map = list2.stream().filter(...);
But the "map" is not giving the expected result when written using stream() API.
Stream concatenation Stream.concat may be applied here to avoid map.putAll
Map<String, String> map = Stream.concat(
list1.stream()
.filter(Class1::isStatus)
.map(obj -> Arrays.asList(obj.getID(), obj.getName())),
list2.stream()
.filter(Class2::isPerformed)
.map(obj -> Arrays.asList(obj.getID(), obj.getName()))
) // Stream<List<String>>
.collect(Collectors.toMap(
arr -> arr.get(0), // key - ID
arr -> arr.get(1),
(v1, v2) -> v1 // keep the first value in case of possible conflicts
));
The code above uses a merge function (v1, v2) -> v1 to handle possible conflicts when the same ID occurs several times in list1 and/or list2 to keep the first occurrence.
However, the following merge function allows joining all the occurrences into one string value (v1, v2) -> String.join(", ", v1, v2).
I'm not sure what expected result you're not seeing but I created a minimal working example that you should be able to adapt for your own use case.
public class Main {
public static void main(String[] args) {
List<Person> personList = new ArrayList<>();
Map<Integer, String> personMap = personList.stream()
.filter(Person::isStatus)
.collect(Collectors.toMap(person -> person.id, person -> person.name));
}
private static class Person {
public String name;
public int id;
public boolean isStatus() {
return true;
}
}
}
Try this,
List<Class1> list1;
List<Class2> list2;
Map<String, String> map1 = list1.stream().filter(Class1::isStatus).collect(Collectors.toMap(Class1::getId, Class1::getName));
Map<String, String> map2 = list2.stream().filter(Class2::isPerformed).collect(Collectors.toMap(Class2::getId, Class2::getName));
map1.putAll(map2);

How to filter based on list returned by map param using Java 8 streams

I'm trying to use Java stream to filter some values based on certain conditions. I am able to achieve the same using traditional for loops and a little bit of streams, but I want to rewrite the same logic fully in streams.
Original code:
public List <String> getProductNames(Hub hub, String requestedGroup) {
List <SupportedProduct> configuredProducts = repo.getSupportedProducts(hub);
List <String> productNames = new ArrayList <> ();
for (SupportedProduct supportedProduct: configuredProducts) {
List < String > categoryNameList = new ArrayList <> ();
String activeCategoryName = supportedProduct.getCategoryDetails().getActiveCategoryName();
if (activeCategoryName == null) {
Optional.ofNullable(supportedProduct.getCategoryDetails().getCategories())
.orElse(Collections.emptyList())
.forEach(category - > categoryNameList.add(category.getName()));
} else {
categoryNameList.add(activeCategoryName);
}
for (String catName: categoryNameList) {
Division division = divisionRepo.getDivisionByCatName(catName);
if (division != null && division.getGroup() == requestedGroup) {
productNames.add(supportedProduct.getProductName());
}
}
}
return productNames;
}
My try:
return Optional.ofNullable(configuredProducts).orElse(Collections.emptyList()).stream()
.map(supportedProduct -> {
List<String> categoryNameList = new ArrayList<>();
String activeCategoryName = supportedProduct.getCategoryDetails().getActiveCategoryName();
if (activeCategoryName == null) {
Optional.ofNullable(supportedProduct.getCategoryDetails().getCategories())
.orElse(Collections.emptyList())
.forEach(category -> categoryNameList.add(category.getName()));
} else {
categoryNameList.add(activeCategoryName);
}
return categoryNameList;
})
.filter(catName ->{
Division division = divisionRepo.getDivisionByCatName(catName);
return division != null && division.getGroup() == requestedGroup;
})........
But I'm lost beyond this.
Please help me to write the same using streams.
EDIT: Added IDEOne for testing - Link
The logic inside is quite complicated, however, try this out:
public List <String> getProductNames(Hub hub, String requestedGroup) {
List<SupportedProduct> configuredProducts = repo.getSupportedProducts(hub);
// extract pairs:
// key=SupportedProduct::getProductName
// values=List with one activeCategoryName OR names of all the categories
Map<String, List<String>> namedActiveCategoryNamesMap = configuredProducts.stream()
.collect(Collectors.toMap(
SupportedProduct::getProductName,
p -> Optional.ofNullable(p.getCategoryDetails().getActiveCategoryName())
.map(Collections::singletonList)
.orElse(Optional.ofNullable(p.getCategoryDetails().getCategories())
.stream()
.flatMap(Collection::stream)
.map(Category::getName)
.collect(Collectors.toList()))));
// look-up based on the categories' names, group equality comparison and returning a List
return namedActiveCategoryNamesMap.entrySet().stream()
.filter(entry -> entry.getValue().stream()
.map(catName -> divisionRepo.getDivisionByCatName(catName))
.filter(Objects::nonNull)
.map(Division::getGroup)
.anyMatch(requestedGroup::equals))
.map(Map.Entry::getKey)
.collect(Collectors.toList());
}
I recommend splitting into separate methods for sake of readability (the best way to go).
The verbose logics of Optional chains including two orElse calls can be surely simplified, however, it gives you the idea.
You can perform within one Stream using Collectors.collectingAndThen. In that case, I'd extract the Function finisher elsewhere, example:
public List<String> getProductNames(Hub hub, String requestedGroup) {
return repo.getSupportedProducts(hub).stream()
.collect(Collectors.collectingAndThen(
Collectors.toMap(
SupportedProduct::getProductName,
categoryNamesFunction()),
productNamesFunction(requestedGroup)));
}
private Function<Map<String, List<String>>, List<String>> productNamesFunction(String requestedGroup) {
return map -> map.entrySet().stream()
.filter(entry -> entry.getValue().stream()
.map(divisionRepo::getDivisionByCatName)
.filter(Objects::nonNull)
.map(Division::getGroup)
.anyMatch(requestedGroup::equals))
.map(Map.Entry::getKey)
.collect(Collectors.toList());
}
private Function<SupportedProduct, List<String>> categoryNamesFunction() {
return p -> Optional.ofNullable(p.getCategoryDetails().getActiveCategoryName())
.map(Collections::singletonList)
.orElse(Optional.ofNullable(p.getCategoryDetails().getCategories())
.stream()
.flatMap(Collection::stream)
.map(Category::getName)
.collect(Collectors.toList()));
}

How to Filter List<String,Object> Collection with java stream?

I have
List<String, Person> generalList
as a list.
there is Customer object Under Person, 1 more list under Customer named Id
I want to filter this nested IdList under object but it is not working.
I tried to use flatMap but this code is not working
String s = generalList.stream()
.flatMap(a -> a.getCustomer().getIdList().stream())
.filter(b -> b.getValue().equals("1"))
.findFirst()
.orElse(null);
I expect the output as String or Customer object
Edit:
My original container is a map, I am filtering Map to List
Explanation.
Map<String, List<Person> container;
List<Person> list = container.get("A");
String s = list.stream()
.flatMap(a -> a.getCustomer().getIdList().stream())
.filter(b -> b.getValue().equals("1"))
.findFirst()
.orElse(null);
Here is the Person
public class Person
{
private Customer customer;
public Customer getCustomer ()
{
return customer;
}
}
And Customer
public class Customer {
private Id[] idList;
/*getter setter*/
}
And Id
public class Id {
private String value;
/*getter setter*/
}
You're possibly looking for a map operation as:
String s = list.stream()
.flatMap(a -> a.getCustomer().getIdList().stream())
.filter(b -> b.getValue().equals("1"))
.findFirst()
.map(Id::getValue) // map to the value of filtered Id
.orElse(null);
which is equivalent of(just to clarify)
String valueToMatch = "1";
String s = list.stream()
.flatMap(a -> a.getCustomer().getIdList().stream())
.anyMatch(b -> b.getValue().equals(valueToMatch))
? valueToMatch : null;
Update 2 This solution works directly on a list of Person objects:
String key = "1";
List<Person> list = container.get("A");
String filteredValue = list.stream()
.flatMap(person -> Arrays.stream(person.getCustomer().getId())
.filter(id -> id.getValue().equals(key)))
.findFirst().get().getValue();
Old answer working with map
Since you are only interested in the values of your map you should stream on them and in the flatMap I not only got a stream on the getId() list but also filtered on them directly. So if I understood your code structure correctly this should work
String key = "1";
String filteredValue = map.values().stream()
.flatMap(list -> list.stream()
.flatMap(person -> Arrays.stream(person.getCustomer().getId())
.filter(id -> id.getValue().equals("1"))))
.findFirst().get().getValue();
Updated to adjust for edited question

Java 8 Join Map with Collectors.toMap

I'm trying to collect in a Map the results from the process a list of objects and that it returns a map. I think that I should do it with a Collectors.toMap but I haven't found the way.
This is the code:
public class Car {
List<VersionCar> versions;
public List<VersionCar> getVersions() {
return versions;
}
}
public class VersionCar {
private String wheelsKey;
private String engineKey;
public String getWheelsKey() {
return wheelsKey;
}
public String getEngineKey() {
return engineKey;
}
}
process method:
private static Map<String,Set<String>> processObjects(VersionCar version) {
Map<String,Set<String>> mapItems = new HashMap<>();
mapItems.put("engine", new HashSet<>(Arrays.asList(version.getEngineKey())));
mapItems.put("wheels", new HashSet<>(Arrays.asList(version.getWheelsKey())));
return mapItems;
}
My final code is:
Map<String,Set<String>> mapAllItems =
car.getVersions().stream()
.map(versionCar -> processObjects(versionCar))
.collect(Collectors.toMap()); // here I don't know like collect the map.
My idea is to process the list of versions and in the end get a Map with two items: wheels and engine but with a set<> with all different items for all versions. Do you have any ideas as can I do that with Collectors.toMap or another option?
The operator you want to use in this case is probably "reduce"
car.getVersions().stream()
.map(versionCar -> processObjects(versionCar))
.reduce((map1, map2) -> {
map2.forEach((key, subset) -> map1.get(key).addAll(subset));
return map1;
})
.orElse(new HashMap<>());
The lambda used in "reduce" is a BinaryOperator, that merges 2 maps and return the merged map.
The "orElse" is just here to return something in the case your initial collection (versions) is empty.
From a type point of view it gets rid of the "Optional"
You can use Collectors.toMap(keyMapper, valueMapper, mergeFunction). Last argument is used to resolve collisions between values associated with the same key.
For example:
Map<String, Set<String>> mapAllItems =
car.getVersions().stream()
.map(versionCar -> processObjects(versionCar))
.flatMap(m -> m.entrySet().stream())
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,
(firstSet, secondSet) -> {
Set<String> result = new HashSet<>();
result.addAll(firstSet);
result.addAll(secondSet);
return result;
}
));
To get the mapAllItems, we don't need and should not define processObjects method:
Map<String, Set<String>> mapAllItems = new HashMap<>();
mapAllItems.put("engine", car.getVersions().stream().map(v -> v.getEngineKey()).collect(Collectors.toSet()));
mapAllItems.put("wheels", car.getVersions().stream().map(v -> v.getWheelsKey()).collect(Collectors.toSet()));
Or by AbstractMap.SimpleEntry which is lighter than the Map created byprocessObjects`:
mapAllItems = car.getVersions().stream()
.flatMap(v -> Stream.of(new SimpleEntry<>("engine", v.getEngineKey()), new SimpleEntry<>("wheels", v.getWheelsKey())))
.collect(Collectors.groupingBy(e -> e.getKey(), Collectors.mapping(e -> e.getValue(), Collectors.toSet())));

Categories

Resources