2017-08-11 5 views
1

私はList<Map.Entry<Double, Boolean>>フィーチャーを持っています。Javaストリームを使用して、キーとそのキーの出現数を含むマップをリストから取得する

可能な値の出現回数をリスト内でBooleanと数えたいと思います。

私が作った現在の試行は

Map<Boolean, List<Map.Entry<Double, Boolean>>> classes = 
    feature.stream().collect(Collectors.groupingBy(Map.Entry::getValue)); 

の代わりに、私は整数でoccurancesの数があるMap<Boolean, Integer>たいMap<Boolean, List<Map.Entity<Double, Boolean>です。

私は

Map<Boolean, List<Map.Entry<Double, Boolean>>> classes = 
    feature.stream().collect(Collectors.groupingBy(Map.Entry::getValue, List::size)); 

を試してみましたが、しかし、これはありません、適切な方法で機能をスローします。

私はストリームAPIを初めて使用していますので、これを達成するための助けがあれば幸いです!

あなたはそれをブール値のリストを取得するには マップ機能を使用して groupingByでき
+1

「ブール値の可能な値の出現」とは何ですか?真実は何回、偽は何回ですか? –

+0

@AnthonyRaymondええと、それはブール値をStringや何かに置き換えることができ、マップに「 – Rabbitman14

答えて

1

他の回答が完璧に動作Map<Boolean,Integer>が必要な場合は、次のように指定する必要があります。

Map<Boolean,Integer> result = feature.stream() 
      .map(Map.Entry::getValue) 
      .collect(Collectors.groupingBy(
       Function.identity(), 
       Collectors.collectingAndThen(Collectors.counting(), Long::intValue))); 
+1

整数は長い間必要ではありません:)しかし、私はそれを尋ねたのであなたが質問に答える唯一の人ですので、私はあなたを受け入れています – Rabbitman14

1

Map<Boolean, Long> collect = feature.stream() 
       .map(Map.Entry::getValue) 
       .collect(Collectors.groupingBy(Function.identity(), Collectors.counting())); 
1

これはあなたMap<Boolean, Long>の結果が得られます:

List<Map.Entry<Double, Boolean>> feature = new ArrayList<>(); 
Map<Boolean, Long> result = feature 
     .stream() 
     .map(Map.Entry::getValue) 
     .collect(Collectors.groupingBy(Function.identity(), 
       Collectors.counting())); 
+0

」や「Collectors.partitioningBy()」と表示されている場合は、「Hello」と「World」のすべての出現を得ることができます。 – shmosel

関連する問題