• HashSet与TreeSet


    1、TreeSet 是二差树实现的,Treeset中的数据是自动排好序的,不允许放入null值

    2、HashSet 是哈希表实现的,HashSet中的数据是无序的,可以放入null,但只能放入一个null,两者中的值都不能重复,就如数据库中唯一约束

     

    一、HashSet

    1.HashSet存字符串

    import java.util.*;
    class Student {

    String id;

    String name;

    public Student(String id,String name) { // 创建构造方法

    this.id=id;

    this.name = name;

    }

    public String toString() { // 重写toString()方法

    return id+":"+name;

    }

    }

    public class Example10 {

    public static void main(String[] args) {

    HashSet hs = new HashSet(); // 创建HashSet集合

    Student stu1 = new Student("1", "Jack"); // 创建Student对象

    Student stu2 = new Student("2", "Rose");

    Student stu3 = new Student("2", "Rose");

    hs.add(stu1);

    hs.add(stu2);

    hs.add(stu3);

    System.out.println(hs);

    }

    }

    2.HashSet存对象

    import java.util.*;

    class Student {

    private String id;

    private String name;

    public Student(String id, String name) {

    this.id = id;

    this.name = name;

    }

    // 重写toString()方法

    public String toString() {

    return id + ":" + name;

    }

    // 重写hashCode方法

    public int hashCode() {

    return id.hashCode(); // 返回id属性的哈希值

    }

    // 重写equals方法

    public boolean equals(Object obj) {

    if (this == obj) { // 判断是否是同一个对象

    return true; // 如果是,直接返回true

    }

    if (!(obj instanceof Student)) { // 判断对象是为Student类型

    return false; // 如果对象不是Student类型,返回false

    }

    Student stu = (Student) obj; // 将对象强转为Student类型

    boolean b = this.id.equals(stu.id); // 判断id值是否相同

    return b; // 返回判断结果

    }

    }

    public class Example11 {

    public static void main(String[] args) {

    HashSet hs = new HashSet(); // 创建HashSet对象

    Student stu1 = new Student("1", "Jack"); // 创建Student对象

    Student stu2 = new Student("2", "Rose");

    Student stu3 = new Student("2", "Rose");

    hs.add(stu1); // 向集合存入对象

    hs.add(stu2);

    hs.add(stu3);

    System.out.println(hs); // 打印集合中的元素

    }

    }

     二、TreeSet

    import java.util.*;
    class MyComparator implements Comparator { // 定义比较器实现Comparator接口
    public int compare(Object obj1, Object obj2) { // 实现比较方法
    String s1 = (String) obj1;
    String s2 = (String) obj2;
    int temp = s1.length() - s2.length();
    return temp;
    }
    }
    public class Example14 {
    public static void main(String[] args) {
    TreeSet ts = new TreeSet(new MyComparator());// 创建TreeSet对象时传入自定义比较器
    ts.add("Jack");// 向该Set对象中添加字符串
    ts.add("Helena");
    ts.add("Eve");
    Iterator it = ts.iterator(); // 获取Iterator对象
    // 通过while循环,逐渐将集合中的元素打印出来
    while (it.hasNext()) {
    // 如果Iterator有元素进行迭代,则获取元素并进行打印
    Object obj = it.next();
    System.out.println(obj);
    }
    }
    }

     

     

     

     

  • 相关阅读:
    高精度
    eps取值
    QtCreator常用快捷键
    C++11 麻辣烫
    谈一些关于华为鸿蒙的看法
    Termux新手基础+进阶学习笔记(间断更新)
    分享互联网2021年最新Java面试题汇总整理-附详细答案解析
    2021年面试,整理全网初、中、高级常见Java面试题附答案
    JS中使用map
    springboot整合MongoDB4.0多数据源实现事务
  • 原文地址:https://www.cnblogs.com/thiaoqueen/p/6555427.html
Copyright © 2020-2023  润新知