1.Set接口
Set 接口继承 Collection 接口,而且它不允许集合中存在重复项,每个具体的 Set 实现类依赖添加的对象的 equals()方法来检查独一性。Set接口没有引入新方法,所以Set就是一个Collection,只不过其行为不同。
下面介绍Set接口3种主要的实现类。
HashSet 为快速查找而设计的Set。存入HashSet的元素必须定义hashCode()。
TreeSet 保存次序的Set,底层为树结构。使用它可以从Set中提取有序的序列。元素必须实现Comparable接口。
LinkedHashSet 具有HashSet的查询速度,且内部使用链表维护元素的顺序(插入顺序)。在使用迭代器遍历Set时,结果会按插入的次序显示。元素必须定义hashCode()方法。
2.HashSet
1)是实现Set接口的hash table(哈希表),依靠HashMap来实现的。
2)我们应该为要存放到散列表的各个对象定义hashCode()和equals()。
import java.util.HashSet;
import java.util.Iterator;
class HashSetTest {
public static void main(String[] args) {
HashSet<Student> hs = new HashSet<Student>();
hs.add(new Student(1, "zhangsan"));
hs.add(new Student(2, "lisi"));
hs.add(new Student(3, "wangwu"));
hs.add(new Student(1, "zhangsan"));// 重写hashCode()方法、equals()方法后,hashset不再接受重复的元素
Iterator it = hs.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
}
}
class Student {
int num;
String name;
Student(int num, String name) {
this.num = num;
this.name = name;
}
public int hashCode() {// 重写hashCode()方法
return num * name.hashCode();
}
public boolean equals(Object o) {// 重写equals()方法
Student s = (Student) o;
return num == s.num && name.equals(s.name);
}
public String toString() {
return num + ":" + name;
}
}
结果:
1:zhangsan
3:wangwu
2:lisi
3.TreeSet
1)TreeSet是依靠TreeMap来实现的。
2)TreeSet是一个有序集合,TreeSet中元素将按照升序排列,缺省是按照自然顺序进行排列,意味着TreeSet中元素要实现Comparable接口。
3)我们可以在构造TreeSet对象时,传递实现了Comparator接口的比较器对象。
示例一,是按照缺省的自然顺序(对于String,则是字母顺序)排列。
import java.util.Iterator;
import java.util.TreeSet;
class TreeSetTest {
public static void main(String[] args) {
TreeSet<String> ts = new TreeSet<String>();
ts.add("a");
ts.add("b");
ts.add("c");
Iterator it = ts.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
}
}
结果:
a
b
c
示例二Student类通过实现Comparable接口来自定义排列顺序。这里排序的依据是Student类的成员变量(int num)。从运行结果可以发现,当出现num重复的情况,只保存首次加入的元素。
import java.util.Iterator;
import java.util.TreeSet;
class TreeSetTest {
public static void main(String[] args) {
TreeSet<Student> ts = new TreeSet<Student>();
ts.add(new Student(2, "Tom"));
ts.add(new Student(1, "Jeff"));
ts.add(new Student(3, "Ada"));
ts.add(new Student(3, "Mary"));
ts.add(new Student(1, "Peter"));//compareTo()返回0,则不加入
Iterator it = ts.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
}
}
class Student implements Comparable {
int num;
String name;
Student(int num, String name) {
this.num = num;
this.name = name;
}
public int compareTo(Object o) {
Student s = (Student) o;
return num > s.num ? 1 : (num == s.num ? 0 : -1);
}
public String toString() {
return num + ":" + name;
}
}
结果:
1:Jeff
2:Tom
3:Ada
示例三通过Student的内部类StudentComparator,来实现第二关键字的排序,即当第一关键字num相等时,使用第二关键字name排序。
import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;
class TreeSetTest {
public static void main(String[] args) {
TreeSet<Student> ts = new TreeSet<Student>();
ts.add(new Student(2, "Tom"));
ts.add(new Student(1, "Jeff"));
ts.add(new Student(3, "Ada"));
ts.add(new Student(3, "Mary"));
ts.add(new Student(1, "Peter"));
Iterator it = ts.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
}
}
class Student implements Comparable {
int num;
String name;
Student(int num, String name) {
this.num = num;
this.name = name;
}
private class StudentComparator implements Comparator {
public int compare(Object o1, Object o2) {
Student s1 = (Student) o1;
Student s2 = (Student) o2;
int result = s1.num > s2.num ? 1 : (s1.num == s2.num ? 0 : -1);
if (result == 0) {
result = s1.name.compareTo(s2.name);
}
return result;
}
}
public int compareTo(Object o) {
/*Student s = (Student) o;
return num > s.num ? 1 : (num == s.num ? 0 : -1);*/
StudentComparator sc=new StudentComparator();
return sc.compare(this, o);
}
public String toString() {
return num + ":" + name;
}
}
结果:
1:Jeff
1:Peter
2:Tom
3:Ada
3:Mary
4.LinkedHashSet
1)应该为要存放到散列表的各个对象定义hashCode()和equals()。
2)按插入顺序输出元素。(比较下面示例结果与HashSet示例的结果)
import java.util.Iterator;
import java.util.LinkedHashSet;
public class LinkedHashSetTest2 {
public static void main(String[] args) {
LinkedHashSet<Student> hs = new LinkedHashSet<Student>();
hs.add(new Student(1, "zhangsan"));
hs.add(new Student(2, "lisi"));
hs.add(new Student(3, "wangwu"));
hs.add(new Student(1, "zhangsan"));// 重写hashCode()方法、equals()方法后,hashset不再接受重复的元素
Iterator it = hs.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
}
}
class Student {
int num;
String name;
Student(int num, String name) {
this.num = num;
this.name = name;
}
public int hashCode() {// 重写hashCode()方法
return num * name.hashCode();
}
public boolean equals(Object o) {// 重写equals()方法
Student s = (Student) o;
return num == s.num && name.equals(s.name);
}
public String toString() {
return num + ":" + name;
}
}
结果:
1:zhangsan
2:lisi
3:wangwu
5.SortedSet接口
“集合框架”提供了个特殊的Set接口:SortedSet,它保持元素的有序顺序。添加到SortedSet实现类的元素必须实现Comparable接口,否则您必须给它的构造函数提供一个Comparator接口的实现。TreeSet类是它的唯一一份实现。
因为集必须包含唯一的项,如果添加元素时比较两个元素导致了0返回值(通过Comparable的compareTo()方法或Comparator的compare()方法),那么新元素就没有添加进去。
注意,SortedSet意思是“根据对象的比较顺序”,而不是“插入顺序”进行排序。
SortedSet中的元素一定是有序的。这使得SortedSet接口多了一些方法:
① Comparator comparator():返回Set锁使用的Comparator对象,或者用null表示它使用Object自有的排序方法。
② Object first():返回最小的元素。
③ Object last():返回最大的元素。
④ SortedSet subSet(fromElement, toElement):返回Set的子集,其中的元素从fromElement开始到toElement为止(包括fromElement,不包括 toElement)。
⑤ SortedSet headSet(toElement):返回Set的子集,其中的元素都应小于toElement。
⑥ SortedSet headSet(toElement):返回Set的子集,其中的元素都应大于fromElement。
import java.util.Collections;
import java.util.Iterator;
import java.util.SortedSet;
import java.util.TreeSet;
public class SortedSetDemo {
static void print(Object obj){
System.out.println(obj);
}
public static void main(String[] args) {
SortedSet<String> sortedSet = new TreeSet<String>();
Collections.addAll(sortedSet, "one two three four five six seven eight"
.split(" "));
print(sortedSet);
String low=sortedSet.first();
String high=sortedSet.last();
print(low);
print(high);
Iterator<String> it=sortedSet.iterator();
for(int i=0;i<=6;i++){
if(i==3)
low=it.next();
if(i==6)
high=it.next();
else it.next();
}
print(low);
print(high);
print(sortedSet.subSet(low, high));
print(sortedSet.headSet(high));
print(sortedSet.tailSet(low));
}
分享到:
相关推荐
该文档主要详细总结了Java集合的相关知识,包括Collection和Map接口、Collection接口的子接口List和Set接口以及具体的实现类、存储原理等;Map接口的子接口HashMap、LinkedHashMap、TreeMap、Properties等
在Java中,Collection接口是一个顶层接口,它有两个主要的子接口:List和Set。其中,List是一个有序的集合,可以包含重复的元素;而Set是一个不重复的集合,元素是无序的。 Collection接口的常用方法 1、add(Object ...
Java集合框架汇总 1.集合框架结构图 1 2.两种特殊的Java容器类List和Set分析 2 3. Collection 接口: 2 ...6.Set接口: 5 7.Map接口: 6 8.Comparable接口和Comparator接口: 7 9. 集合框架面试题 7
Java集合框架中的Set接口是一种不允许包含重复元素的集合。Set接口继承自Collection接口,因此它具有Collection接口定义的所有方法。同时,Set接口还具有自己的特殊方法,例如:添加元素、删除元素、判断元素是否...
Java集合框架概述 Java集合框架是一个抽象数据...Collection接口有两个重要的子接口:List和Set。 2、List接口 List接口是有序集合,可以存储重复元素。它定义了有序的列表,可以根据索引位置访问列表中的元素。List
031304_【第13章:Java类集】_Set接口笔记.pdf 031305_【第13章:Java类集】_排序及重复元素说明笔记.pdf 031306_【第13章:Java类集】_SortedSet接口笔记.pdf 031307_【第13章:Java类集】_Iterator接口笔记.pdf ...
Set -- Set接口(没有对下标操作的方法) 特点:无序的,且不可重复 HashSet: 数据结构:Hash表/Hash数组 运用:去重复 TreeSet: 数据结构:二叉树 运用:自然排序 Map --- Map接口 特
Set接口中的方法和Collection一致。 |--HashSet: 内部数据结构是哈希表 ,是不同步的。 如何保证该集合的元素唯一性呢? 是通过对象的hashCode和equals方法来完成对象唯一性的。 如果对象的hashCode值不同,...
Collection:Collection 是集合 List、Set、Queue 的最基本的接口。 Iterator:迭代器,可以通过迭代器遍历集合中的数据 Map:是映射表的基础接口 4. JAVA 多线程并发 5. JAVA 基础 5.1JAVA 异常分类及处理...
< java.util >Set接口 80 < java.util >Map接口 81 把map集合转成set的方法 82 使用集合的技巧 83 Collections--集合工具类 83 Arrays—数组对象工具类 84 增强for循环 85 可变参数(...) 86 枚举:关键字 enum 86 ...
其超级父接口是:java.util.Collection; 第二种:以键值对存储。(类似于python的集合)其超级父接口是:java.util.Map; 前者每个位置只能保存一个元素,后者可以保存两个元素。 分类 Collection又可分为List、Set、...
抽象类是否可实现(implements)接口? 抽象类是否可继 承具体类(concrete class)? 抽象类中是否可以有静态的 main 方法? ...................... 16 21、写 clone()方法时,通常都有一行代码,是什么? ..............
C语言实现的,提供语言和(基于SQL的)数据库进行交互的“一致性”的接口 JDBC:java版本的ODBC JDBC连接数据库的步骤: 1.注册驱动(加载驱动): 注册的方式: 1.使用类加载器(使用反射的方式) Class.for...
· Set 接口继承 Collection,但不允许重复,使用自己内部的一个排列机制。 · List 接口继承 Collection,允许重复,以元素安插的次序来放置元素,不会重新排列。 · Map接口是一组成对的键-值对象,即所持有...
Java集合类是Java.util包中的重要内容,它提供了一套性能优良、使用方便的接口和类,用于处理对象的集合。这些类主要用于存储、检索、操作一组对象数据。...Set接口的实现类包括HashSet、LinkedHashSet等。
前者是Collection层次结构中的根接口,而“Collections”是一个提供静态方法来处理某些Collection类型的类。 3. 单例属性 单例模式: 保持构造器私有【private】,并导出【提供外界访问】公有的静态成员 客户端...
企业常见java面试题,java基础,java进阶 JDK 和 JRE 有什么区别? == 和 equals 的区别是什么? 两个对象的 hashCode()相同,则 equals()也一定为 true,对吗? final 在 java 中有什么作用? java 中操作字符串...
java.util包中定义了各种用于集合操作的类和接口,这些类和接口构成了Java语言的集合框架(Collection Framework)。 Java集合中可以放对象,不能存放基础数据类型数。 Collection Framework 根据不同类型的集合的特点...