您好,登录后才能下订单哦!
这篇文章主要介绍了Lambda如何实现字符串出现次数排序算法,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。
四种方式的执行时间比例大概是 100:50:15:12
/**
* Lambda+stream 排序
*/
public List<String> useLambdaStream(){
List<String> strList = Arrays.asList("a,b,cc,b,b,cc".split(","));
//
Map<String, Long> sortMap = strList.stream().collect(Collectors.groupingBy(t -> t, Collectors.counting()));
Map<String, String> filterMap = new HashMap<>();
List<String> sortList = strList.stream().filter(t -> null == filterMap.put(t, t)).collect(Collectors.toList());
sortList.sort((t1, t2) -> (int) (sortMap.get(t2) - sortMap.get(t1)));
return sortList;
}
/**
* Lambda 排序
*/
public List<String> useLambda() {
List<String> strList = Arrays.asList("a,b,cc,b,b,cc".split(","));
//
Map<String, Integer> sortMap = new HashMap<>();
for (String s : strList) {
Integer count = sortMap.get(s);
if (null != count) {
sortMap.put(s, count + 1);
} else {
sortMap.put(s, 1);
}
}
//
List<Entry<String, Integer>> entryList = new ArrayList<>(sortMap.entrySet());
entryList.sort((t1, t2) -> t2.getValue() - t1.getValue());
//
List<String> listResult = new ArrayList<>();
for (Entry<String, Integer> mapping : entryList) {
listResult.add(mapping.getKey());
}
return listResult;
}
/**
* Comparator 排序
*/
public List<String> useComparator() {
List<String> strList = Arrays.asList("a,b,cc,b,b,cc".split(","));
//
Map<String, Integer> map = new TreeMap<String, Integer>();
for (String key : strList) {
Integer value = map.get(key);
if (null == value) {
map.put(key, 1);
} else {
map.put(key, value + 1);
}
}
//
List<Entry<String, Integer>> list = new ArrayList<>(map.entrySet());
Collections.sort(list, new Comparator<Entry<String, Integer>>() {
@Override
public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) {
return o2.getValue().compareTo(o1.getValue());
}
});
//
List<String> listResult = new ArrayList<>();
for (Entry<String, Integer> mapping : list) {
listResult.add(mapping.getKey());
}
return listResult;
}
/**
* Comparable 排序
*/
public List<String> useComparable(){
List<String> strList = Arrays.asList("a,b,cc,b,b,cc".split(","));
//
List<OrderAssist> listOrder = new ArrayList<>();
for(String s : strList){
OrderAssist tmp = new OrderAssist(s, 1);
if(listOrder.contains(tmp)){
listOrder.get(listOrder.indexOf(tmp)).count++;
}else{
listOrder.add(tmp);
}
}
Collections.sort(listOrder);
//
List<String> listResult = new ArrayList<>();
for(OrderAssist object : listOrder){
listResult.add(object.name);
}
return listResult;
}
//comparable辅助类
class OrderAssist implements Comparable<OrderAssist>{
public String name;
public int count;
public OrderAssist(String name, int count){
this.name = name;
this.count = count;
}
@Override
public boolean equals(Object object){
return this.name.equals(((OrderAssist)object).name);
}
@Override
public int compareTo(OrderAssist entity) {
return entity.count - count;
}
}
感谢你能够认真阅读完这篇文章,希望小编分享的“Lambda如何实现字符串出现次数排序算法”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。