Map 接口
1. 以键值对形式保存数据
key --- value
2. 键保持唯一
HashSet和HashMap之间的关系
底层都是使用哈希算法
HashSet底层依赖HashMap来实现
Map的添加方法
HashMap<String, Integer> map = new HashMap<>();
Integer i1 = map.put("张三", 19);
Integer i2 = map.put("李四", 13);
Integer i3 = map.put("王五", 12);
Integer i4 = map.put("赵六", 13);
Integer i5 = map.put("赵六", 33);
// 赵六的13 被 33 覆盖了
// put返回的是 被覆盖的值
// 没被覆盖就返回null
System.out.println(map);
System.out.println(i1);
System.out.println(i2);
System.out.println(i3);
System.out.println(i4);
System.out.println(i5);
HashMap<String, Integer> map = new HashMap<>();
map.put("张三", 19);
map.put("李四", 13);
map.put("王五", 12);
map.put("赵六", 13);
// 是否包含key
boolean key = map.containsKey("李四")
System.out.println(key)
boolean value = map.containsValue(13);
System.out.println(value)
// 获取所有key的set
Set keySet = map.keySet()
System.out.println(keySet)
// 获取所有value的集合
Collection values = map.values();
System.out.println(values);
Integer value = map.remove("张四");
System.out.println(value);
// 清空map
map.clear();
Student 类:
public class Student implements Comparable{
private String name;
private int age;
public Student() {
super();
}
public Student(String name, int age) {
super();
this.name = name;
this.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;
}
@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 String toString() {
return "[ 姓名 : " + name + " , 年龄 : " + age + " ]";
}
@Override
public int compareTo(Student o) {
int num = this.age - o.getAge();
return num == 0 ? 1 : num;
}
}
HashMap<Student, String> map = new HashMap<>();
map.put(new Student("张三", 13), "江苏");
map.put(new Student("李四", 18), "河南");
map.put(new Student("王五", 17), "安徽");
map.put(new Student("李四", 18), "河北");
System.out.println(map);
Map的遍历
keySet
迭代器遍历
HashMap<String, Integer> map = new HashMap<>();
map.put("张三", 19);
map.put("李四", 13);
map.put("王五", 12);
map.put("赵六", 13);
Set<String> keySet = map.keySet();
Iterator<String> iterator = keySet.iterator();
while (iterator.hasNext()) {
String key = iterator.next();
Integer value = map.get(key);
System.out.println(key + " = " + value);
}
增强for循环遍历
HashMap<String, Integer> map = new HashMap<>();
map.put("张三", 19);
map.put("李四", 13);
map.put("王五", 12);
map.put("赵六", 13);
Set<String> keySet = map.keySet();
for (String key : keySet) {
Integer value = map.get(key);
System.out.println(key + " = " + value);
}
entrySet
迭代器遍历
HashMap<String, Integer> map = new HashMap<>();
map.put("张三", 19);
map.put("李四", 13);
map.put("王五", 12);
map.put("赵六", 13);
Set<Map.Entry<String, Integer>> entrySet = map.entrySet();
Iterator<Map.Entry<String, Integer>> iterator = entrySet.iterator();
while (iterator.hasNext()) {
Entry<String, Integer> entry = iterator.next();
String key = entry.getKey();
Integer value = entry.getValue();
System.out.println(key + " = " + value);
}
快速遍历
HashMap<String, Integer> map = new HashMap<>();
map.put("张三", 19);
map.put("李四", 13);
map.put("王五", 12);
map.put("赵六", 13);
for (Entry<String, Integer> entry : map.entrySet()) {
System.out.println(entry.getKey() + " = " + entry.getValue());
}
LinkedHashMap
特点: 有序存入 有序取出
TreeMap<Student, String> map = new TreeMap<>();
map.put(new Student("张三", 19), "安徽");
map.put(new Student("李四", 16), "江苏");
map.put(new Student("王五", 17), "浙江");
map.put(new Student("赵六", 12), "湖南");
System.out.println(map);
{ 姓名: 赵六 年龄: 12 湖南, 姓名: 李四 年龄: 16 江苏, 姓名: 王五 年龄: 17 浙江, 姓名: 张三 年龄: 19 安徽}
ArrayList<Integer> list = new ArrayList<>();
list.add(14);
list.add(11);
list.add(12);
list.add(13);
Collections.sort(list);
System.out.println(list);
int binarySearch = Collections.binarySearch(list, 13);
System.out.println(binarySearch);
Collections.reverse(list);
System.out.println(list);
ArrayList list = new ArrayList<>();
list.add(new Student("张三", 11));
list.add(new Student("李四", 13));
list.add(new Student("王五", 12));
Collections.sort(list);
System.out.println(list);
[ 姓名: 张三 年龄: 11, 姓名: 王五 年龄: 12, 姓名: 李四 年龄: 13]
ArrayList 数组实现 线程不安全 查询快 增删慢
LinkedList 链表实现 线程不安全 查询慢 增删快
Vector 数组实现 线程安全 被ArrayList替代
HashSet 哈希算法 线程不安全 作用去重
LinkedHashSet 线程不安全 特点: 有序(怎么存怎么取)
TreeSet 线程不安全 作用:排序(两种方法)
HashMap 线程不安全 作用: 键位可以去重
LinkedHashMap 线程不安全 特点: 有序(怎么存怎么取)
TreeMap 线程安全 被HashMap取代 不能保存null值