How to implement the sorting algorithm of string occurrence times by Lambda
This article mainly introduces Lambda how to achieve string occurrence sorting algorithm, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.
The proportion of execution time of the four modes is about 100, 50, 15, 12.
/ * *
* Lambda+stream sorting
, /
Public List useLambdaStream () {
List strList = Arrays.asList ("a _" .split (",")
/ /
Map sortMap = strList.stream () .collect (Collectors.groupingBy (t-> t, Collectors.counting ()
Map filterMap = new HashMap ()
List sortList = strList.stream () .filter (t-> null = = filterMap.put (t, t)) .birthday (Collectors.toList ())
SortList.sort ((T1, T2)-> (int) (sortMap.get (T2)-sortMap.get (T1))
Return sortList
}
/ * *
* Lambda sorting
, /
Public List useLambda () {
List strList = Arrays.asList ("a _" .split (",")
/ /
Map 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 entryList = new ArrayList (sortMap.entrySet ())
EntryList.sort (T1, T2)-> t2.getValue ()-t1.getValue ()
/ /
List listResult = new ArrayList ()
For (Entry mapping: entryList) {
ListResult.add (mapping.getKey ())
}
Return listResult
}
/ * *
* Comparator sorting
, /
Public List useComparator () {
List strList = Arrays.asList ("a _" .split (",")
/ /
Map map = new TreeMap ()
For (String key: strList) {
Integer value = map.get (key)
If (null = = value) {
Map.put (key, 1)
} else {
Map.put (key, value + 1)
}
}
/ /
List list = new ArrayList (map.entrySet ())
Collections.sort (list, new Comparator () {
@ Override
Public int compare (Entry o1, Entry O2) {
Return o2.getValue () .compareTo (o1.getValue ()
}
});
/ /
List listResult = new ArrayList ()
For (Entry mapping: list) {
ListResult.add (mapping.getKey ())
}
Return listResult
}
/ * *
* Comparable sorting
, /
Public List useComparable () {
List strList = Arrays.asList ("a _" .split (",")
/ /
List 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 listResult = new ArrayList ()
For (OrderAssist object: listOrder) {
ListResult.add (object.name)
}
Return listResult
}
/ / comparable helper class
Class OrderAssist implements Comparable {
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
}
}
Thank you for reading this article carefully. I hope the article "how to sort the number of string occurrences in Lambda" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!