Map:一次添加一对元素。Collection一次添加一个元素。
Map也称为双列集合,Collection集合称为单例集合。
其实map集合中存储的就是键值对。
map集合中必须保证键的唯一性。
常用方法:
1.添加
value put(key,value); 返回前一个和key关联的值,如果没有返回null
2.删除
void clear();情况map集合
value remove(key);根据指定的键删除这个键值对。
3.判断
boolean containsKey(key);
boolean containsValue(value);
boolean isEmpty();
4.获取
value get(key);通过键拿值,如果没有该键返回null.
当然可以通过返回null,来判断是否包含指定键。
int size();获取键值对的个数
Map集合的常用的方法
public class Map02_1 {
public static void main(String[] args) {
Map<Integer,String> map=new HashMap<Integer, String>();
method(map);
}
public static void method(Map<Integer,String> map){//学号和姓名
//添加元素
System.out.println(map.put(8, "wangcai"));//null
System.out.println(map.put(8, "xiaoqiang"));//wangcai,存相同的键,值会覆盖。
map.put(2, "zhangsan");
map.put(7, "zhouliu");
//删除
// System.out.println("remove:"+map.remove(2));
//判断
System.out.println("contains:"+map.containsKey(7));
//获取
System.out.println("get:"+map.get(8));
System.out.println(map.size());
System.out.println(map);
}
}
取出Map集合的2中方法
1.keySet
public class Map03_1 {
public static void main(String[] args) {
Map<Integer,String> map=new HashMap<Integer, String>();
method_2(map);
}
public static void method_2(Map<Integer,String> map) {
map.put(8,"wangwu");
map.put(2,"zhouliu");
map.put(7,"xiaoqiang");
map.put(6,"wangcai");
//取出map中所有的元素
//原理,通过keySet方法获取map中多有的键所在的Set集合,在通过Set的迭代器获取到每一个键
//在对每一个键通过map集合的get()方法获取其对应的值即可。
Set<Integer> setKey=map.keySet();
Iterator< Integer> it=setKey.iterator();
while(it.hasNext()){
Integer key=it.next();
System.out.println(key);
System.out.println(map.get(key));
}
}
}
2.entrySet
public class Map04_1 {
public static void main(String[] args) {
Map<Integer,String> map=new HashMap<Integer,String>();
method(map);
}
public static void method(Map<Integer, String> map) {
map.put(8,"wangwu");
map.put(2,"zhouliu");
map.put(7,"xiaoqiang");
map.put(6,"wangcai");
Collection<String> values=map.values();
Iterator<String> it1=values.iterator();
while(it1.hasNext()){
System.out.println(it1.next());
}
/*
* 通过Map转成set就可以迭代。
* 找到了另一个方法,entrySet。
* 该方法将键和值的映射关系作为对象存储到了Set集合中,而这个映射关系的类型就是Map.Entry类型(结婚证)
* */
Set<Map.Entry<Integer, String>> entrySet=map.entrySet();
// System.out.println(entrySet);
Iterator<Map.Entry<Integer, String>> it=entrySet.iterator();
while(it.hasNext()){
// System.out.println(it.next());
Map.Entry<Integer, String> me=it.next();
Integer i=me.getKey();
String s=me.getValue();
System.out.println(i+":::"+s);
}
}
}
Map常用的子类
|--HashTable:内部结构是哈希表,是同步的。不允许null作为键,null作为值。
|--Properties:用来存储键值对型的配置文件信息,可以和IO技术相结合。
|--HashMap:内部结构是哈希表,是不同步。允许null作为键,null作为值。
|--TreeMap:内部结构是二叉树,是不同步的,可以对map集合中的键进行排序。
HashMap存储自定义对象
public class Student {
private int age;
private String name;
public Student(){}
public Student(String name,int age){
this.age=age;
this.name=name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student other = (Student) obj;
if (age != other.age)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
//如果为true,视为相同元素,不存。
//如果为false,那么视为不同元素,就进行存储
}
public class Map07_1 {
public static void main(String[] args) {
/*
* 将学生对象和学生的归属地通过键与值存储到map集合中。
*
* */
HashMap<Student,String> hm=new HashMap<Student,String>();
hm.put(new Student("lisi",38), "北京");
hm.put(new Student("zhaoliu",24), "上海");
hm.put(new Student("xiaoqiang",31), "沈阳");
hm.put(new Student("wangcai",28), "大连");
hm.put(new Student("zhaoliu",24), "铁岭");
/* Set<Student> keySet=hm.keySet();
Iterator<Student> it=keySet.iterator();*/
Iterator<Student> it=hm.keySet().iterator();
while(it.hasNext()){
Student key=it.next();
String value=hm.get(key);
System.out.println(key.getName()+":"+key.getAge()+"----"+value);
}
}
}
TreeMap存储自定义对象
public class Student implements Comparable{
private int age;
private String name;
Student(){}
Student(String name,int age){
this.age=age;
this.name=name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student other = (Student) obj;
if (age != other.age)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
@Override
public int compareTo(Object o) {
int temp=0;
Student stu=(Student) o;
temp=this.age-stu.age;
return temp;
}
}
public class Map08_1 {
public static void main(String[] args) {
TreeMap<Student,String> tm=new TreeMap<Student,String>(new ComparatorByName());
tm.put(new Student("lisi",38), "北京");
tm.put(new Student("zhaolius",24), "上海");
tm.put(new Student("xiaoqiang",31), "沈阳");
tm.put(new Student("wangcai",28), "大连");
tm.put(new Student("zhaoliu",24), "铁岭");
Set<Map.Entry<Student,String>> entry=tm.entrySet();
Iterator<Map.Entry<Student, String>> it=entry.iterator();
while(it.hasNext()){
Map.Entry<Student, String> me= it.next();
Student student=me.getKey();
String value=me.getValue();
System.out.println(student.getName()+":"+student.getAge()+"---"+value);
}
}
}
//比较器
public class ComparatorByName implements Comparator<Student> {
@Override
public int compare(Student s1, Student s2) {
int temp=0;
temp=s1.getName().compareTo(s2.getName());
return temp==0?s1.getAge()-s2.getAge():temp;
}
}
LinkedHashMap
public class Map09_1 {
public static void main(String[] args) {
HashMap<Integer, String> hm=new LinkedHashMap<Integer, String>();
hm.put(7, "zhouqi");
hm.put(3,"zhangsan");
hm.put(1, "qiangyi");
hm.put(5, "wangwu");
Set<Map.Entry<Integer,String>> entry=hm.entrySet();
Iterator<Map.Entry<Integer, String>> it=entry.iterator();
while(it.hasNext()){
Map.Entry<Integer, String> me=it.next();
Integer key=me.getKey();
String value=me.getValue();
System.out.println(key+":"+value);
}
}
}