当前位置 : 主页 > 网络编程 > 其它编程 >

java数据集合Map接口

来源:互联网 收集:自由互联 发布时间:2023-07-02
转载请注明出处:http:blog.csdn.netdroyonarticledetails22893461Map接口不是Collection接口的继承类。Map接口用于维护键值对 转载请注明出处:http://blog.csdn.net/droyon/article/details/22893461 Map接口不是Collec
转载请注明出处:http:blog.csdn.netdroyonarticledetails22893461Map接口不是Collection接口的继承类。Map接口用于维护键值对 转载请注明出处:http://blog.csdn.net/droyon/article/details/22893461

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 hashMap new HashMap();hashMap.put(1, "111");hashMap.put(2, "222");hashMap.put(3, "333");//【重要】hashMap.put(3, "444");//【重要】Set 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 hashMapSort new HashMap();hashMapSort.put(new Index2(), "111");hashMapSort.put(new Index2(), "222");Index2 ind new Index2();hashMapSort.put(ind, "333");hashMapSort.put(ind, "555");Set setSort hashMapSort.keySet();for(Entry entry:hashMapSort.entrySet()){System.out.println("HashMap排序entry is:"entry);}System.out.println("----------------------LinkedHashMap");/*** LinkedHashMap* 增、删、改较快*/LinkedHashMap linkMap new LinkedHashMap();linkMap.put(new Index(), "444");linkMap.put(new Index(), "777");linkMap.put(new Index(), "666");for(Entry entry:linkMap.entrySet()){System.out.println("entry is:"entry);}System.out.println("-----------------------TreeMap");/*** TreeMap* 红黑树实现* 【红黑树可以实现排序】*/TreeMap treeMap new TreeMap();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 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);}Overridepublic String toString() {return "index is:"indexsuper.toString();}}/*** 倒序排序* author wanghl**/class Index2 implements Comparable {private int index;private static AtomicInteger sIndex new AtomicInteger();public Index2(){index sIndex.addAndGet(1);}public Index2(int i){index sIndex.addAndGet(i);}Overridepublic String toString() {return "index is:"indexsuper.toString();}Overridepublic 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 hashMap new HashMap();hashMap.put(1, "111");hashMap.put(2, "222");hashMap.put(3, "333");//【重要】hashMap.put(3, "444");//【重要】Set 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 hashMapSort new HashMap();hashMapSort.put(new Index2(), "111");hashMapSort.put(new Index2(), "222");Index2 ind new Index2();hashMapSort.put(ind, "333");hashMapSort.put(ind, "555");Set setSort hashMapSort.keySet();for(Entry entry:hashMapSort.entrySet()){System.out.println("HashMap排序entry is:"entry);}System.out.println("----------------------LinkedHashMap");/*** LinkedHashMap* 增、删、改较快*/LinkedHashMap linkMap new LinkedHashMap();linkMap.put(new Index(), "444");linkMap.put(new Index(), "777");linkMap.put(new Index(), "666");for(Entry entry:linkMap.entrySet()){System.out.println("entry is:"entry);}System.out.println("-----------------------TreeMap");/*** TreeMap* 红黑树实现* 【红黑树可以实现排序】*/TreeMap treeMap new TreeMap();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 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);}Overridepublic String toString() {return "index is:"indexsuper.toString();}}/*** 倒序排序* author wanghl**/class Index2 implements Comparable {private int index;private static AtomicInteger sIndex new AtomicInteger();public Index2(){index sIndex.addAndGet(1);}public Index2(int i){index sIndex.addAndGet(i);}Overridepublic String toString() {return "index is:"indexsuper.toString();}Overridepublic int compareTo(Index2 other) {return (other.index - this.index);}}

上一篇:如何卸载服务器硬盘
下一篇:没有了
网友评论