您的位置:首页 > 编程语言 > Java开发

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来存放的。

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);
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Map