Collection 之Map

 

Map 接口是实现了类的键值对形式存储数据(key-value)

Map接口有HashMap和TreeMap子类

   HashMap 是数据结构的哈希表方式的结构。

   TreeMap 是数据结构的二叉树结构.什么是二叉树呢?主要就是一个元素下面只有两个元素。

 

Map类中存储的是键值对的标识,所以键不能重复。

Map中键值对的比较是通过equals和hashCode方法,重写equals方法必须要重写hashCode方法。

 

package com.study;

import java.util.*;

public class MapDemo01 {
	
	public static void main(String []args) {
		Map hash  = new HashMap();
		Map tree = new TreeMap();
		
		/**
		hash.put("zds1", new Integer(100));
		hash.put("zds2", new Double(100.00));
		hash.put("zds3", new Integer(200));
		hash.put("zds4", 300);
		
		tree.put("1", 100);
		tree.put("2", new Boolean(true));
		
		
		System.out.println(hash.get("zds4"));
		System.out.println(hash.size());
		System.out.println(hash.containsKey("zds1"));
		System.out.println(hash.containsKey("zds11"));
		System.out.println(hash.containsValue(new Integer(200)));
		System.out.println(hash.containsValue(new Integer(2002)));
		
		System.out.println(hash.put("zds3", new Integer(222)));//把新数据放入value中,在提取老数据的值返回
		
		Map test = new HashMap(tree);
		test.putAll(hash);
		System.out.println(test);
		*/
		hash.put("map1", new MapDemo("zhudanshneg","100"));
		hash.put("map1", new MapDemo("zhudanshneg","300")); //重复则以前的键的数据将替换掉
		hash.put("map3", new MapDemo("zhudanshneg","400"));
		System.out.println(hash);
		
		System.out.println(tree.isEmpty());
	}
		
	
}

class MapDemo {
	private String name;
	private String width;
	
	public MapDemo(String name ,String width) {
		this.name=name;
		this.width=width;
	}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getWidth() {
		return width;
	}
	public void setWidth(String width) {
		this.width = width;
	}
	
	public String toString() {
		return this.name +"  "+this.width;
	}
	
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值