java.util.Map接口
来源:互联网 发布:光学字符识别软件 编辑:程序博客网 时间:2024/06/08 09:04
先看代码
public class A {public void linkedhashmap() {System.out.println("LinkedHashMap");Map<Integer, Integer> linkedHashmap = new LinkedHashMap();linkedHashmap.put(6, 6);linkedHashmap.put(100, 100);linkedHashmap.put(3, 3);linkedHashmap.put(5, 5);Iterator<Map.Entry<Integer, Integer>> it = linkedHashmap.entrySet().iterator();while (it.hasNext()) {Map.Entry<Integer, Integer> entry = it.next();System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());}}public void hashmap() {System.out.println("HashMap");Map<Integer, Integer> hashmap = new HashMap();hashmap.put(6, 6);hashmap.put(100, 100);hashmap.put(3, 3);hashmap.put(5, 5);Iterator<Map.Entry<Integer, Integer>> it = hashmap.entrySet().iterator();while (it.hasNext()) {Map.Entry<Integer, Integer> entry = it.next();System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());}}public void treemap() {System.out.println("TreeMap");Map<Integer, Integer> treemap = new TreeMap();treemap.put(6, 6);treemap.put(100, 100);treemap.put(3, 3);treemap.put(5, 5);Iterator<Map.Entry<Integer, Integer>> it = treemap.entrySet().iterator();while (it.hasNext()) {Map.Entry<Integer, Integer> entry = it.next();System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());}}public static void main(String[] args) {A a = new A();a.linkedhashmap();a.hashmap();a.treemap();}}
HashMap不保证顺序,TreeMap根据key值顺序,LinkedHashMap根据插入的顺序。
输入结果为:
LinkedHashMap
key= 6 and value= 6
key= 100 and value= 100
key= 3 and value= 3
key= 5 and value= 5
HashMap
key= 3 and value= 3
key= 100 and value= 100
key= 5 and value= 5
key= 6 and value= 6
TreeMap
key= 3 and value= 3
key= 5 and value= 5
key= 6 and value= 6
key= 100 and value= 100
HashMap:
TreeMap:
LinkedHashMap:
hash
0 0
- java.util.Map接口
- 接口java.util.Map原理
- 接口java.util.Map.Entry的使用
- 接口java.util.Map.Entry的使用
- 『java.util』 map接口
- 接口java.util.Map.Entry的使用
- 接口java.util.Map.Entry的使用
- 接口java.util.Map.Entry的使用
- 接口java.util.Map.Entry的使用
- java8中java.util.Map接口新增默认方法说明
- java.util.Map翻译
- java.util 中 map
- java.util.Map
- java.util.Map
- java.util.Map
- 接口 java.util.Enumeration
- java.util.Collection接口
- java.util.Enumeration接口
- Servlet自动加载
- static关键字
- 陈大年:创业16年来,我深深意识到,「拼命创业就能成功」是错误的
- 内容滚动条和子div高度自适应
- 蓝桥杯-排他平方数
- java.util.Map接口
- 3月16日计算机课小练
- 九度OJ_1201:二叉排序树
- Linux.网络抓包库libpcap
- 【征集告示】2017年第二届中国江西工艺美术(陶瓷)佳构..
- saxon-he报错
- New 开始~page
- POI+Maven实现数据导出到Excel
- 将树莓派安装到U盘上,并实现扩容