您好,登錄后才能下訂單哦!
這篇文章主要介紹了Lambda如何實(shí)現(xiàn)字符串出現(xiàn)次數(shù)排序算法,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
四種方式的執(zhí)行時(shí)間比例大概是 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;
}
}
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“Lambda如何實(shí)現(xiàn)字符串出現(xiàn)次數(shù)排序算法”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。