In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
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!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.