1.集合的概念 | 2.Collection接口 | 3.List接口与实现类 |
---|---|---|
4.泛型和工具类 | 5.Set接口与实现类 | 6.Map接口与实现类 |
Map接口与实现类
一丶Map父接口
特点:存储一对数据(Key-Value),无序、无下标,键不可重复,值可重复。
方法:
V put(K key,V value) //将对象存入到集合中,关联键值。key重复则覆盖原值。
Object get(Object key) //根据键获得对应的值
Set //返回所有key
Collection values() //返回包含所有值得Collection集合。
Set<Map.Entry<K,V>> //键值匹配的Set集合
1.Map接口使用
特点(1)存储键值对(2)键不能重复(3)无序
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class Demo08 {
public static void main(String[] args) {
//创建Map集合
Map<String,String> map = new HashMap<>();
//1.添加元素
map.put("uk","英国");
map.put("cn","中国");
map.put("usa","美国");
System.out.println("元素个数:"+map.size());//元素个数:3
System.out.println(map.toString());//{usa=美国, uk=英国, cn=中国}
//2.删除元素
map.remove("usa");
System.out.println(map.toString());//{uk=英国, cn=中国}
//3.1使用keySet()遍历
Set<String> keyset=map.keySet();
for (String key:keyset) {
System.out.println(key+map.get(key));//uk英国/ncn中国/n
}
//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());//uk英国/ncn中国/n
}
//4.判断
System.out.println(map.containsKey("cn"));//true
System.out.println(map.containsValue("日本"));//false
}
}
二、Map集合的实现类
1.HashMap集合的使用
存储结构:哈希表(数组+链表+红黑树)
使用key可hashcode和equals作为重复
import java.util.Objects;
public class Student {
private String name;
private int no;
public Student() {
}
public Student(String name, int no) {
super();
this.name = name;
this.no = no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return no == student.no && Objects.equals(name, student.name);
}
@Override
public int hashCode() {
return Objects.hash(name, no);
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", no=" + no +
'}';
}
}
import java.util.HashMap;
import java.util.Map;
public class Demo09 {
public static void main(String[] args) {
//创建集合
HashMap<Student,String> students = new HashMap<Student,String>();
//1.添加元素
Student s1 = new Student("孙悟空",100);
Student s2 = new Student("猪八戒",99);
Student s3 = new Student("沙和尚",88);
students.put(s1,"北京");
students.put(s2,"上海");
students.put(s3,"广州");
students.put(new Student("沙和尚",88),"广州");
System.out.println("元素个数:"+students.size());
System.out.println(students.toString());
//2.删除
students.remove(s1);
System.out.println("删除之后:"+students.toString());
//3.使用KeySet()遍历
for (Student key: students.keySet()) {
System.out.println(key.toString()+"---"+students.get(key));
}
//3.2使用entrySet()
for(Map.Entry<Student,String>entry:students.entrySet()){
System.out.println(entry.getKey()+entry.getValue());
}
//4判断
System.out.println(students.containsKey(new Student("孙悟空",100)));
System.out.println(students.containsValue("广州"));
}
}
2.Hashtable
线程安全,运行效率慢;不允许null作为key或者value.
3.Properties
Hashtable的子类,要求key和value都是String。通常用于配置文件的读取.
4.TreeMap
实现了SortedMap接口(是Map的子接口),可以对Key自动排序。
public class Student implements Comparable<Student>{
@Override
public int compareTo(Student o) {
int n1 = this.no-o.getNo();
return n1;
}
import java.util.Map;
import java.util.TreeMap;
public class Demo10 {
public static void main(String[] args) {
//新建集合
TreeMap<Student,String> treeMap = new TreeMap<Student,String>();
//1.添加元素
Student s1 = new Student("孙悟空",100);
Student s2 = new Student("猪八戒",99);
Student s3 = new Student("沙和尚",88);
treeMap.put(s1,"北京");
treeMap.put(s2,"上海");
treeMap.put(s3,"广州");
System.out.println("元素个数:"+treeMap.size());
System.out.println(treeMap.toString());
//2.删除
treeMap.remove(new Student("猪八戒",99));
System.out.println(treeMap.size());
//3.1使用keySet遍历
for (Student key: treeMap.keySet()) {
System.out.println(key+treeMap.get(key));
}
//3.2
for (Map.Entry<Student,String> entry:treeMap.entrySet()){
System.out.println(entry.getKey()+entry.getValue());
}
//4.判断
System.out.println(treeMap.containsKey(new Student("沙和尚",88)));
}
}
三、Collections工具类
概念:集合工具类,定义了除了存取意外的集合常用方法。
方法
-public static void reverse(List<?> list) //反转集合中元素的顺序
-public static void shuffle(List<?> list) //随机重置集合元素的顺序
-public static void sort(List list) //升序排列(元素类型必须实现Comparable接口)
import java.util.*;
/**
* 演示Collections工具类的使用
*/
public class Demo11 {
public static void main(String[] args) {
List<Integer> list = new ArrayList<>();
list.add(20);
list.add(5);
list.add(12);
list.add(30);
list.add(6);
//sort排序
System.out.println("排序之前:"+list.toString());
Collections.sort(list);
System.out.println("排序之后:"+list.toString());
//binarySearch二分查找
int i = Collections.binarySearch(list,12);
System.out.println(i);
//copy复制
List<Integer> dest = new ArrayList<>();
for (int k =0;k< list.size();k++){
dest.add(0);
}
Collections.reverse(list);
System.out.println(dest.toString());
//reverse反转
Collections.reverse(list);
System.out.println("反转之后:"+list);
//shuffle 打乱
Collections.shuffle(list);
System.out.println("打乱之后:"+list);
//补充:list转成数组
Integer[] arr = list.toArray(new Integer[5]);
System.out.println(arr.length);
System.out.println(Arrays.toString(arr));
}
}
排序之前:[20, 5, 12, 30, 6]
排序之后:[5, 6, 12, 20, 30]
2
[0, 0, 0, 0, 0]
反转之后:[5, 6, 12, 20, 30]
打乱之后:[12, 6, 20, 5, 30]
5
[12, 6, 20, 5, 30]