【集合框架】4. Set容器
来源:互联网 发布:递推算法求韩信点兵 编辑:程序博客网 时间:2024/04/30 09:29
集合框架
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; }*/}
0 0
- 【集合框架】4. Set容器
- set集合容器
- set集合容器
- set集合容器
- Set集合容器
- set集合容器
- set集合容器
- set集合容器用法
- set集合容器
- set集合容器
- set集合容器
- set 集合容器
- set集合容器
- set集合容器
- set集合容器
- 集合容器set
- set集合容器
- set集合容器
- java多线程-04-线程池
- 019 Remove Nth Node From End of List
- 每日一练--抽奖
- android系统级别硬件访问服务程序
- 二叉树的高度 java 利用递归和层次遍历两种方法
- 【集合框架】4. Set容器
- 一元多项式的加减
- mysql 常用命令
- Linux网络编程---I/O复用模型之poll
- 【集合框架】5. Collections类
- android 开发 -- NavigationView和DrawerLayout实现 侧滑栏(Material Design)
- 时间序列分析这件小事(六)--非平稳时间序列与差分
- 基于JQuery 的分页控件
- java调用spark的借口运行WordCount