今日内容
1. Map
面试题
Collection集合和Map集合有什么区别?
分析 Collection 单列集合,只能存储一种引用类型 遍历方式:增强for输出所有内容,比较器:Iterator 应用范围广 List ArrayList--比较器:ListIterator--添加了修改功能 Vertor--比较器:Enumeration LinkedList Set HashSet---依赖于HashMap TreeSet---依赖于TreeMap Map 双列集合,可以存储键值对 遍历方式:通用方法--获取所有键,找到对应的值 应用范围 HashMap---存储和获取 TreeMap---存储和获取(按照排序规则排序) Map--称为实体--Map<Integer Protuct>
Map---集合 针对键有效,键必须唯一 Map<K,V>双列集合--可以存储键值对 基本功能 添加 V put(K key , V value) ---添加键值对元素,并且有返回值 ---Map针对键有效,键如果是唯一的,返回值是Null(因为前面没有键值!) ---如果键值重复,后面的键值会覆盖前面的键值,返回值是前面的键值! 删除 V remove(Object K) ---删除键,返回对应的键值 V clear() ---将Map清空 判断 boolean containsKey(Object Key) ---判断集合中是否包含指定的键,包含返回true,否则返回false boolean containsValue(Object Value) ---判断集合中是否包含指定的值,包含返回true,否则返回false
import java.util.HashMap;
import java.util.Map;
//Map基本功能测试
public class MapTest {
public static void main(String[] args) {
//创建集合---键值对
Map<String,String> map = new HashMap();
//添加数据--观察返回值
String value = map.put("1","我是一号!");
System.out.println(value);//null--因为这是第一个,前面没有元素
String value1 = map.put("1","我也是一号!");
System.out.println(value1);//我是一号!--如果键覆盖则返回的是上一个键的键值
//添加数据
map.put("1","我才是一号!");
map.put("2","我是二号!");
map.put("3","我是三号!");
map.put("4","我是四号!");
System.out.println(map);//{1=我才是一号!, 2=我是二号!, 3=我是三号!, 4=我是四号!}
//删除数据
map.remove("3");//删除了三号
System.out.println(map);//{1=我才是一号!, 2=我是二号!, 4=我是四号!}
//判断
boolean flag = map.containsKey("2");
System.out.println(flag);//true
boolean flag1 = map.containsValue("我是三号1");
System.out.println(flag1);//false
//清空
map.clear();
System.out.println(map);//{}--被清空!
}
}
Map集合的遍历 1)将所有的键放到一个集合中,遍历的时候直接遍历键,获取值---遍历丈夫找妻子 Set<E> 对象名 = 集合对象名.keySet(); ---获取键 2)将所有的键值对放到一个集合中,通过整体调用局部---遍历所有结婚证,找到谁用谁 Set<Map.Entry<E,E>> 对象名 = 集合对象名.entrySet(); ---获取键值对
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
//Map集合第一种遍历方式
public class MapTest {
public static void main(String[] args) {
//方式一,将所有键放到一个集合中,通过遍历键,遍历值--推荐使用!
//创建集合
Map<String,String> map = new HashMap<>();
//创建数据并录入
map.put("1","1号");
map.put("2","2号");
map.put("3","3号");
map.put("4","4号");
map.put("5","5号");
map.put("6","6号");
//遍历
//将所有键放到一个集合中--自然按照键排序--用Set!
Set<String> set = map.keySet();
//遍历键集合
for (String key: set){
String value = map.get(key);//将获取的键值赋给value
System.out.print(value+" ");//1号 2号 3号 4号 5号 6号
}
}
}
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
//Map集合第二种方式
public class MapTest1 {
public static void main(String[] args) {
//创建集合
Map<String,String> map = new HashMap<>();
//创建数据并录入
map.put("1","1号");
map.put("2","2号");
map.put("3","3号");
map.put("4","4号");
map.put("5","5号");
//将键值对存入一个集合
Set<Map.Entry<String, String>> set = map.entrySet();
for(Map.Entry<String,String> m: set){
String key = m.getKey();//分别获取键
String value = m.getValue();//获取键值
System.out.println(key+"---"+value);
}
}
}
2. HashMap
HashMap---集合 Map实现类---依赖于equals() and HashCode()--保证键唯一! put()--录入
3. TreeMap
TreeMap 针对键有效,排序 构造方法 无参构造方法 public TreeMap()--自然排序--类比TreeSet 有参构造方法 public TreeMap(Comparator<? super E>comparator)--比较器排序--类比TreeSet 如果键是自定义类型,键必须唯一,需要有排序规则!
//无参构造
import java.util.Objects;
public class Student implements Comparable<Student> {
private String name ;
private int age ;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return age == student.age && name.equals(student.name);
}
@Override
public int hashCode() {
return Objects.hash(name, age);
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", 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;
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public Student() {
}
@Override
public int compareTo(Student s) {
//加入排序条件--以年龄为主要排序条件
int num = this.age - s.age ;
//姓名为副条件
int num1 = num==0?(this.name.compareTo(s.name)):num ;
return num1;
}
}
import java.util.Set;
import java.util.TreeMap;
//无参构造--自然排序!--类比TreeSet
public class TreeMapTest {
public static void main(String[] args) {
TreeMap<Student,String> tm = new TreeMap<>() ;//无参构造--自然排序,数据要实现Comparable接口
//创建数据
Student student = new Student("钟离",500);
Student student1 = new Student("胡桃",18);
Student student2 = new Student("钟",50);
Student student3 = new Student("离",20);
Student student4 = new Student("钟离",100);
//录入数据
tm.put(student,"退休");
tm.put(student1,"在职");
tm.put(student2,"退休");
tm.put(student3,"退休");
tm.put(student4,"退休");
//将所有键放入一个集合
Set<Student> set = tm.keySet();
//遍历输出
for (Student s: set){
System.out.println(s.getName()+"---"+s.getAge()+"---"+tm.get(s));
/*
胡桃---18---在职
离---20---退休
钟---50---退休
钟离---100---退休
钟离---500---退休
*/
}
}
}
import java.util.Objects;
public class Student{
private String name ;
private int age ;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return age == student.age && name.equals(student.name);
}
@Override
public int hashCode() {
return Objects.hash(name, age);
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", 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;
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public Student() {
}
}
import java.util.Comparator;
import java.util.Set;
import java.util.TreeMap;
//有参构造---匿名内部类!
public class TreeMapTest {
public static void main(String[] args) {
//创建集合
TreeMap<Student,String> tm = new TreeMap<>(new Comparator<Student>() {
@Override
public int compare(Student s1, Student s2) {
//加入排序条件--以年龄为主要排序条件
int num = s1.getAge() - s2.getAge() ;
//姓名为副条件
int num1 = num==0?(s1.getName().compareTo(s2.getName())):num ;
return num1;
}
});
//创建并获取数据
Student s = new Student("钟离",500);
Student s1 = new Student("钟",10);
Student s2 = new Student("钟离",50);
Student s3 = new Student("离",85);
//录入数据
tm.put(s,"退休");
tm.put(s1,"退休");
tm.put(s2,"退休");
tm.put(s3,"退休");
//遍历
Set<Student> set = tm.keySet();
for (Student student: set){
System.out.println(student.getName()+"--"+student.getAge());
}
/*
钟--10
钟离--50
离--85
钟离--500
*/
}
}
4. Collections
Collection---针对集合操作工具类 public static <T> int binarySearch(List<? extends Comparable<? super T>> list,T key) public static <T> T max(Collection<? extends T> coll) ---针对Collection集合获取最大值(自然顺序比较获取最大值) public static <T> T min(Collection<? extends T> coll) ---针对Collection集合获取最小值(自然顺序比较获取最小值) public static void reverse(List<?> list) ---将List集合的元素反转 public static void shuffle(List<?> list) ---针对List集合随机置换 public static <T extends Comparable<? super T>> void sort(List<T> list) ---针对List集合按照自然顺序排序 public static <T extends Comparable<? super T>> void sort(List<T> list,Comparator<T> comparator) ---针对List集合按照比较器进行排序