当前位置:网站首页>The entryset of map implements that maps are arranged in ascending order by key or descending order by value
The entryset of map implements that maps are arranged in ascending order by key or descending order by value
2022-06-10 07:25:00 【zhangsan3333】
TreeMap<Integer, Integer> Press key Ascending order
TreeMap<Integer, Integer> Press value Descending order
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());
// Press key Ascending order
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 Ascending order
return o1.getKey() - o2.getKey();
}
});
for (Map.Entry<Integer, Integer> entry : list) {
System.out.println(entry.getKey() + " " + entry.getValue());
}
System.out.println();
// Press value Descending order
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 Descending order
return o2.getValue() - o1.getValue();
}
});
for (Map.Entry<Integer, Integer> entry : list) {
System.out.println(entry.getKey() + " " + entry.getValue());
}
}
}

Map<Integer, String[]> according to key Ascending order
package MapTest;
import java.util.*;
// according to key Ascending order
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());
// Press key Ascending order
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 Ascending order
return o1.getKey() - o2.getKey();
}
});
for (Map.Entry<Integer, String[]> entry : list) {
System.out.println(entry.getKey() + " " + Arrays.toString(entry.getValue()));
}
}
}

Map<String, String> according to value Ascending order
package MapTest;
import java.util.*;
//## Map<String, String> according to value Ascending order
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");
// take map<String,String> Turn into ArryList, but list The elements are 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) {
// Ascending order
int flag = o1.getValue().compareTo(o2.getValue());
//value equal
if (flag == 0) {
return o1.getKey().compareTo(o2.getKey());
}
return flag;
}
});
// Traverse list obtain map Sorted elements inside
for (Map.Entry<String, String> en : list) {
System.out.println(en.getKey() + " " + en.getValue());
}
}
}

边栏推荐
- Arduino配置ESP32开发环境
- Chenxi bookkeeping book for bookkeeping, and use items to view accounts
- Typecho模板 vCards/简约个性便的个人博客主题模板
- Arduino configuring esp32 development environment
- What's new in openlayers 6.13
- 28. mobile terminal touch event
- Matlab: 多项式表示及其基本运算
- bson,json
- Summary of CUDA parallel computing optimization strategies
- 网上开户是安全的吗?
猜你喜欢

3 ZK's election mechanism

【宽度优先搜索】LeetCode1091二进制矩阵中的最短路径

I/o basic knowledge sorting

Instagram CEO: "Apple iPad is not very popular, and it is not worth developing an exclusive version of the app"

『Three.js』起飞!

Around ifelse and business logic

How to modify photos of downloaded word resume templates

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

You can have zongzi if you want, but you have to go through my authentication and authorization

LabVIEW controls Arduino to realize infrared ranging (advanced chapter-6)
随机推荐
用友OA漏洞复现手册
P1073 [noip2009 improvement group] optimal trade problem solving hierarchical graph shortest path
WPF 数据绑定(三)
Opencv learning (II) -- installing opencv on raspberry pie
mongo,mongodb优化思路
19 r judgment control function exercise
3 zk的选举机制
How can smart cities accelerate their development with the help of cloud computing?
618 l'informatique en nuage stimule la diffusion en direct du commerce électronique
[econometrics] instrumental variable estimation and two-stage least square method
Typecho template vcards/ simple and personalized personal blog theme template
Successfully solved: importerror: cannot import name 'import' from 'sklearn preprocessing
mysql之general-log
一本通1281.最长上升子序列 题解 动态规划
What's new in openlayers 6.13
New Phytologist: Wang Jianjun, et al. Reviewed the mountain microbiome and ecosystem functions under global change
Rk3399 default browser and Chinese language
Next generation enterprise IT architecture: cloud native architecture
Instagram CEO: "Apple iPad is not very popular, and it is not worth developing an exclusive version of the app"
P1073 [NOIP2009 提高组] 最优贸易 题解 分层图最短路