集合框架
4. Set容器
4.1 HashSet接口
- HashSet扩展AbstractSet并且实现Set接口
- HashSet使用散列表(又称哈希表)进行存储
- 构造方法
- HashSet()
- HashSet(Collection c)
- HashSet(int capacity)
- HashSet(int capacity,float fillRatio)
- HashSet没有定义任何超过它父类和接口的方法
- 散列集合没有确保其元素的顺序,因为散列处理通常不参与排序
HashSet源代码分析——HashSet操作的是HashMap的键
- HashSet调用的构造方法里创建了一个HashMap:
public HashSet() {
map = new HashMap<>();
}
- 给HashSet添加元素实际上就是添加到HashMap的键,值不需要
public boolean add(E e) {
return map.put(e, PRESENT)==null;
}
- HashSet的size就是HashMap的size,HashMap是空的,HashSet就是空的
public int size() {
return map.size();
}
public boolean isEmpty() {
return map.isEmpty();
}
- HashSet的迭代器是HashSet的所有键的迭代器,包含contains是去HashMap中查询有没有指定的key:
public Iterator<E> iterator() {
return map.keySet().iterator();
}
public boolean contains(Object o) {
return map.containsKey(o);
}
Dem1:HashSet的基本用法
package Collection;
import java.util.HashSet;
public class HashSetDemo1 {
public static void main(String[] args){
HashSet<String> data=new HashSet<String>();
data.add("Mandy");
data.add("Anne");
//add()方法返回一个boolean值,判断是否添加成功
System.out.println(data.add("Sarah"));
data.add("Lara");
//Sarah重复出现了,add()方法返回false,说明Set不能存储重复的值
System.out.println(data.add("Sarah"));
System.out.println(data);
HashSet<sstudent> stu=new HashSet<sstudent>();
/*在使用默认的equals方法时,new出来的两个“Mandy”被视为不同的对象,在HashSet中都能成功添加
重写sstudent的equals方法,让name和age对应相等的对象比较后返回ture,HashSet就只会存放一个*/
stu.add(new sstudent("Mandy",20));
System.out.println(stu.add(new sstudent("Mandy",20)));
stu.add(new sstudent("Jenny",20));
System.out.println(stu.size());
}
}
class sstudent{
private String name;
private int age;
public sstudent(String name, int age) {
super();
this.name = name;
this.age = age;
}
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;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
sstudent other = (sstudent) 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;
}
}
4.2 TreeSet接口:
- TreeSet为使用树来进行存储的Set接口提供了一个工具,对象按升序排序存储,访问和检索很快
- 在存储了大量的需要进行快速检索的排序信息的情况下,TreeSet是一个很好的选择
- 构造方法
- TreeSet()
- TreeSet(Collection c)
- TreeSet(Comparator comp)
- TreeSet(SortedSet ss)
- TreeSet没有定义任何超过它父类和接口的方法
- 散列集合没有确保其元素的顺序,因为散列处理通常不参与排序
- TreeSet调用的构造方法里创建了一个TreeMap:
public TreeSet() {
this(new TreeMap<E,Object>());
}
- 给TreeSet添加元素实际上就是添加到TreeMap的键,值不需要
public boolean add(E e) {
return m.put(e, PRESENT)==null;
}
Demo2:TessSet的基本用法
package Collection;
import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;
public class TreeSetDemo {
public static void main(String[] args) {
TreeSet<Pperson> ps=new TreeSet<Pperson>(new Comparator<Pperson>(){
@Override
public int compare(Pperson o1, Pperson o2){
if(o1.getAge()-o2.getAge()>0){
return 1;
}else if(o1.getAge()-o2.getAge()<0){
return -1;
}else
return 0;
}
});
ps.add(new Pperson("Anne",18));
ps.add(new Pperson("Rose",20));
ps.add(new Pperson("Cara",12));
ps.add(new Pperson("Bony",19));
Iterator<Pperson> it=ps.iterator();
while(it.hasNext()){
Pperson p=it.next();
System.out.println(p.getName()+"--"+p.getAge());
}
}
}
class Pperson /*implements Comparable<Pperson>*/{
private String name;
private int age;
public Pperson(String name, int age) {
super();
this.name = name;
this.age = age;
}
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;
}
/*@Override
public int compareTo(Pperson o){
if(this.age-o.age>0){
return 1;
}else if(this.age-o.age<0){
return -1;
}else
return 0;
}*/
}