哈希表、有序表以及比较器的用法-左程云

哈希表、有序表和比较器的用法

哈希表的用法

主要数据结构:hashset与hashmap

  • hashset与hashmap原理一样,主要区别是有无伴随数据,hashset是纯Value结构,hashmap是key-value结构
  • 增删改查时间复杂度均为O(1)
  • 无序
  • 对于包装类Integer,String,Long等,hashset与hashmap均使用值做key
  • 对于引用类型,使用内存地址作为key
  • 补充:任何java中的类,都可以重写hashcode和equals方法完成指定键或者值做key的问题

主要api的使用:

HashSet<String> set = new HashSet<>();

        set.add(str1);

        System.out.println(set.contains("Hello"));

        System.out.println(set.contains(str2));

        set.add(str2);

        System.out.println(set.size());

        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"));

        System.out.println(map1.containsKey(str2));

        System.out.println(map1.get(str2));

        System.out.println(map1.get("你好") == null);

        map1.remove("Hello");

        System.out.println(map1.size());

        map1.clear();

        System.out.println(map1.isEmpty());
  • 使用数组代替哈希表,一般笔试中,未必需要申请哈希表
    • 前提:key值范围可控固定
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("在笔试场合中哈希表往往会被数组替代");

有序表的用法

  • TreeSet和TreeMap原理一样,有无伴随数据的区别
  • 增、删、改、查 + 很多和有序相关的操作(floor找地板、ceilling找天花板等),时间为O(log n) 因为维护了有序结构
  • 有序表比较相同的东西会去重,如果不想去重就加入更多的比较策略(比较器定制)。堆不会去重
  • 有序表在java里就是红黑树实现的
  • AVL树、SB树、替罪羊树、Treap、Splay、跳表等等很多结构都可实现同样功能

基本的treemap和treeset的使用

// 底层红黑树

        TreeMap<Integer, String> treeMap = new TreeMap<>();

        treeMap.put(5, "这是5");

        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));

        System.out.println(treeMap.containsKey(10));

        System.out.println(treeMap.get(4));

        treeMap.put(4, "张三是4");

        System.out.println(treeMap.get(4));

  

        treeMap.remove(4);

        System.out.println(treeMap.get(4) == null);

  

        System.out.println(treeMap.firstKey());

        System.out.println(treeMap.lastKey());

        // 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());

        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());

        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());

        while (!heap2.isEmpty()) {

            System.out.println(heap2.poll());

        }

比较器

  • 自定义比较器:记住一个lambda表达式(a,b)->a-b这样是升序,换成b-a是降序
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());

  

        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";

        System.out.println(str1.compareTo(str2));

        System.out.println(str2.compareTo(str1));

    }

  

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值