JAVA SortedMap接口
SortedMap接口主要提供有序的Map实现。
Map的主要实现有HashMap,TreeMap,HashTable,LinkedHashMap。
TreeMap实现了SortedMap接口,保证了有序性。默认的排序是根据key值进行升序排序,也可以重写comparator方法来根据value进行排序。
HashMap与TreeMap的比较
-
public class SortedMapTest2 {
-
-
public static void main(String[] args) {
-
-
Map<String,Object> hashMap = new HashMap<String,Object>();
-
hashMap.put("1", "a");
-
hashMap.put("5", "b");
-
hashMap.put("2", "c");
-
hashMap.put("4", "d");
-
hashMap.put("3", "e");
-
Set<Entry<String, Object>> entry = hashMap.entrySet();
-
for(Entry<String, Object> temp : entry){
-
System.out.println("hashMap:"+temp.getKey()+" 值"+temp.getValue());
-
}
-
-
System.out.println("\n");
-
-
SortedMap<String,Object> sortedMap = new TreeMap<String,Object>();
-
sortedMap.put("1", "a");
-
sortedMap.put("5", "b");
-
sortedMap.put("2", "c");
-
sortedMap.put("4", "d");
-
sortedMap.put("3", "e");
-
Set<Entry<String, Object>> entry2 = sortedMap.entrySet();
-
for(Entry<String, Object> temp : entry2){
-
System.out.println("sortedMap:"+temp.getKey()+" 值"+temp.getValue());
-
}
-
-
}
-
-
}
运算的结果为
-
hashMap:1 值a
-
hashMap:2 值c
-
hashMap:3 值e
-
hashMap:4 值d
-
hashMap:5 值b
-
-
sortedMap:1 值a
-
sortedMap:2 值c
-
sortedMap:3 值e
-
sortedMap:4 值d
-
sortedMap:5 值b
看上去还以为HashMap也保证了有序性,其实是随机的,如果值设置的复杂一点,如下例:
-
public class SortedMapTest3 {
-
-
public static void main(String[] args) {
-
-
Map<String,Object> hashMap = new HashMap<String,Object>();
-
hashMap.put("1b", "a");
-
hashMap.put("2", "b");
-
hashMap.put("4b", "d");
-
hashMap.put("3", "c");
-
hashMap.put("2b", "d");
-
hashMap.put("3b", "c");
-
Set<Entry<String, Object>> entry = hashMap.entrySet();
-
for(Entry<String, Object> temp : entry){
-
System.out.println("hashMap:"+temp.getKey()+" 值"+temp.getValue());
-
}
-
-
System.out.println("\n");
-
-
SortedMap<String,Object> sortedMap = new TreeMap<String,Object>();
-
sortedMap.put("1b", "a");
-
sortedMap.put("2", "b");
-
sortedMap.put("4b", "d");
-
sortedMap.put("3", "c");
-
sortedMap.put("2b", "d");
-
sortedMap.put("3b", "c");
-
Set<Entry<String, Object>> entry2 = sortedMap.entrySet();
-
for(Entry<String, Object> temp : entry2){
-
System.out.println("sortedMap:"+temp.getKey()+" 值"+temp.getValue());
-
}
-
-
}
-
-
}
运算的结果是:
-
hashMap:2b 值d
-
hashMap:1b 值a
-
hashMap:2 值b
-
hashMap:3 值c
-
hashMap:4b 值d
-
hashMap:3b 值c
-
-
sortedMap:1b 值a
-
sortedMap:2 值b
-
sortedMap:2b 值d
-
sortedMap:3 值c
-
sortedMap:3b 值c
-
sortedMap:4b 值d
很显然只有TreeMap保证了有序性。
那如果想要根据value值来进行排序
-
public class SortedMapTest {
-
-
public static void main(String[] args) {
-
-
SortedMap<String,String> sortedMap = new TreeMap<String,String>();
-
sortedMap.put("1", "a");
-
sortedMap.put("5", "b");
-
sortedMap.put("2", "c");
-
sortedMap.put("4", "d");
-
sortedMap.put("3", "e");
-
Set<Entry<String, String>> entry2 = sortedMap.entrySet();
-
for(Entry<String, String> temp : entry2){
-
System.out.println("修改前 :sortedMap:"+temp.getKey()+" 值"+temp.getValue());
-
}
-
System.out.println("\n");
-
-
//这里将map.entrySet()转换成list
-
List<Map.Entry<String,String>> list =
-
new ArrayList<Map.Entry<String,String>>(entry2);
-
-
Collections.sort(list, new Comparator<Map.Entry<String,String>>(){
-
-
@Override
-
public int compare(Entry<String, String> o1, Entry<String, String> o2) {
-
// TODO Auto-generated method stub
-
return o1.getValue().compareTo(o2.getValue());
-
}
-
-
});
-
-
for(Map.Entry<String,String> temp :list){
-
System.out.println("修改后 :sortedMap:"+temp.getKey()+" 值"+temp.getValue());
-
}
-
}
-
-
}
运行结果为:
-
修改前 :sortedMap:1 值a
-
修改前 :sortedMap:2 值c
-
修改前 :sortedMap:3 值e
-
修改前 :sortedMap:4 值d
-
修改前 :sortedMap:5 值b
-
-
修改后 :sortedMap:1 值a
-
修改后 :sortedMap:5 值b
-
修改后 :sortedMap:2 值c
-
修改后 :sortedMap:4 值d
-
修改后 :sortedMap:3 值e
JAVA SortedMap接口的更多相关文章
- Java 数据类型:集合接口Map:HashTable;HashMap;IdentityHashMap;LinkedHashMap;Properties类读取配置文件;SortedMap接口和TreeMap实现类:【线程安全的ConcurrentHashMap】
Map集合java.util.Map Map用于保存具有映射关系的数据,因此Map集合里保存着两个值,一个是用于保存Map里的key,另外一组值用于保存Map里的value.key和value都可以是 ...
- SortedMap接口:进行排序操作。
回顾:SortedSet是TreeSet的实现接口,此接口可以排序. SortedMap接口同样可以排序,是TreeMap的实现接口,父类. 定义如下: public class TreeMap< ...
- SortedMap接口的实现类TreeMap介绍和实现Comparator自定义比较器(转)
与SortedSet接口类似,SortedMap也是一个结构,待排序的Map,其一个比较常用的实现类是TreeMap. TreeMap的put(K key, V value)方法在每添加一个元素时,都 ...
- SortedMap接口
SortedMap接口是排序接口,只要是实现了此接口的子类,都属于排序的子类,TreeMap也是此接口的一个子类. import java.util.Map; import java.util.Sor ...
- 深入理解Java的接口和抽象类(转)
深入理解Java的接口和抽象类 对于面向对象编程来说,抽象是它的一大特征之一.在Java中,可以通过两种形式来体现OOP的抽象:接口和抽象类.这两者有太多相似的地方,又有太多不同的地方.很多人在初学的 ...
- 深入理解Java的接口和抽象类
深入理解Java的接口和抽象类 对于面向对象编程来说,抽象是它的一大特征之一.在Java中,可以通过两种形式来体现OOP的抽象:接口和抽象类.这两者有太多相似的地方,又有太多不同的地方.很多人在初学的 ...
- java微信接口之五—消息分组群发
一.微信消息分组群发接口简介 1.请求:该请求是使用post提交地址为: https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_t ...
- java微信接口之四—上传素材
一.微信上传素材接口简介 1.请求:该请求是使用post提交地址为: https://api.weixin.qq.com/cgi-bin/media/uploadnews?access_token=A ...
- android 学习随笔二十七(JNI:Java Native Interface,JAVA原生接口 )
JNI(Java Native Interface,JAVA原生接口) 使用JNI可以使Java代码和其他语言写的代码(如C/C++代码)进行交互. 问:为什么要进行交互? 首先,Java语言提供的类 ...
随机推荐
- x264代码剖析(十五):核心算法之宏块编码中的变换编码
x264代码剖析(十五):核心算法之宏块编码中的变换编码 为了进一步节省图像的传输码率.须要对图像进行压缩,通常採用变换编码及量化来消除图像中的相关性以降低图像编码的动态范围.本文主要介绍变换编码的相 ...
- C语言深度剖析-----数组参数和指针参数分析
数组退化的意义 当向函数传递数组时, 二维数组参数 等价关系 注意事项 只能去一维数组 无法向一个函数传递一个任意的多维数组,注释地方出错 传递与访问二维数组的方式 动态地算出二维数组的列
- 【Codeforces Round #435 (Div. 2) A】Mahmoud and Ehab and the MEX
[链接]h在这里写链接 [题意] 在这里写题意 [题解] 让x没有出现,以及0..x-1都出现就可以了. [错的次数] 0 [反思] 在这了写反思 [代码] #include <bits/std ...
- js实现点击不同的按钮后各自返回被点击的次数
js实现点击不同的按钮后各自返回被点击的次数 一.总结 1.注意:返回的不是三个按钮总的点击数,而是每一个的 2.用全局变量的话每一个按钮要多一个函数,用闭包就很方便 二.js实现点击不同的按钮后各自 ...
- 微信端 h5 视频 video 自动播放
document.addEventListener("WeixinJSBridgeReady",function(){ document.getElementById(" ...
- 【u247】生物进化
Time Limit: 1 second Memory Limit: 128 MB [问题描述] 在一片茂密的原始森林中,生物学家们发现了几种远古时期的动物化石.他们将化石依次编号为1,2,3,--n ...
- ios开发之Quartz2D 四:画饼图
#import "PieView.h" @implementation PieView - (void)drawRect:(CGRect)rect { // Drawing cod ...
- php实现包含min函数的栈(这个题目用另外一个栈做单调栈的话时间复杂度会低很多)
php实现包含min函数的栈(这个题目用另外一个栈做单调栈的话时间复杂度会低很多) 一.总结 这个题目用另外一个栈做单调栈的话时间复杂度会低很多 二.php实现包含min函数的栈 题目描述 定义栈的数 ...
- vs 外部依赖项、附加依赖项以及如何添加依赖项目
我们在 VS 中创建 Win32 控制台应用程序,vs 会为解决方案创建默认地创建 4 个 filters(资源管理器中没有对应的目录和文件夹): 头文件:一般为 .h 文件 外部依赖项 源文件:一般 ...
- eclipse 远程debug tomcat web项目
1.首先须要在linux系统tomcat/bin文件夹下配置catalina.sh这个文件里添加: CATALINA_OPTS="-Xdebug -Xrunjdwp:transport=d ...