---------------------- <a href="http://www.itheima.com"target="blank">ASP.Net+Unity开发</a>、<a href="http://www.itheima.com"target="blank">.Net培训</a>、期待与您交流! ----------------------
Map与Collection在集合框架中属并列存在
Map存储的是键值对
Map存储元素使用put方法,Collection使用add方法
Map集合没有直接取出元素的方法,而是先转成Set集合,在通过迭代获取元素
Map集合中键要保证唯一性
Map集合
该集合存储键值对。一对一对往里存。而且要保证键的唯一性。
1,添加。
put(K key, V value)
putAll(Map<? extends K,? extends V> m)
2,删除。
clear()
remove(Object key)
3,判断。
containsValue(Object value)
containsKey(Object key)
isEmpty()
4,获取。
get(Object key)
size()
values()
entrySet()
keySet()
Map
|--Hashtable:底层是哈希表数据结构,不可以存入null键null值。该集合是线程同步的。jdk1.0.效率低。
|--HashMap:底层是哈希表数据结构,允许使用 null 值和 null 键,该集合是不同步的。将hashtable替代,jdk1.2.效率高。
|--TreeMap:底层是二叉树数据结构。线程不同步。可以用于给map集合中的键进行排序。
和Set很像。
其实大家,Set底层就是使用了Map集合。
添加元素,如果出现添加时,相同的键。那么后添加的值会覆盖原有键对应值。
map集合的两种取出方式:
1,Set<k> keySet:将map中所有的键存入到Set集合。因为set具备迭代器。
所有可以迭代方式取出所有的键,在根据get方法。获取每一个键对应的值。
Map集合的取出原理:将map集合转成set集合。在通过迭代器取出。
2,Set<Map.Entry<k,v>> entrySet:
将map集合中的映射关系存入到了set集合中,
而这个关系的数据类型就是:Map.Entry
Entry其实就是Map中的一个static内部接口。
为什么要定义在内部呢?
因为只有有了Map集合,有了键值对,才会有键值的映射关系。
关系属于Map集合中的一个内部事物。
而且该事物在直接访问Map集合中的元素。
什么使用map集合呢?
当数据之间存在这映射关系时,就要先想map集合。
练习
/*
每一个学生都有对应的归属地
学生student,地址String
学生属性:姓名,年龄
注意:姓名和年龄相同的视为同一个学生
保证学生的唯一性
*/
import java.util.*;
class HashMapTest
{
public static void main(String[] args)
{
HashMap<Student,String> mp = new HashMap<Student,String>();
mp.put(new Student("lisi01",11),"beijing");
mp.put(new Student("lisi04",14),"shanghai");
mp.put(new Student("lisi05",15),"nanjin");
mp.put(new Student("lisi02",12),"wujian");
mp.put(new Student("lisi03",13),"guangdong");
mp.put(new Student("lisi01",11),"0000");
//第一种取出方式:keySet()
Set<Student> keySet = mp.keySet();//将取出来的键存储到Set集合中
Iterator<Student> it = keySet.iterator();
while (it.hasNext())
{
Student stu = it.next();
String addr = mp.get(stu);
System.out.println(stu+"::"+addr);
}
//第二种方式:entrySet()
Set<Map.Entry<Student,String>> entrySet = mp.entrySet();
Iterator<Map.Entry<Student,String>> iter = entrySet.iterator();
while (iter.hasNext())
{
Map.Entry<Student,String> me = iter.next();//迭代出来的都是一对一对出现的
Student sutd =me.getKey();
String addre = me.getValue();
System.out.println(sutd+"::"+addre);
}
}
}
class Student implements Comparable<Student>
{
//将元素私有化
private String name;
private int age;
//初始化
Student(String name,int age)
{
this.name=name;
this.age=age;
}
//
public int compareTo(Student s)
{
int num = new Integer(this.age).compareTo(new Integer(s.age));
if (num==0)
return this.name.compareTo(s.name);
return num;
}
//提供外部获取,修改方法
public void setName(String name)
{
this.name=name;
}
public String getName()
{
return name;
}
public void setAge(int age)
{
this.age=age;
}
public int getAge()
{
return age;
}
//为了保持元素的唯一性,复写hashCodeTo和equals两个方法
public int hashCode()
{
return name.hashCode()+age*39;
}
public boolean equals(Object obj)
{
if(!(obj instanceof Student))
throw new ClassCastException("类转换异常");
Student s = (Student)obj;
return this.name.equals(s.name) && this.age==s.age;
}
//复写自己的toString
public String toString()
{
return name+"::"+age;
}
}
---------------------- <a href="http://www.itheima.com"target="blank">ASP.Net+Unity开发</a>、<a href="http://www.itheima.com"target="blank">.Net培训</a>、期待与您交流! ----------------------