设为首页 加入收藏

TOP

Java入门8(Comparator比较器,HashMap)(一)
2023-07-25 21:24:38 】 浏览:43
Tags:Java 入门 Comparator HashMap

comparator比较器

? Comparator不同于Comparable,使用更加的灵活,可以在不同场景下使用比较器,实际开发中,更推荐comparator比较器

// 新建一个学生类,作为栗子
public class Student {
    private int sno;
    private float height;
    private float weight;
}
// 单独定义一个比较器类StudentHeightComparator,实现了Comparator接口里面的compare方法
public class StudentHeightComparator implements Comparator<Student> {
    @Override
    public int compare(Student o1, Student o2) {
        return Float.compare(o1.getHeight(), o2.getHeight());
    }
}
// 使用comparator比较器
public static void main(String[] args) {
    List<Student> studentList = new ArrayList<>();
    Random random = new Random();
    for (int i = 0; i < 100; i++) {
        Student studentTmp = new Student(i,100 + random.nextInt(100),50 + random.nextInt(50));
        studentList.add(studentTmp);
    }
    // 创建比较器实例
    Comparator<Student> c = new StudentHeightComparator();
    // 把比较器实例当作参数传入sort方法
    Collections.sort(studentList,c);
    for (Student student : studentList) {
        System.out.println(student);
    }
}

Set接口常用实现类

三个常用实现类:TreeSet(基于Java红黑树,底层map),HashSet(哈希表,底层map),LinkedHashSet()

Set特点:

  1. 不允许存储重复元素
  2. 没有索引,不能通过普通for循环遍历
  3. 不保证有序:存储顺序有可能和读取顺序不一致

HashSet 和 TreeSet区别:TreeSet 支持比较器

// HashSet
public static void main(String[] args) {
    Set<Integer> set = new java.util.HashSet<>();
    for (int i = 0; i < 10; i++) {
        set.add(i);
    }
    // 无法存储重复元素
    set.add(1);
    // 无法保证存储和读取顺序一致
    // 遍历的时候,调用了set重写的toString方法
    System.out.println(set);
    // 使用迭代器遍历set集合
    Iterator<Integer> setIterator = set.iterator();
    while (setIterator.hasNext()){
        System.out.println(setIterator.next());
    }
}
// TreeSet
// TreeSet底层使用的红黑树,红黑树是有序二叉树的一种
// TreeSet支持在数据存储的时候提供比较器
public static void main(String[] args) {
    // 先创建比较器对象
    Comparator<Student> c = new StudentHeightComparator();
    // 创建TreeSet的同时提供比较器
    java.util.TreeSet<Student> treeSet = new java.util.TreeSet<>(c);
    // 插入数据
    Random random = new Random();
    for (int i = 0; i < 10; i++) {
        Student studentTmp = new Student(i,100 + random.nextInt(100),50 + random.nextInt(50));
        treeSet.add(studentTmp);
    }
    // 遍历
    for (Student student : treeSet) {
        System.out.println(student);
    }
}

Map的常见实现类

三个常用实现类:HashMap,TreeMap,HashTable

HashMap

? JDK中HashMap在1.7版本以及1.7版本之前使用的数组加链表的形式实现

  1. HashMap存储的所有的Map集合都是双列集合,由key和value组成
  2. Java底层使用一个Entry(键值对对象)的类负责存放key和value
  3. HashMap要求key是不可以重复的,如果你存储了两个key相同的键值对,新的value就会替换掉原有的value
public static void main(String[] args) {
    Map<String,String> map = new HashMap<>();
    // 插入键值对
    map.put("Entry01","robot01");
    map.put("Entry02","robot02");
    map.put("Entry01","robot03");
    System.out.println(map);
    // map里只有两个键值对:{Entry01=robot03, Entry02=robot02}

    // 可以通过key来获取(get)元素
    System.out.println(map.get("Entry02")); // robot02
    // size()返回键值对的数量
    System.out.println(map.size()); // 2

    // 是否有指定键,值
    System.out.println(map.containsKey("Entry01")); // true
    // 因为键相同,所以robot01被robot03所替代
    System.out.println(map.containsValue("robot01")); // false

    // 根据key去删除整个键值对
    map.remove("Entry01");
    System.out.println(map); // {Entry02=robot02}

    // keySet() 将当前集合的所有key以Set集合的方式返回
    map.put("Entry05","robot05");
    System.out.println(map.keySet()); // [Entry05, Entry02]

    // va
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇【深入浅出 Yarn 架构与实现】6-4.. 下一篇IDEA给【类】和【方法】设置作者..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目