码迷,mamicode.com
首页 > 其他好文 > 详细

Map接口

时间:2019-08-16 20:39:36      阅读:80      评论:0      收藏:0      [点我收藏+]

标签:equal   public   通过   compareto   pre   包含   out   java   nta   

实现Map接口的类用来存储键—值 对。

Map接口的实现类有HashMap和TreeMap等。

Map类中存储的键—值通过键来标识,所以键值不能重复

Object put (Object key, Object value)//返回值是旧的value值, 如果传入的key已经存在的话,新的value会替换旧的value

Object get (Object key) ;//拿到相对应的value对象

Object remove (Object key) ;//移除相对应的value对象

boolean containsKey (Object key) ;//是否包含这个key

boolean containsValue(Object value)//是否包含这个值

int size ();//一共多少对对象

boolean isEmpty();是不是空的

void putAll(Map t);把另外一个Map全部的东西都加进来

void clear();//清除

例子如下:

import java.util.List;
import java.util.LinkedList;
import java.util.Collections;
import java.util.*;
public class Test {
	public static void main(String[] args) {
		Map m1 = new HashMap();Map m2 = new TreeMap();
		m1.put("one",new Integer(1));
		m1.put("two",new Integer(2));
		m1.put("three",new Integer(3));
		m2.put("A",new Integer(1));
		m2.put("B",new Integer(2));
		System.out.println(m1.size());
		System.out.println(m1.containsKey("one"));
		System.out.println(m2.containsValue(new Integer(1)));
		if(m1.containsKey("two")) {
			int i = ((Integer)m1.get("two")).intValue();
			System.out.println(i);
		}
		Map m3 = new HashMap(m1);
		m3.putAll(m2);
		System.out.println(m3);
		
		
		
		
		
		
		
		
		
		
		
		
		/*List l1 = new LinkedList();
		l1.add(new Name("Karl","M"));
		l1.add(new Name("Steven","Lee"));
		l1.add(new Name("John","O"));
		l1.add(new Name("Tom","M"));
		System.out.println(l1);
		Collections.sort(l1);
		System.out.println(l1);*/
		
		
		
		
		
		
		
		
		
		/*
		Set s = new HashSet();
		s.add("hello");
		s.add("world");
		s.add(new Name("f1","11"));
		s.add(new Integer(100));
		*/
		
		/*
		
		s.add("hello");
		s.add("hello");
		*/
		//Set 
		/*
		Set s1 = new HashSet();
		Set s2 = new HashSet();
		s1.add("a");s1.add("b");s1.add("c");
		s2.add("d");s2.add("a");s2.add("b");
		Set sn = new HashSet(s1);
		sn.retainAll(s2);
		Set su = new HashSet(s1);
		su.addAll(s2);
		
		System.out.println(sn);
		
		System.out.println(su);
		*/
		
		
		
		
		
		
		
		
		/*
		Collection c = new HashSet();
		c.add("hello");
		c.add(new Name("f1","11"));
		c.add(new Name("f2","12"));
		c.add(new Name("f3","13"));
		c.add(new Integer(100));
		c.remove("hello");
		c.remove(new Integer(100));
		
		Iterator i = c.iterator();
		while(i.hasNext()) {
			Name n = (Name)i.next();
			System.out.print(n.getfirstName()+" ");
		}*/
		/*System.out.println(c.remove(new Name("f1","11")));
		System.out.println(c);*/
	}
}
class Name implements Comparable {
	private String firstName,secondName;
	public Name(String firstName,String secondName) {
		this.firstName = firstName;
		this.secondName = secondName;
	}
	public String getfirstName() {return firstName;}
	public String getsecondName() {return secondName;}
	public String toString() {
		return firstName+" "+secondName;
	}
	
	public boolean equals(Object obj) {
		if(obj instanceof Name) {
			Name name = (Name) obj;
			return (firstName.equals(name.firstName))&&(secondName.equals(name.secondName));
		}
		return super.equals(obj);
	}
	
	public int hashCode() {
		return firstName.hashCode();
	}
	
	public int compareTo(Object o) {
		Name n = (Name) o;
		int lastCmp = 
				secondName.compareTo(n.secondName);
		return
					(lastCmp!=0 ? lastCmp:firstName.compareTo(n.firstName));
	}
}

 

Map接口

标签:equal   public   通过   compareto   pre   包含   out   java   nta   

原文地址:https://www.cnblogs.com/lsswudi/p/11366011.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!