# 使用流中的`PartitioningBy`如何找到上下平均值的值？

go评论51阅读模式

How do you use PartitioningBy in streams to find a above and below average value?

# 问题

``````public class scrabble {

public static int scrapleVal(String word) {
Map<Character, Integer> letterValues = new HashMap<>();

letterValues.put('a', 1);
letterValues.put('b', 3);
letterValues.put('c', 3);
letterValues.put('d', 2);
letterValues.put('e', 1);
letterValues.put('f', 4);
letterValues.put('g', 2);
letterValues.put('h', 4);
letterValues.put('i', 1);
letterValues.put('j', 8);
letterValues.put('k', 5);
letterValues.put('l', 1);
letterValues.put('m', 3);
letterValues.put('n', 1);
letterValues.put('o', 1);
letterValues.put('p', 3);
letterValues.put('q', 10);
letterValues.put('r', 1);
letterValues.put('s', 1);
letterValues.put('t', 1);
letterValues.put('u', 1);
letterValues.put('v', 8);
letterValues.put('w', 4);
letterValues.put('x', 8);
letterValues.put('y', 4);
letterValues.put('z', 10);

return word.toLowerCase().chars().map(e -> letterValues.get((char) e)).sum();
}

public static void main(String[] args) {

String[] words = {"Java", "program", "list", "string", "unix",
"hours", "syntax", "error"};

// 前三个单词
System.out.println("前三个单词是：");
Stream.of(words).sorted((e1, e2) -> scrapleVal(e2) - scrapleVal(e1)).limit(3).forEach
(e -> System.out.println(e + " : " + scrapleVal(e)));
// 单词平均值
System.out.println("\n单词平均值是：");
double averageScrapleValue = Stream.of(words)
.mapToInt(scrabble::scrapleVal).average().orElse(0.0);
System.out.println(averageScrapleValue);
// 高于平均值的单词
System.out.println("高于平均值的单词：");
Stream.of(words).filter(word -> scrapleVal(word) > averageScrapleValue).forEach
(e -> System.out.println(e + " : " + scrapleVal(e)));
// 低于平均值的单词
System.out.println("低于平均值的单词：");
Stream.of(words).filter(word -> scrapleVal(word) < averageScrapleValue).forEach
(e -> System.out.println(e + " : " + scrapleVal(e)));
}
}
``````

I'm a beginner here trying to work out some stream syntax for one of my projects. My project is a Scrabble word value counter, where I have a method that uses HashMap to store the letters and their values, while I have a String array that stores the words that need to be processed. Using stream I was able to grab the top three and average value of the words. I am currently having issues trying to find:

• those word above average
• those words below average

How would I split the values to above and below average using the partitioningBy method? Keeping in mind that this has to be done via Streams.

Here is my code so far:

``````import java.util.Arrays;
import java.util.HashMap;
import java.util.IntSummaryStatistics;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
public class scrabble {
public static int scrapleVal(String word)
{
Map&lt;Character, Integer&gt; letterValues = new HashMap&lt;&gt;();
letterValues.put(&#39;a&#39;, 1);
letterValues.put(&#39;b&#39;, 3);
letterValues.put(&#39;c&#39;, 3);
letterValues.put(&#39;d&#39;, 2);
letterValues.put(&#39;e&#39;, 1);
letterValues.put(&#39;f&#39;, 4);
letterValues.put(&#39;g&#39;, 2);
letterValues.put(&#39;h&#39;, 4);
letterValues.put(&#39;i&#39;, 1);
letterValues.put(&#39;j&#39;, 8);
letterValues.put(&#39;k&#39;, 5);
letterValues.put(&#39;l&#39;, 1);
letterValues.put(&#39;m&#39;, 3);
letterValues.put(&#39;n&#39;, 1);
letterValues.put(&#39;o&#39;, 1);
letterValues.put(&#39;p&#39;, 3);
letterValues.put(&#39;q&#39;, 10);
letterValues.put(&#39;r&#39;, 1);
letterValues.put(&#39;s&#39;, 1);
letterValues.put(&#39;t&#39;, 1);
letterValues.put(&#39;u&#39;, 1);
letterValues.put(&#39;v&#39;, 8);
letterValues.put(&#39;w&#39;, 4);
letterValues.put(&#39;x&#39;, 8);
letterValues.put(&#39;y&#39;, 4);
letterValues.put(&#39;z&#39;, 10);
return word.toLowerCase().chars().map(e-&gt;letterValues.get((char)e)).sum();
}
public static void main(String[] args) {
String [] words = {&quot;Java&quot;, &quot;program&quot;, &quot;list&quot;, &quot;string&quot;, &quot;unix&quot;,
&quot;hours&quot;, &quot;syntax&quot;, &quot;error&quot;};
//top three words
System.out.println(&quot;Top three words are: &quot;);
Stream.of(words).sorted((e1,e2)-&gt;scrapleVal(e2)-scrapleVal(e1)).limit(3).forEach
(e-&gt;System.out.println(e+&quot; : &quot;+scrapleVal(e)));
//average word value
System.out.println(&quot;\nAverage value fo words is: &quot;);
double averageScrapleValue = Stream.of(words)
.mapToInt(scrabble::scrapleVal).average().orElse(0.0);
System.out.println(averageScrapleValue);
//above average words
Stream.of(words).collect(Collectors
.partitioningBy((scrabble::scrapleVal) &gt; averageScrapleValue)).forEach
(e-&gt;System.out.println(e+&quot; : &quot;+scrapleVal(e)));
//below average words
Stream.of(words).collect(Collectors
.partitioningBy((scrabble::scrapleVal) &lt; averageScrapleValue)).forEach
(e-&gt;System.out.println(e+&quot; : &quot;+scrapleVal(e)));
}
}
``````

# 答案1

`partitioningBy`之后，您会得到一个映射数据。

``````Map<Boolean, List<String>>
map = Stream.of(words).collect(Collectors
.partitioningBy(w -> scrapleVal(w) < averageScrapleValue));
``````

``````List<String> bellowAvg = map.get(true);
List<String> aboveAndEqualAvg = map.get(false);
``````

``````[list, string, hours, error]
[Java, program, unix, syntax]
``````

You get data in map after `partitioningBy`

``````Map&lt;Boolean, List&lt;String&gt;&gt;
map = Stream.of(words).collect(Collectors
.partitioningBy(w -&gt; scrapleVal(w) &lt; averageScrapleValue));
``````

Here `map.get(true)` give bellow average and `map.get(false)` give equal and above average words.

``````List&lt;String&gt; bellowAvg = map.get(true);
List&lt;String&gt; aboveAndEqualAvg = map.get(false);
``````

Output:

``````[list, string, hours, error]
[Java, program, unix, syntax]
``````

• 本文由 发表于 2020年8月2日 22:18:10
• 转载请务必保留本文链接：https://go.coder-hub.com/63217039.html
• java
• java-stream
• partitioning

go 44

go 39

go 45

go 50