1、体系结构
Set
├HashSet
├TreeSet
2、Set
元素是无序的(存入和取出的顺序不一定一致),元素不可以重复,而且方法和Collection一致。Set集合取出元素的方式只有一种:迭代器。
3、HashSet
底层数据结构是哈希表,HashSet是如何保证元素唯一性的呢?
是通过元素的两个方法,hashCode和equals来完成,如果元素hashCode值相同才会判断equals是否为true;如果hashCode值相同是不会调用equals
注意:对于判断元素是否存在以及删除以来的方法就是hashCode和equals方法。、
代码示例:
package collection;
public class PersonHashSet {
private String name;
private int age;
//重写构造函数
PersonHashSet(String name,int age)
{
this.name=name;
this.age=age;
}
//重写hashCode
public int hashCode()
{
return name.hashCode()+age*37;
}
//重写equals方法
public boolean equals(Object obj)
{
if(!(obj instanceof PersonHashSet))
return false;
PersonHashSet p=(PersonHashSet)obj;
return this.name.equals(p.name)&&this.age==age;
}
//获取姓名
public String getName()
{
return name;
}
//获取年龄
public int getAge()
{
return age;
}
}
package collection;
import java.util.*;
public class HashSetDemo {
/**
* @param args
*/
public static void main(String[] args) {
//定义集合hs
HashSet hs=new HashSet();
//添加元素
hs.add(new PersonHashSet("wangwu01",33));
hs.add(new PersonHashSet("wangwu01",34));
hs.add(new PersonHashSet("wangwu02",35));
hs.add(new PersonHashSet("wangwu01",33));
hs.add(new PersonHashSet("wangwu03",33));
hs.add(new PersonHashSet("wangwu02",35));
hs.add(new PersonHashSet("wangwu01",33));
Iterator it=hs.iterator();
while(it.hasNext())
{
PersonHashSet p=(PersonHashSet)it.next();
sop(p.getName()+":"+p.getAge());
}
}
//打印结果
private static void sop(Object obj) {
System.out.println(obj);
}
}
4、TreeSet
可以对Set集合中的元素进行排序,底层数据结构是二叉树,保证元素唯一性的依据:compareTo方法return 0;
TreeSet排序的第一种方式:
让元素自身具备比较性,元素需要实现Comparable接口,覆盖copareTo方法,这种方式称为元素的自然顺序,或者叫做默认顺序。
代码示例:
package collection;
/**
*TreeSet排序的第一种方法,让元素自身具备比较性
*
*
*/
public class Student implements Comparable
{
private String name;
private int age;
Student(String name,int age)
{
this.name=name;
this.age=age;
}
//重写compareTo方法
public int compareTo(Object obj)
{
if(!(obj instanceof Student))
throw new RuntimeException("不是学生");
Student s=(Student)obj;
if(this.age>s.age)
return 1;
else if(this.age==s.age)
return this.name.compareTo(s.name);
return -1;
}
//获取姓名
public String getName()
{
return name;
}
//获取年龄
public int getAge()
{
return age;
}
}
package collection;
import java.util.*;
public class TreeSetDemo {
/**
* @param args
*/
public static void main(String[] args) {
//定义一个TreeSet集合ts
TreeSet ts=new TreeSet();
//添加元素
ts.add(new Student("wangwu01",33));
ts.add(new Student("wangwu02",33));
ts.add(new Student("wangwu01",34));
ts.add(new Student("wangwu01",33));
ts.add(new Student("wangwu02",33));
ts.add(new Student("wangwu01",33));
Iterator it=ts.iterator();
while(it.hasNext())
{
Student s=(Student)it.next();
sop(s.getName()+":"+s.getAge());
}
}
//打印元素
public static void sop(Object obj)
{
System.out.println(obj);
}
}
TreeSet排序的第二种方式:
当元素自身不具备比较性时,或者具备的比较性不是所需要的,这时就需要让集合自身具备比较性。在集合初始化的时候就有了比较性。定一个类,实现Comparator接口,覆盖compare方法。
代码示例:
package collection;
import java.util.*;
/**
*TreeSet排序的第二种方法,创建比较器,让集合自身具有比较性
*
*
*/
public 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(s2.getAge());
return num;
}
}
package collection;
import java.util.*;
public class TreeSetDemo {
/**
* @param args
*/
public static void main(String[] args) {
//定义一个TreeSet集合ts
TreeSet ts=new TreeSet(new MyCompare());//在集合初始化时定义一个比较器
//添加元素
ts.add(new Student("wangwu01",33));
ts.add(new Student("wangwu02",33));
ts.add(new Student("wangwu01",34));
ts.add(new Student("wangwu01",33));
ts.add(new Student("wangwu02",33));
ts.add(new Student("wangwu01",33));
Iterator it=ts.iterator();
while(it.hasNext())
{
Student s=(Student)it.next();
sop(s.getName()+":"+s.getAge());
}
}
//打印元素
public static void sop(Object obj)
{
System.out.println(obj);
}
}
当两种排序方式都存在时,以比较器为主。
5、实例:按照字符串长度排序
a) 思路:字符串本身具备比较性,但是他的比较方式不是所需要的,这时就只能使用比较器。
b) 代码实现:
package collection;
import java.util.Comparator;
/**
*
* 字符串比较器
*
*/
public class StrLenComparator implements Comparator
{
public int compare(Object o1,Object o2)
{
String s1=(String)o1;
String s2=(String)o2;
int num=new Integer(s1.length()).compareTo(new Integer(s2.length()));//先比较字符串的长度
if(num==0)
return s1.compareTo(s2);//如果字符串长度相同,再比较字符串内容
return num;
}
}
package collection;
import java.util.Iterator;
import java.util.TreeSet;
public class TreeSetTest {
/**
* @param args
*/
public static void main(String[] args) {
TreeSet ts=new TreeSet(new StrLenComparator());
ts.add("abcdef");
ts.add("af");
ts.add("bbcdef");
ts.add("abc");
ts.add("abc");
ts.add("def");
ts.add("aa");
Iterator it=ts.iterator();
while(it.hasNext())
{
System.out.println(it.next());
}
}
}