java数据集合Map接口
2014-04-03 21:22
369 查看
转载请注明出处:http://blog.csdn.net/droyon/article/details/22893461
Map接口不是Collection接口的继承类。Map接口用于维护键/值对。(Key/value),描述了从不重复的键到值的映射。
Map中不能有重复的键,Map实现类中存储的“键值”映射对是通过键来唯一标示的。Map底层的键是用Set来存放的。
Map接口不是Collection接口的继承类。Map接口用于维护键/值对。(Key/value),描述了从不重复的键到值的映射。
Map中不能有重复的键,Map实现类中存储的“键值”映射对是通过键来唯一标示的。Map底层的键是用Set来存放的。
Map接口不是Collection接口的继承类。Map接口用于维护键/值对。(Key/value),描述了从不重复的键到值的映射。
Map中不能有重复的键,Map实现类中存储的“键值”映射对是通过键来唯一标示的。Map底层的键是用Set来存放的。
import java.util.HashMap; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.LinkedHashSet; import java.util.Map.Entry; import java.util.Properties; import java.util.Set; import java.util.TreeMap; import java.util.concurrent.atomic.AtomicInteger; public class MapTest { public static void main(String args[]){ System.out.println("-------------------HashMap"); /** * HashMap 使用频率较高,索引,查找较快 * 存储键值相同的entry,会替换原有的entry。 */ HashMap<Integer, String> hashMap = new HashMap<Integer, String>(); hashMap.put(1, "111"); hashMap.put(2, "222"); hashMap.put(3, "333");//【重要】 hashMap.put(3, "444");//【重要】 Set<Integer> set = hashMap.keySet(); Iterator iterator = set.iterator(); while(iterator.hasNext()){ System.out.println("....HashMap元素遍历。 元素为:"+hashMap.get(iterator.next())); } System.out.println("------------------------"); /** * HashMap使用Set进行key值存放,Set实现Compareable接口不会影响HashMap的顺序 * 存储键值相同的entry,会替换原有的entry。 */ HashMap<Index2, String> hashMapSort = new HashMap<Index2, String>(); hashMapSort.put(new Index2(), "111"); hashMapSort.put(new Index2(), "222"); Index2 ind = new Index2(); hashMapSort.put(ind, "333"); hashMapSort.put(ind, "555"); Set<Index2> setSort = hashMapSort.keySet(); for(Entry<Index2, String> entry:hashMapSort.entrySet()){ System.out.println("HashMap排序:entry is:"+entry); } System.out.println("----------------------LinkedHashMap"); /** * LinkedHashMap * 增、删、改较快 */ LinkedHashMap<Index, String> linkMap = new LinkedHashMap<Index, String>(); linkMap.put(new Index(), "444"); linkMap.put(new Index(), "777"); linkMap.put(new Index(), "666"); for(Entry<Index, String> entry:linkMap.entrySet()){ System.out.println("entry is:"+entry); } System.out.println("-----------------------TreeMap"); /** * TreeMap * 红黑树实现 * 【红黑树可以实现排序】 */ TreeMap<Index2, String> treeMap = new TreeMap<Index2, String>(); treeMap.put(new Index2(), "444"); treeMap.put(new Index2(), "555"); treeMap.put(new Index2(2), "666"); Index2 ind2 = new Index2(); treeMap.put(ind2, "777"); treeMap.put(ind2, "888"); System.out.println("第一个key:"+treeMap.firstKey()); for(Entry<Index2, String> entry:treeMap.entrySet()){ System.out.println("entry is:"+entry); } System.out.println("-----------Properties"); /** * Properties * 哈希树实现 */ Properties properties = new Properties(); properties.setProperty("aaa", "111"); properties.setProperty("bbb", "222"); properties.setProperty("aaa", "333"); Set propertiesSet = properties.keySet(); for(Object o:propertiesSet){ System.out.println("-----Properties value is:"+properties.getProperty((String)o)); } } } class Index { private int index; private static AtomicInteger sIndex = new AtomicInteger(); public Index(){ index = sIndex.addAndGet(1); } @Override public String toString() { return "index is:"+index+super.toString(); } } /** * 倒序排序 * @author wanghl * */ class Index2 implements Comparable<Index2> { private int index; private static AtomicInteger sIndex = new AtomicInteger(); public Index2(){ index = sIndex.addAndGet(1); } public Index2(int i){ index = sIndex.addAndGet(i); } @Override public String toString() { return "index is:"+index+super.toString(); } @Override public int compareTo(Index2 other) { return (other.index - this.index); } }
Map接口不是Collection接口的继承类。Map接口用于维护键/值对。(Key/value),描述了从不重复的键到值的映射。
Map中不能有重复的键,Map实现类中存储的“键值”映射对是通过键来唯一标示的。Map底层的键是用Set来存放的。
import java.util.HashMap; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.LinkedHashSet; import java.util.Map.Entry; import java.util.Properties; import java.util.Set; import java.util.TreeMap; import java.util.concurrent.atomic.AtomicInteger; public class MapTest { public static void main(String args[]){ System.out.println("-------------------HashMap"); /** * HashMap 使用频率较高,索引,查找较快 * 存储键值相同的entry,会替换原有的entry。 */ HashMap<Integer, String> hashMap = new HashMap<Integer, String>(); hashMap.put(1, "111"); hashMap.put(2, "222"); hashMap.put(3, "333");//【重要】 hashMap.put(3, "444");//【重要】 Set<Integer> set = hashMap.keySet(); Iterator iterator = set.iterator(); while(iterator.hasNext()){ System.out.println("....HashMap元素遍历。 元素为:"+hashMap.get(iterator.next())); } System.out.println("------------------------"); /** * HashMap使用Set进行key值存放,Set实现Compareable接口不会影响HashMap的顺序 * 存储键值相同的entry,会替换原有的entry。 */ HashMap<Index2, String> hashMapSort = new HashMap<Index2, String>(); hashMapSort.put(new Index2(), "111"); hashMapSort.put(new Index2(), "222"); Index2 ind = new Index2(); hashMapSort.put(ind, "333"); hashMapSort.put(ind, "555"); Set<Index2> setSort = hashMapSort.keySet(); for(Entry<Index2, String> entry:hashMapSort.entrySet()){ System.out.println("HashMap排序:entry is:"+entry); } System.out.println("----------------------LinkedHashMap"); /** * LinkedHashMap * 增、删、改较快 */ LinkedHashMap<Index, String> linkMap = new LinkedHashMap<Index, String>(); linkMap.put(new Index(), "444"); linkMap.put(new Index(), "777"); linkMap.put(new Index(), "666"); for(Entry<Index, String> entry:linkMap.entrySet()){ System.out.println("entry is:"+entry); } System.out.println("-----------------------TreeMap"); /** * TreeMap * 红黑树实现 * 【红黑树可以实现排序】 */ TreeMap<Index2, String> treeMap = new TreeMap<Index2, String>(); treeMap.put(new Index2(), "444"); treeMap.put(new Index2(), "555"); treeMap.put(new Index2(2), "666"); Index2 ind2 = new Index2(); treeMap.put(ind2, "777"); treeMap.put(ind2, "888"); System.out.println("第一个key:"+treeMap.firstKey()); for(Entry<Index2, String> entry:treeMap.entrySet()){ System.out.println("entry is:"+entry); } System.out.println("-----------Properties"); /** * Properties * 哈希树实现 */ Properties properties = new Properties(); properties.setProperty("aaa", "111"); properties.setProperty("bbb", "222"); properties.setProperty("aaa", "333"); Set propertiesSet = properties.keySet(); for(Object o:propertiesSet){ System.out.println("-----Properties value is:"+properties.getProperty((String)o)); } } } class Index { private int index; private static AtomicInteger sIndex = new AtomicInteger(); public Index(){ index = sIndex.addAndGet(1); } @Override public String toString() { return "index is:"+index+super.toString(); } } /** * 倒序排序 * @author wanghl * */ class Index2 implements Comparable<Index2> { private int index; private static AtomicInteger sIndex = new AtomicInteger(); public Index2(){ index = sIndex.addAndGet(1); } public Index2(int i){ index = sIndex.addAndGet(i); } @Override public String toString() { return "index is:"+index+super.toString(); } @Override public int compareTo(Index2 other) { return (other.index - this.index); } }
相关文章推荐
- Python中map()函数浅析
- Android使用Google Map浅谈
- jquery与google map api结合使用 控件,监听器
- jquery遍历数组与筛选数组的方法
- Android Map新用法:MapFragment应用介绍
- js Map List 遍历使用示例
- velocity显示List与Map的方法详细解析
- Google Map Api和GOOGLE Search Api整合实现代码
- 关于STL中的map容器的一些总结
- vector,map,list,queue的区别详细解析
- 浅析stl序列容器(map和set)的仿函数排序
- Java中List与Map初始化的一些写法分享
- Java用for循环Map详细解析
- 5个数组Array方法: indexOf、filter、forEach、map、reduce使用实例
- JAVA Map架构和API介绍
- list,set,map,数组之间的相互转换详细解析
- java map遍历的四种方法总结
- 浅析Java中Map与HashMap,Hashtable,HashSet的区别
- HAZELCAST 客户端命令 可用于简单调试
- JDK API阅读体会 2-哈希还是哈希