Java8stream操作排序(upingBy)、统计
(Col。。。
简要:
今天我们⽤Java8 Lambda表达式实现⼀个经典top K 题:
给⼀⾮空的单词列表,返回前 k 个出现次数最多的单词。 返回的答案应该按单词出现频率由⾼到低排序。如果不同的单词有相同出现
频率,按字母顺序排序。
我们直接进⼊主题:
⾸先我们的有⼀个实现这个逻辑的⽅法如下:
返回结果变量list数据集合
public static List<String> getList(String[] listValue, long k) {
return Arrays.stream(listValue)
//分组并且统计次数
.upingBy(Function.identity(), unting()))
.entrySet()
.stream()
//按单词⾸字母排序
.sorted((v1, v2) -> {
if (v1.getValue().Value())) {
Key()Key());
} else {
Value()Value());
}
})
.map(Map.Entry::getKey)
.limit(k)
.List());
}
/**
* @param args 参数
* @description 给⼀⾮空的单词列表,返回前 k 个出现次数最多的单词。返回的答案应该按单词出现频率由⾼到低排序。如果不同的单词有相同出现频率,按字母    * <p>
* ⽰例:
* 输⼊: ["i", "love", "leetcode", "i", "love", "coding","code", "test", "auto", "shit", "auto"], k = 3
* 输出: ["auto","i", "love"]
* 解析: "auto"、"i" 和 "love" 为出现次数最多的三个单词,均为2次。注意,按字母顺序 "a" 在 "i"、"love" 之前, "i" 在 "love" 之前。
*/
public static void main(String[] args) {
String[] listValue = {"i", "love", "leetcode", "i", "love", "coding", "code", "test", "auto", "shit", "auto"};
long k = 3;
List<String> list = getList(listValue, k);
System.out.println(list);
}
结果输出:java stream
实现输出次数最多的单词+出现次数
public static void main(String[] args) {
String[] listValue = {"i", "love", "leetcode", "i", "love", "coding", "code", "test", "auto", "shit", "auto"};
Map<String, Long> result = getResult(listValue);
System.out.println(result);
}
// 另外⼀种写法按照单词⾸字母排序
public static Map<String, Long> getResult(String[] listValue) {
Map<String, Long> resultMap = new LinkedHashMap<>();
Map<String, Long> collectMap = Arrays.stream(listValue)
.upingBy(Function.identity(), unting()));
.stream()
// 按照单词⾸字母排序
.sorted((v1, v2) -> {
if (v1.getValue().Value())) {
Key()Key());
} else {
Value()Value());
}
})
.forEachOrdered(e -> resultMap.Key(), e.getValue()));
return resultMap;
}
结果输出:
实现不是按单词排序(随机)次数最多的单词+出现次数
public static void main(String[] args) {
String[] listValue = {"i", "love", "leetcode", "i", "love", "coding", "code", "test", "auto", "shit", "auto"};
System.out.println(getResult2(listValue));
}
// 没按⾸字母排序
public static Map<String, Long> getResult2(String[] listValue) {
Map<String, Long> resultMap = new LinkedHashMap<>();
Arrays.stream(listValue)
.upingBy(Function.identity(), unting()))
.entrySet()
.stream()
.sorted(Map.Entry.<String, Long>comparingByValue().reversed())
.forEachOrdered(e -> resultMap.Key(), e.getValue()));
return resultMap;
}
结果输出:
完整的代码⽰例:
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
public class test {
/**
* @param args 参数
* @description 给⼀⾮空的单词列表,返回前 k 个出现次数最多的单词。返回的答案应该按单词出现频率由⾼到低排序。如果不同的单词有相同出现频率,按字母    * <p>
* ⽰例:
* 输⼊: ["i", "love", "leetcode", "i", "love", "coding","code", "test", "auto", "shit", "auto"], k = 3
* 输出: ["auto","i", "love"]
* 解析: "auto"、"i" 和 "love" 为出现次数最多的三个单词,均为2次。注意,按字母顺序 "a" 在 "i"、"love" 之前, "i" 在 "love" 之前。
*/
public static void main(String[] args) {
String[] listValue = {"i", "love", "leetcode", "i", "love", "coding", "code", "test", "auto", "shit", "auto"};
long k = 3;
List<String> list = getList(listValue, k);
System.out.println(list);
Map<String, Long> result = getResult(listValue);
System.out.println(result);
System.out.println(getResult2(listValue));
System.out.println(getResult2(listValue));
}
public static List<String> getList(String[] listValue, long k) {
return Arrays.stream(listValue)
//分组并且统计次数
.upingBy(Function.identity(), unting()))
.
entrySet()
.stream()
//按单词⾸字母排序
.sorted((v1, v2) -> {
if (v1.getValue().Value())) {
Key()Key());
} else {
Value()Value());
}
})
.map(Map.Entry::getKey)
.
limit(k)
.List());
}
// 另外⼀种写法按照单词⾸字母排序
public static Map<String, Long> getResult(String[] listValue) {
Map<String, Long> resultMap = new LinkedHashMap<>();
Map<String, Long> collectMap = Arrays.stream(listValue)
.upingBy(Function.identity(), unting()));
.stream()
// 按照单词⾸字母排序
.
sorted((v1, v2) -> {
if (v1.getValue().Value())) {
Key()Key());
} else {
Value()Value());
}
})
//                .sorted(Map.Entry.<String, Long>comparingByValue().reversed())
.forEachOrdered(e -> resultMap.Key(), e.getValue()));
return resultMap;
}
/
/ 没按⾸字母排序
public static Map<String, Long> getResult2(String[] listValue) {
Map<String, Long> resultMap = new LinkedHashMap<>();
Arrays.stream(listValue)
.upingBy(Function.identity(), unting()))
.entrySet()
.stream()
.sorted(Map.Entry.<String, Long>comparingByValue().reversed())
.forEachOrdered(e -> resultMap.Key(), e.getValue()));
return resultMap;
}
}
最后有没有Java8 Lambda表达式处理这种功能实现逻辑实不实很简便,⼀⾏代码搞定。