List parsing using java streams

I have a list of restaurant objects with attributes like city, type, rating, and I want to print the list with the highest rating for this combination (city, type). Since I created this example to use a java thread, answer how this can be done with threads:

import java.util.List;
import java.util.ArrayList;
import java.util.stream.Collectors;
import java.util.Map;
import java.util.Optional;
import static java.util.stream.Collectors.maxBy;
import static java.util.stream.Collectors.groupingBy;
import static java.util.Comparator.comparingLong;

class Restaurant {
  public String city; // Any city
  public String type; // Tier1, Tier2, etc. 
  public Long rating; // for simplicity sake, assume its a number from 1 to 5
  public Restaurant(String city, String type, Long rating) { 
    this.city = city;
    this.type = type;
    this.rating = rating;
  }
  public String getCity() { return city; }
  public String getType() { return type; }
  public Long getRating() { return rating; }
}

public class HelloWorld
{
  public static void main(String[] args)
  {
    List<Restaurant> mylist = new ArrayList<Restaurant>();
    mylist.add(new Restaurant("City1", "Tier1", 1L));
    mylist.add(new Restaurant("City1", "Tier1", 2L));
    mylist.add(new Restaurant("City1", "Tier2", 1L));
    mylist.add(new Restaurant("City2", "Tier1", 1L));
    mylist.add(new Restaurant("City2", "Tier1", 3L));
    mylist.add(new Restaurant("City2", "Tier3", 1L));
    // expected outcome: (City1, Tier1, 2), (City1, Tier2, 1), (City2, Tier1, 3), (City2, Tier3, 1)

    Map<String, Map<String, Optional<Restaurant>>> mymap =
   mylist.stream().collect(groupingBy(Restaurant::getCity, groupingBy(Restaurant::getType, maxBy(comparingLong(Restaurant::getRating)))));
   /* Upto here everything is fine, but I want to have everything stored back into a list. Tried flatMap(Map::values).collect(Collectors.toList()); but it fails*/
  /*My output list should have 4 objects of Restaurant*/
  /*(City1, Tier1, 2), (City1, Tier2, 1), (City2, Tier1, 3), (City2, Tier3, 1)*/
  }
}
+4
source share
4 answers

First, you can create a composite key to get rid of nested maps. Arrays.asList(r.getCity(), r.getType())- This is what you need. Then groupingByit is not suitable for your case, since you must handle these unpleasant options. Instead, use a collector toMapwith a custom merge function:

Map<List<String>, Restaurant> mymap = mylist.stream().collect(
        toMap(r -> Arrays.asList(r.getCity(), r.getType()), r -> r,
                BinaryOperator.maxBy(comparingLong(Restaurant::getRating))));

mymap.values() , . List, :

List<Restaurant> list = new ArrayList<>(mymap.values());
+3

, :

    List<Optional<Restaurant>> resultList = mymap.values()
                                                 .stream()
                                                 .flatMap(map -> map.values().stream())
                                                 .collect(Collectors.toList());

mymap.values().stream() a Stream<Map<String, Optional<Restaurant>>>. flatMap() Stream<Optional<Restaurant>>, collect(Collectors.toList()) List<Optional<Restaurant>>.

+2

I have not worked with Java 8 yet, but I think that

mylist.stream().sorted(new Comparator<Restaurant>() {
     public int compare(Restaurant r1, Restaurant r2) {
        // impl here how your restaurant should be compared
        // (by rating or smth. else)
     }
}).collect(groupingBy( .../ al the rest stuff here/ ...));
-1
source

using the Java 8 stream API:

List<Restaurant> sortedList = mylist.stream()
.sorted((restaurant1, restaurant2) -> Long.compare(restaurant1.getRating(), restaurant2.getRating()))
.collect(Collectors.toList());
-2
source

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


All Articles