hashset和hashmap
mport java.util.HashMap;
import java.util.HashSet;
public class Code01_HashSetAndHashMap {
public static void main(String[] args) {
// Integer、Long、Double、Float
// Byte、Short、Character、Boolean
// String等都有这个特征
String str1 = new String("Hello");
String str2 = new String("Hello");
// false,因为不同的内存地址
System.out.println(str1 == str2);
// true,因为它们的值是相同的
System.out.println(str1.equals(str2));
HashSet<String> set = new HashSet<>();
set.add(str1); //时间复杂度O(1)
System.out.println(set.contains("Hello"));
System.out.println(set.contains(str2)); //true,在哈希表中,根据值来
set.add(str2);
System.out.println(set.size()); // 1,就一份hello,会去重
set.remove(str1);
set.clear();
System.out.println(set.isEmpty());
System.out.println("===========");
HashMap<String, String> map1 = new HashMap<>();
map1.put(str1, "World");
System.out.println(map1.containsKey("Hello")); //true
System.out.println(map1.containsKey(str2)); //true
System.out.println(map1.get(str2)); //world
System.out.println(map1.get("你好") == null); //true
map1.remove("Hello");
System.out.println(map1.size()); //0
map1.clear();
System.out.println(map1.isEmpty()); //true
System.out.println("===========");
// 一般在笔试中,未必需要申请哈希表
HashMap<Integer, Integer> map2 = new HashMap<>();
map2.put(56, 7285);
map2.put(34, 3671263);
map2.put(17, 716311);
map2.put(24, 1263161);
// 上面的map2行为,可以被如下数组的行为替代
int[] arr = new int[100];
arr[56] = 7285;
arr[34] = 3671263;
arr[17] = 716311;
arr[24] = 1263161;
// 哈希表的增、删、改、查,都可以被数组替代,前提是key的范围是固定的、可控的
System.out.println("在笔试场合中哈希表往往会被数组替代");
System.out.println("===========");
Student s1 = new Student(17, "张三");
Student s2 = new Student(17, "张三");
HashMap<Student, String> map3 = new HashMap<>();
map3.put(s1, "这是张三");
System.out.println(map3.containsKey(s1)); //true
System.out.println(map3.containsKey(s2)); //false 根据内存地址来,因为Student类没有重写hashCode和equals方法,会根据内存地址来作为key
map3.put(s2, "这是另一个张三");
System.out.println(map3.size()); //2
System.out.println(map3.get(s1)); //这是张三
System.out.println(map3.get(s2)); //这是另一个张三
}
public static class Student {
public int age;
public String name;
public Student(int a, String b) {
age = a;
name = b;
}
}
}
treeset,treemap和PriorityQueue
import java.util.PriorityQueue;
import java.util.TreeMap;
import java.util.TreeSet;
public class Code02_TreeSetAndTreeMap {
public static void main(String[] args) {
// TreeMap的底层是红黑树
TreeMap<Integer, String> treeMap = new TreeMap<>();
treeMap.put(5, "这是5"); //时间复杂度是O(logN)
treeMap.put(7, "这是7");
treeMap.put(1, "这是1");
treeMap.put(2, "这是2");
treeMap.put(3, "这是3");
treeMap.put(4, "这是4");
treeMap.put(8, "这是8");
System.out.println(treeMap.containsKey(1)); //true
System.out.println(treeMap.containsKey(10)); //false
System.out.println(treeMap.get(4)); //这是4
treeMap.put(4, "张三是4");
System.out.println(treeMap.get(4)); //张三是4
treeMap.remove(4);
System.out.println(treeMap.get(4) == null); //true
System.out.println(treeMap.firstKey()); //1
System.out.println(treeMap.lastKey()); //8
// TreeMap中,所有的key,<= 4且最近的key是什么
System.out.println(treeMap.floorKey(4));
// TreeMap中,所有的key,>= 4且最近的key是什么
System.out.println(treeMap.ceilingKey(4));
System.out.println("========");
TreeSet<Integer> set = new TreeSet<>();
set.add(3);
set.add(3);
set.add(4);
set.add(4);
System.out.println("有序表大小 : " + set.size()); //2 会去重
while (!set.isEmpty()) {
System.out.println(set.pollFirst()); //从小到大弹出
// System.out.println(set.pollLast());
}
// 堆,默认小根堆、如果要大根堆,定制比较器!
PriorityQueue<Integer> heap1 = new PriorityQueue<>();
heap1.add(3);
heap1.add(3);
heap1.add(4);
heap1.add(4);
System.out.println("堆大小 : " + heap1.size()); //4,优先级队列不回去重
while (!heap1.isEmpty()) {
System.out.println(heap1.poll()); //从小到大弹出
}
// 定制的大根堆,用比较器!
PriorityQueue<Integer> heap2 = new PriorityQueue<>((a, b) -> b - a);
heap2.add(3);
heap2.add(3);
heap2.add(4);
heap2.add(4);
System.out.println("堆大小 : " + heap2.size()); //4
while (!heap2.isEmpty()) {
System.out.println(heap2.poll()); //从大到小弹出
}
}
}
定制比较器
import java.util.Arrays;
import java.util.Comparator;
import java.util.TreeSet;
public class Code03_Comparator {
public static class Employee {
public int company;
public int age;
public Employee(int c, int a) {
company = c;
age = a;
}
}
public static class EmployeeComparator implements Comparator<Employee> {
@Override
public int compare(Employee o1, Employee o2) {
// 任何比较器都默认
// 如果返回负数认为o1的优先级更高
// 如果返回正数认为o2的优先级更高
// 任何比较器都是这样,所以利用这个设定,可以定制优先级怎么确定,也就是怎么比较
// 不再有大小的概念,就是优先级的概念
return o1.age - o2.age; //谁年龄小谁优先级高
}
}
public static void main(String[] args) {
Employee s1 = new Employee(2, 27);
Employee s2 = new Employee(1, 60);
Employee s3 = new Employee(4, 19);
Employee s4 = new Employee(3, 23);
Employee s5 = new Employee(1, 35);
Employee s6 = new Employee(3, 55);
Employee[] arr = { s1, s2, s3, s4, s5, s6 };
Arrays.sort(arr, new EmployeeComparator());
for (Employee e : arr) {
System.out.println(e.company + " , " + e.age);
}
System.out.println("=====");
Arrays.sort(arr, (a, b) -> b.age - a.age);
for (Employee e : arr) {
System.out.println(e.company + " , " + e.age);
}
System.out.println("=====");
// 所有员工,先按照谁的公司编号小,谁在前;如果公司编号一样,谁年龄小谁在前
Arrays.sort(arr, (a, b) -> a.company != b.company ? (a.company - b.company) : (a.age - b.age));
for (Employee e : arr) {
System.out.println(e.company + " , " + e.age);
}
TreeSet<Employee> treeSet1 = new TreeSet<>(new EmployeeComparator()); //不加比较器,会出错,他不知道怎么比大小
for (Employee e : arr) {
treeSet1.add(e);
}
System.out.println(treeSet1.size());
// 会去重
treeSet1.add(new Employee(2, 27));
System.out.println(treeSet1.size()); //还是6,因为发现重复的key
System.out.println("===");
// 如果不想去重,就需要增加更多的比较
// 比如对象的内存地址、或者如果对象有数组下标之类的独特信息
TreeSet<Employee> treeSet2 = new TreeSet<>((a, b) -> a.company != b.company ? (a.company - b.company)
: a.age != b.age ? (a.age - b.age) : a.toString().compareTo(b.toString())); //公司不一样,直接比出大小,要是公司一样,年龄不一样,直接比出大小,如果公司年龄都一样,根据地址比大小,这样就不会去重了
for (Employee e : arr) {
treeSet2.add(e);
}
System.out.println(treeSet2.size());
// 不会去重
treeSet2.add(new Employee(2, 27));
System.out.println(treeSet2.size());
System.out.println("===");
// PriorityQueue不会去重,不再展示了
// 字典序(字符串是怎么比大小的)
String str1 = "abcde";
String str2 = "ks";
//str1 < str2,因为a<k
System.out.println(str1.compareTo(str2));
System.out.println(str2.compareTo(str1));
}
}