当前位置:网站首页>Map的entrySet实现map按key升序排列或value降序排列
Map的entrySet实现map按key升序排列或value降序排列
2022-06-10 07:20:00 【zhangsan3333】
TreeMap<Integer, Integer>按key升序排列
TreeMap<Integer, Integer>按value降序排列
package MapTest;
import java.util.*;
public class Map4 {
public static void main(String[] args) {
TreeMap<Integer, Integer> treeMap = new TreeMap<>();
treeMap.put(1, 310);
treeMap.put(3, 310);
treeMap.put(4, 539);
treeMap.put(2, 420);
ArrayList<Map.Entry<Integer, Integer>> list = new ArrayList<>(treeMap.entrySet());
//按key升序排列
Collections.sort(list, new Comparator<Map.Entry<Integer, Integer>>() {
@Override
public int compare(Map.Entry<Integer, Integer> o1, Map.Entry<Integer, Integer> o2) {
//o1-o2升序排列
return o1.getKey() - o2.getKey();
}
});
for (Map.Entry<Integer, Integer> entry : list) {
System.out.println(entry.getKey() + " " + entry.getValue());
}
System.out.println();
//按value降序排列
Collections.sort(list, new Comparator<Map.Entry<Integer, Integer>>() {
@Override
public int compare(Map.Entry<Integer, Integer> o1, Map.Entry<Integer, Integer> o2) {
//o2-o1降序排列
return o2.getValue() - o1.getValue();
}
});
for (Map.Entry<Integer, Integer> entry : list) {
System.out.println(entry.getKey() + " " + entry.getValue());
}
}
}

Map<Integer, String[]>按照key升序排列
package MapTest;
import java.util.*;
//按照key升序排列
public class Map4 {
public static void main(String[] args) {
HashMap<Integer, String[]> map = new HashMap<>();
map.put(1, new String[]{
"4536117005899"});
map.put(3, new String[]{
"3424342341666"});
map.put(4, new String[]{
"5536117001860"});
map.put(2, new String[]{
"2022032600001", "2022032600002", "2022032600003"});
ArrayList<Map.Entry<Integer, String[]>> list = new ArrayList<>(map.entrySet());
//按key升序排列
Collections.sort(list, new Comparator<Map.Entry<Integer, String[]>>() {
@Override
public int compare(Map.Entry<Integer, String[]> o1, Map.Entry<Integer, String[]> o2) {
//o1-o2升序排列
return o1.getKey() - o2.getKey();
}
});
for (Map.Entry<Integer, String[]> entry : list) {
System.out.println(entry.getKey() + " " + Arrays.toString(entry.getValue()));
}
}
}

Map<String, String>按照value升序排列
package MapTest;
import java.util.*;
//## Map<String, String>按照value升序排列
public class Map5 {
public static void main(String[] args) {
Map<String, String> map = new HashMap<String, String>();
map.put("b", "4");
map.put("a", "5");
map.put("c", "3");
map.put("d", "5");
//将map<String,String> 转化为ArryList,但list里面的元素为Entry<String,String>
List<Map.Entry<String, String>> list = new ArrayList<Map.Entry<String, String>>(map.entrySet());
Collections.sort(list, new Comparator<Map.Entry<String, String>>() {
@Override
public int compare(Map.Entry<String, String> o1, Map.Entry<String, String> o2) {
//升序排列
int flag = o1.getValue().compareTo(o2.getValue());
//value相等
if (flag == 0) {
return o1.getKey().compareTo(o2.getKey());
}
return flag;
}
});
//遍历list得到map里面排序后的元素
for (Map.Entry<String, String> en : list) {
System.out.println(en.getKey() + " " + en.getValue());
}
}
}

边栏推荐
- Instagram CEO: "Apple iPad is not very popular, and it is not worth developing an exclusive version of the app"
- JS method to verify whether the content of the input box is empty
- How to modify photos of downloaded word resume templates
- 张小白教你使用OGG实现Oracle 19C到MySQL 5.7的数据同步(4)
- Mongo, mongodb optimization ideas
- Nignx configuring websocket
- [dry goods] typescript: extensions, infer and DVA type
- 29. solution for 300ms delay time of click event at mobile terminal
- How R language uses ggplot2 to draw QQ graph and box graph
- Applet: get the current page routing information through getcurrentpages
猜你喜欢

New Phytologist: Wang Jianjun, et al. Reviewed the mountain microbiome and ecosystem functions under global change

Analysis: stable currency is not "stable currency", but a product in essence

What if online gambling can't be withdrawn?

PS 2022 installation failure error code 182 solution

How R language uses ggplot2 to draw QQ graph and box graph

leetcode.38 ---外观数列

LabVIEW controls Arduino to realize infrared ranging (advanced chapter-6)

How to use Navicat to create an associated primary foreign key for two tables

想要粽子可以,但是得经过我的认证授权才可以

How to modify photos of downloaded word resume templates
随机推荐
All in one 1281 Dynamic programming of the longest ascending subsequence solution
JS数据交互之本地存储
Chenxi bookkeeping book for bookkeeping, and use items to view accounts
RT thread design and Implementation: RT thread overview and architecture
How to get the small icon in the title on the website
Daily practice: Longest symmetric string (pay attention to the last two methods)
leetcode.38 ---外观数列
SQL makes a column empty
Ros2+gazebo11+car+opencv line patrol recognition and speed steering control learning
Implicit type conversion (integer promoted part)
[C language] C language programming: dynamic address book
Long press the page at the mobile terminal, do not copy text and single picture
qt制作简易的视频通话
Fastjson using notes
Custom annotations support spiel expressions (dynamic parameters)
如何快速剪辑多个短视频,去除视频片尾
loj10131. 「一本通 4.4 例 2」暗的连锁 题解 树上差分(边差分)
Arena, a new proposal of the go language community, can optimize memory allocation
autojs与冰狐智能辅助的优缺点
618 l'informatique en nuage stimule la diffusion en direct du commerce électronique