Java基础——Map接口

Java基础——Map接口,第1张

Java基础——Map接口 Map接口是和Collection同等级的根接口,它表示一个键值对(key-value)的映射,每一个key对应一个value,查找Map中的数据,总是根据key来获取,所以key是不可重复的。 1.Map接口

Map集合的特点:

1)Map集合是双边队列的,通过键值对(key-value)来进行存储数据

2)key值不能重复,value值可以重复

Map集合下面常用的方法:

增加:put(K key, V value);存放的是一个键值对的数据

putAll(Map k, Map v);将一个map集合存放到另一个map集合

删除:remove(Object k);通过键删除整个键值对,并返回被删除的值

修改:put(K key, V value); 当key存在的时候,就修改。当key不存在时,就添加

查询:boolean isEmpty();是否为空,map集合为空就返回true,不为空就返回false

boolean containsKey();是否包含这个键

boolean containsValue();是否包含这个值

V get(Object k);通过键获取值

Collection values();获取map集合中所有的value值,返回的是Collection集合

Set>entrySet()`;将键值对实体成一个Set集合

示例

public class Test {
public static void main(String[] args) {
HashMap map = new HashMap();
map.put("01", "哈哈");
map.put("02", "嘻嘻");
map.put("03", "嘿嘿");
map.put("03", "呵呵");
System.out.println(map);
HashMap map1 = new HashMap();
map1.put("07", "呼呼");
map1.put("08", "哦哦");
map1.put("01", "哈哈哈");
map.putAll(map1);
System.out.println(map);
System.out.println(map.remove("07"));
System.out.println(map);
map.put("01", "哈哈");
System.out.println(map);
System.out.println(map.isEmpty());
System.out.println(map.size());
System.out.println(map.containsKey("08"));
System.out.println(map.containsValue("嘿嘿嘿"));

Set set = map.keySet();
System.out.println(set);
System.out.println(map.get("01"));
Collection collection = map.values();
System.out.println(collection);

Set> set1 = map.entrySet();
System.out.println(set1);
for (Entry entry : set1) {
System.out.println(entry.getKey());
System.out.println(entry.getValue());
        }
    }
}

2.Map的子类

1)HashMap 底层是哈希表数据结构,基于hash值进行存储的,依靠key,要保证键的唯一性,需要覆盖hashCode方法,和equals方法。

示例

class Person {
	private String name;
	private int age;
	Person() {
 
	}
	public Person(String name, int age) {
 
		this.name = name;
		this.age = age;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	@Override
	public int hashCode() {
		return this.name.hashCode() + age * 37;
	}
	@Override
	public boolean equals(Object obj) {
		if (obj instanceof Person) {
			Person p = (Person) obj;
			return this.name.equals(p.name) && this.age == p.age;
		} else {
			return false;
		}
	}
	@Override
	public String toString() {
		return "Person@name:" + this.name + " age:" + this.age;
	}
}
public class Test {
	public static void main(String[] args) {
		HashMap hm = new HashMap();
		hm.put(new Person("jack", 20), "1001");
		hm.put(new Person("rose", 18), "1002");
		hm.put(new Person("lucy", 19), "1003");
		hm.put(new Person("hmm", 17), "1004");
		hm.put(new Person("ll", 25), "1005");
		System.out.println(hm);
		System.out.println(hm.put(new Person("rose", 18), "1006"));
 
		Set> entrySet = hm.entrySet();
		Iterator> it = entrySet.iterator();
		while (it.hasNext()) {
			Entry next = it.next();
			Person key = next.getKey();
			String value = next.getValue();
			System.out.println(key + " = " + value);
		    }
	    }
    }
}

2)TreeMap 基于二叉树进行存储的,存储的形式也是key,实现了SortedMap接口会对key值进行排序。

示例

class Student implements Comparable{
private String name;
private int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + "]";
}
@Override
public int compareTo(Student o) {
int n1 = this.age - o.age;
int n2 = this.name.compareTo(o.name);
       if (n1 == 0) {
           return n2;
      } else {
           return n1;
      }
    }
}

public class Test {
public static void main(String[] args) {
Student stu1 = new Student("张三", 12);
Student stu3 = new Student("李四", 20);
Student stu2 = new Student("王五", 23);
Student stu4 = new Student("马六", 13);
Student stu5 = new Student("赵二", 13);

TreeMap treeMap = new TreeMap();
treeMap.put(stu1, "踏实");
treeMap.put(stu2, "勤奋");
treeMap.put(stu3, "刻苦");
treeMap.put(stu4, "愚钝");
treeMap.put(stu5, "懒散");
System.out.println(treeMap);
}

}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5722185.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-18
下一篇 2022-12-18

发表评论

登录后才能评论

评论列表(0条)

保存