博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HashSet与TreeSet
阅读量:7240 次
发布时间:2019-06-29

本文共 2357 字,大约阅读时间需要 7 分钟。

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

 

 

 

 

 

 

 

转载于:https://www.cnblogs.com/thiaoqueen/p/6555427.html

你可能感兴趣的文章
学习 ExtJS 4 面板与布局
查看>>
SQL ALTER TABLE 语句
查看>>
使用jquery提交form表单并自定义action
查看>>
Unity3D引用dll打包发布的问题及解决
查看>>
Android开发之Google Map
查看>>
基于内容的图片检索CBIR(Content Based Image Retrieval)简介
查看>>
VS2012编译LibZip库
查看>>
[置顶] 程序员的奋斗史(二十五)——情绪与生活
查看>>
Linux kernel中网络设备的管理
查看>>
反转字符串
查看>>
FusionCharts或其它flash的div图层总是浮在最上层? (转)
查看>>
[Android] Service和IntentService中显示Toast的区别
查看>>
How Tomcat Works(七)
查看>>
烟大 2239: 十进制与八进制的转换(栈和队列)
查看>>
hdu 4681(枚举+dp)
查看>>
Parallel Decision Tree
查看>>
iPhone较为基础的代码片段
查看>>
SED入门
查看>>
使用Unity3D引擎开发赛车游戏
查看>>
Mule消息路由
查看>>