java数据集合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<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);
	}
	
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hailushijie

您的鼓励是我创作最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值