目录
一.Set集合的特点
无序、对象不能重复(唯一)
二.Set集合的遍历方式
三.Set集合常用实现类
一.Set集合的特点
无序、对象不能重复(唯一)
eg:
//Set //1.特点:无序,唯一 //1) 无序:元素添加的顺序与输出的顺序不一致 //2)唯一:不允许添加重复的元素(被过滤了) Setset=new HashSet<>(); set.add(new Student(1, "zs", 100f)); set.add(new Student(2, "ls", 30f)); set.add(new Student(3, "ww", 20f)); set.add(new Student(1, "zs", 120f));
判断是过滤还是替换了:
1)默认使用equals比较,Object类型是引用类型,默认比较的是内存地址
2)重写hashcode和equals方法,先比较hadhcode值是否相同,在比较equals
2.1)hashcode值不相同,则不比较equals
2.2)hashcode值相同,则比较equals
二.Set集合的遍历方式
因为Set集合获取不到下标,所有遍历的方式只有两种
1.foreach
2.迭代器
//获取迭代器 Iteratorit = set.iterator(); while(it.hasNext()) { System.out.println(it.next()); }
三.Set集合常用实现类
HashSet
TreeSet:根据某种(规则)对里面的元素进行排序
自然比较接口: java.lang.Comparable
比较器: java.util.Comparator
String以AscII码进行比较,返回差值
实例:
entity包中的Student实体类
package com.zking.Connection.entity; import java.io.Serializable; import java.util.Comparator; public class Student implements Serializable,Comparable{ private Integer sid; private String sname; private Float score; public Student(Integer sid, String sname, Float score) { super(); this.sid = sid; this.sname = sname; this.score = score; } public Integer getSid() { return sid; } public void setSid(Integer sid) { this.sid = sid; } public String getSname() { return sname; } public void setSname(String sname) { this.sname = sname; } public Float getScore() { return score; } public void setScore(Float score) { this.score = score; } public Student() { super(); // TODO Auto-generated constructor stub } @Override public String toString() { return "Student [sid=" + sid + ", sname=" + sname + ", score=" + score + "]"; } @Override public int hashCode() { final int prime = 31; int result = 1; // result = prime * result + ((score == null) ? 0 : score.hashCode()); result = prime * result + ((sid == null) ? 0 : sid.hashCode()); result = prime * result + ((sname == null) ? 0 : sname.hashCode()); return result; } @Override public boolean equals(Object obj) { //true代表已存在 false代表允许添加 if (this == obj) return true; //obj对象是否为空 if (obj == null) return false; if (getClass() != obj.getClass()) return false; Student other = (Student) obj; if(this.getSid()!=other.getSid()) return false; return true; } @Override public int compareTo(Student o) { //比较者大于被比较者 返回1 //比较者等于被比较者 返回0 //比较者小于被比较者 返回-1 return this.getSid()-o.getSid(); } }
//TreeSet:以某种特定的规则对集合中的元素进行排序(升序和降序) //java.lang.Comparable:自然比较接口 //java.util.Comparator:比较器(推荐) Setset1=new TreeSet<>(); set1.add("zs"); set1.add("ls"); set1.add("ww"); Set set=new TreeSet<>(); set.add(new Student(1, "zs", 100f)); set.add(new Student(2, "ls", 30f)); set.add(new Student(3, "ww", 20f)); set.forEach(System.out::println);
自定义比较器
以名字排序,创建一个java文件,编写代码:
public class NameComparator implements Comparator{ @Override public int compare(Student o1, Student o2) { // TODO Auto-generated method stub return o1.getSname().hashCode()-o2.getSname().hashCode(); } }
最后去调用就可以了
//调用自定义 Setset=new TreeSet<>(new NameComparator()); set.add(new Student(1, "zs", 100f)); set.add(new Student(2, "ls", 30f)); set.add(new Student(3, "ww", 20f)); set.forEach(System.out::println);
总结:学习Set集合框架,同时也可以方便开发。



