HashSet和TreeSet有什么区别?

HashSet和TreeSet有什么区别?HashSet 不能保证元素的排列顺序 顺序有可能发生变化集合元素可以是 null 但只能放入一个 nullHashSet 底层是采用 HashMap 实现的 HashSet 底层是哈希表实现的 TreeSet Treeset 中的数据是排好序的 不允许放入 null 值 TreeSet 是通过 TreeMap 实现的 只不过 Set 用的只是 Map 的 key TreeSet 的底层实现是采用二叉树 红 黑树 的数据

参考代码: HashSetTest TreeSetTest TreeSetStudentTest

// Set的使用(不允许有重复的对象),可以是null,但只能放入一个null public class HashSetTest { public static void main(String[] args) { HashSet 
  
    hashSet = new HashSet 
   
     (); String a = new String("A"); String b = new String("B"); String E = new String("E"); String F = new String("F"); String c = new String("B"); String h = null; String i = null; hashSet.add(a); hashSet.add(b); hashSet.add(E); hashSet.add(F); hashSet.add(h); hashSet.add(i); System.out.println("hashSet得长度:" + hashSet.size()); // 元素B已经存在,插入时不在插入 String cz = hashSet.add(c) ? "此对象不存在" : "已经存在"; System.out.println("测试是否可以添加对象: " + cz); // 测试其中是否已经包含某个对象 System.out.println("hashSet中包含A:" + hashSet.contains("A")); Iterator 
    
      iter = hashSet.iterator(); while (iter.hasNext()) { System.out.println("hashSet中的元素:" + iter.next()); } // 测试某个对象是否可以删除 System.out.println("删除不存在的元素e:" + hashSet.remove("e")); System.out.println("删除存在的元素E:" + hashSet.remove("E")); System.out.println(); // 如果你想再次使用iter变量,必须重新更新,重新获取 iter = hashSet.iterator(); while (iter.hasNext()) { System.out.println("删除存在的元素E后:" + iter.next()); } } } 
     
    
  
// 有序的 public class TreeSetTest { public static void main(String[] args) { TreeSet 
  
    tree = new TreeSet 
   
     (); tree.add("China"); tree.add("America"); tree.add("Japan"); tree.add("Chinese"); tree.add("Diio"); Iterator 
    
      iter = tree.iterator(); while (iter.hasNext()) { System.out.println(iter.next()); } } } 
     
    
  
/ * 测试TreeSet存储自定义对象,并对对象排序的两种方式 */ public class TreeSetStudentTest { public static void main(String[] args) { System.out.println("下面是元素实现Comparable接口====="); TreeSet 
  
    ts = new TreeSet 
   
     (); ts.add(new Student("zhangsan01", 25)); ts.add(new Student("zhangsan02", 21)); ts.add(new Student("zhangsan03", 18)); ts.add(new Student("zhangsan04", 26)); ts.add(new Student("zhangsan04", 27)); ts.add(new Student("zhangsan04", 27)); ts.add(new Student("zhangsan05", 50)); Iterator 
    
      it = ts.iterator(); while (it.hasNext()) { Student stu = (Student) it.next(); System.out.println("姓名:" + stu.getName() + " 年龄:" + stu.getAge()); } System.out.println("下面是用自定义比较器类来实现比较的====="); TreeSet 
     
       tsc = new TreeSet 
      
        (new MyCompare()); tsc.add(new Student("zhangsan01", 25)); tsc.add(new Student("zhangsan02", 21)); tsc.add(new Student("zhangsan03", 18)); tsc.add(new Student("zhangsan04", 26)); tsc.add(new Student("zhangsan04", 27)); tsc.add(new Student("zhangsan04", 27)); tsc.add(new Student("zhangsan05", 50)); Iterator 
       
         itc = tsc.iterator(); while (itc.hasNext()) { Student stu = (Student) itc.next(); System.out.println("姓名:" + stu.getName() + " 年龄:" + stu.getAge()); } } } class Student implements Comparable 
         { private String name; private int age; public Student(String name, int age) { this.name = name; this.age = age; } / * 年龄和名称都相等的时候 */ @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 int hashCode() { final int prime = 31; int result = 1; result = prime * result + age; result = prime * result + ((name == null) ? 0 : name.hashCode()); System.out.println("hashCode : "+ result); return result; } 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; } public int compareTo(Object obj) { if (!(obj instanceof Student)) throw new RuntimeException("不是学生对象"); // 先按照年龄,之后看名称 Student stu = (Student) obj; if (this.age > stu.age) return 1; if (this.age == stu.age) return this.name.compareTo(stu.name); return -1; } } // 自定义比较类 class MyCompare implements Comparator  { public int compare(Object o1,Object o2) { Student s1 = (Student) o1; Student s2 = (Student) o2; // 先按名称,之后看年龄 int num = s1.getName().compareTo(s2.getName()); if( num == 0 ) return new Integer(s1.getAge()).compareTo(new Integer(s2.getAge())); return num; } }   
        
       
      
     
    
  
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/212531.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月18日 下午7:50
下一篇 2026年3月18日 下午7:51


相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号