英文:
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<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"};
//top three words
System.out.println("Top three words are: ");
Stream.of(words).sorted((e1,e2)->scrapleVal(e2)-scrapleVal(e1)).limit(3).forEach
(e->System.out.println(e+" : "+scrapleVal(e)));
//average word value
System.out.println("\nAverage value fo words is: ");
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) > averageScrapleValue)).forEach
(e->System.out.println(e+" : "+scrapleVal(e)));
//below average words
Stream.of(words).collect(Collectors
.partitioningBy((scrabble::scrapleVal) < averageScrapleValue)).forEach
(e->System.out.println(e+" : "+scrapleVal(e)));
}
}
答案1
得分: 1
在partitioningBy
之后,您会得到一个映射数据。
Map<Boolean, List<String>>
map = Stream.of(words).collect(Collectors
.partitioningBy(w -> scrapleVal(w) < averageScrapleValue));
在这里,map.get(true)
会提供低于平均值的单词,而map.get(false)
会提供平均值及以上的单词。
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<Boolean, List<String>>
map = Stream.of(words).collect(Collectors
.partitioningBy(w -> scrapleVal(w) < averageScrapleValue));
Here map.get(true)
give bellow average and map.get(false)
give equal and above average words.
List<String> bellowAvg = map.get(true);
List<String> aboveAndEqualAvg = map.get(false);
Output:
[list, string, hours, error]
[Java, program, unix, syntax]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论