Map集合

Map集合

Map接口的特点:

用于存储任意键值对(Key- Value)

键:无序、无下标、不允许重复(唯一)

值:无序、无下标、允许重复

Map(Intertace)

1.HashMap(Class)

2.SortedMap(Intertace)   TreeMap(Class)

Map父接口

特点:存储一对数据(Key-Value), 无序、无下标,键不可重复,值可重复

方法:

V put(K key,V value)  //将对象存入到集合中,关联键值。key重复则覆盖原值

Object get (Object key)  //根据键获取对应的值

Set<K>  //返回所有key

Collection<V> values()  //返回包含所有值的Collection集合

Set<Map. Entry<K, V>>  //键值匹配的Set集合

eg : 

public class Demo01 {
public static void main(String[] args) {
//创建Map集合
Map<String,String> map = new HashMap<>();
//1.添加元素
map.put("cn","中国");
map.put("uk","英国");
map.put("usa","美国");
map.put("cn","zhongguo");
System.out.println("元素个数:"+map.size());
System.out.println(map.toString());
//2.删除
//map.remove("usa") ;
//System.out.println(map.toString() );
//3.遍历
//3.1使用keySet();
Set<String> keySet = map.keySet();
for (String key : map.keySet()) {
System.out.println(key+"---"+map.get(key));
}
//3.2使用entrySet();方法 其效率要高一些
//Set<Map.Entry<String,String>> entries = map.entrySet();
for (Map.Entry<String,String> entry : map.entrySet()) {
System.out.println(entry.getKey()+"--------"+entry.getValue());
}
//4.判断
System.out.println(map.containsKey("cn"));
System.out.println(map.containsKey("法国"));
System.out.println(map.isEmpty());//判断是否为空
}
}

Map集合的实现类

HashMap [ 重点]:

JDK1.2版本,线程不安全,运行效率快;允许用null作为key或是value

Hashtable:

DK1.0版本,线程安全,运行效率慢;不允许null作为key或是value

Properties

Hashtable的子类,要求key和value都是String。通常用于配置文件的读取

eg : 

public class Student implements Comparable<Student>{
private String name;
private int stoNo;
public Student() {
}
public Student(String name, int stoNo) {
this.name = name;
this.stoNo = stoNo;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getStoNo() {
return stoNo;
}
public void setStoNo(int stoNo) {
this.stoNo = stoNo;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", stoNo=" + stoNo +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return stoNo == student.stoNo &&
Objects.equals(name, student.name);
}
@Override
public int hashCode() {
return Objects.hash(name, stoNo);
}
@Override
public int compareTo(Student o) {
int n2 = this.stoNo-o.getStoNo();
return n2;
}
}
public class Demo02 {
public static void main(String[] args) {
//创建集合
HashMap<Student,String> students = new HashMap<Student,String>();
//1.添加元素
Student s1 = new Student("孙悟空",100);
Student s2 = new Student("猪八戒",80);
Student s3 = new Student("沙和尚",70);
students.put(s1,"北京");
students.put(s2,"上海");
students.put(s3,"杭州");
//students.put(s3,"南京");
students.put(new Student("沙和尚",70),"杭州");//内存地址不一样
System.out.println("元素个数:"+students.size());
System.out.println(students.toString());
//2.删除
//students.remove(s1);
//System.out.println(students.toString());
//3.遍历
//3.1使用keySet();
for (Student key : students.keySet()) {
System.out.println(key.toString()+"---"+students.get(key));
}
//3.2使用entrySet();方法
for (HashMap.Entry<Student,String> entry : students.entrySet()) {
System.out.println(entry.getKey()+"--"+entry.getValue());
}
//4.判断
System.out.println(students.containsKey(new Student("猪八戒",80)));
System.out.println(students.containsKey("上海"));
System.out.println(students.isEmpty());//判断是否为空
}
}
public class Demo03 {
public static void main(String[] args) {
//新建集合(定制比较)
TreeMap<Student,String> treeMap = new TreeMap<Student,String>();
//1.添加元素
Student s1 = new Student("孙悟空",100);
Student s2 = new Student("猪八戒",80);
Student s3 = new Student("沙和尚",70);
treeMap.put(s1,"北京");
treeMap.put(s2,"上海");
treeMap.put(s3,"杭州");
System.out.println("元素个数:"+treeMap.size());
System.out.println(treeMap.toString());
//2.删除
//treeMap.remove(s1);
//System.out.println(treeMap.toString());
//3.遍历
//3.1使用keySet();
for (Student key : treeMap.keySet()) {
System.out.println(key+"---"+treeMap.get(key));
}
//3.2使用entrySet();方法
for (Map.Entry<Student,String> entry : treeMap.entrySet()) {
System.out.println(entry.getKey()+"-----"+entry.getValue());
}
//4.判断
System.out.println(treeMap.containsKey(new Student("沙和尚",70)));
System.out.println(treeMap.isEmpty());//判断是否为空
}
}
posted @ 2021-03-15 15:29  星忄守候  阅读(50)  评论(0)    收藏  举报