您好,登錄后才能下訂單哦!
這篇文章主要介紹了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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。