2017-12-20 20 views
1

誰もが外の地図との内の地図の結果をマップする方法を私にガイドできますか。私は、Listpoint ID、Pricepoint、Rankを持つPricepointオブジェクトを作成しました。各Pointpointの最小ランクを取得して内部マップにマップしました。その結果、それぞれのIDと結果をマッピングします。おかげPricepointの最低ランクを見つける方法

public class Pricepoint { 

String id; 
String pricepoint; 
int rank; 

public Pricepoint(String id,String pricepoint,int rank) 
{ 
    this.id = id; 
    this.pricepoint = pricepoint; 
    this.rank= rank; 
} 


public String getId() { 
    return id; 
} 

public String getPricepoint() { 
    return pricepoint; 
} 

public int getRank() { 
    return rank; 
} 

public static void main(String[] args) 
{ 
    final Comparator<Pricepoint> comp = (p1, p2) -> Integer.compare(p1.getRank(), p2.getRank()); 
    List<Pricepoint> p = new LinkedList<>(); 
    p.add(new Pricepoint("1","PP1",1)); 
    p.add(new Pricepoint("2", "PP1", 2)); 
    p.add(new Pricepoint("3","PP2",3)); 
    p.add(new Pricepoint("4", "PP2", 4)); 
    Map<String,Map<String,Integer>> map1 = p.stream() 
         .collect(Collectors.toMap(Pricepoint::getPricepoint,Pricepoint::getRank,Math::min))// I'm struck here 
+2

質問を明確にしてください。結果はどのように見えますか? – Flown

+0

以下のような最低ランクの単価ポイントレコードとして出力が必要です。{1 = pp1 = 1,3 = pp2 = 3} – user9122587

答えて

1

は、それは私が間違っていないよ場合は、このような何かをしたいようだ:

Map<String, Map<String, Integer>> map1 = p.stream() 
      .collect(Collectors.collectingAndThen(
        Collectors.groupingBy(
          Pricepoint::getPricepoint, 
          Collectors.collectingAndThen(
            Collectors.minBy(Comparator.comparing(Pricepoint::getRank)), 
            Optional::get)), 
        (Map<String, Pricepoint> x) -> { 
         return x.entrySet() 
           .stream() 
           .collect(Collectors.groupingBy(
             entry -> entry.getValue().getId(), 
             Collectors.toMap(Entry::getKey, e -> e.getValue().getRank()))); 
        })); 

    System.out.println(map1); 

が、これはただめちゃくちゃ複雑です...私は正直にあなたが本当にこのためのユースケースを持っている願っています。

+0

コードありがとうございますが、{1 = pp1 = 1,3 = pp2 = 3} – user9122587

+0

@ user9122587編集を参照してください – Eugene

関連する問題