TreeSet 的使用
本文存在重大权限,请右上角
1.根据实体某个属性去重 同时排序
public static void main(String[] args) {
List<Userr> userrs = Arrays.asList(new Userr(1, "x"), new Userr(2, "x"), new Userr(1, "x"),
new Userr(1, "x"), new Userr(2, "x"), new Userr(3, "x"), new Userr(1, "x"),
new Userr(1, "x"), new Userr(3, "x"));
Set<Userr> set = new TreeSet<Userr>(Comparator.comparing(Userr::getLsh));
set.addAll(userrs);
ArrayList<Userr> userrs1 = new ArrayList<>(set);
System.out.println(userrs1);
}
@Data
@AllArgsConstructor
@NoArgsConstructor
static
class Userr {
private Integer lsh;
private String x;
}
线程不安全 可以用Collections.synchronizedSet()
2. 看看实体类
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Userr implements Comparable<Userr> {
private Integer lsh;
private String x;
@Override
public int compareTo(Userr o) {
if (this.equals(o)) {
return 0;
} else {
if (o.getLsh() == null) {
return 1;
}
if (this.getLsh() == null) {
return -1;
}
return this.lsh.compareTo(o.getLsh());
}
}
3. 不废话 上代码
@Test
public void ad() {
List<Userr> userrs = Arrays.asList(new Userr(11, "x"), new Userr(2, "x"), new Userr(1, "x"),
new Userr(1, "x"), new Userr(2, "x"), new Userr(3, "x"), new Userr(1, "x"),
new Userr(31, "x"), new Userr(3, "x"));
// TreeSet 使用时 要么实体类实现 Comparable 要么 TreeSet 有 Comparable 构造
/**
* java.lang.ClassCastException: class xyz.tangzkk.baespmvc.web.Userr
* cannot be cast to class java.lang.Comparable
* ↓↓↓↓↓↓ 这里报错
*/
// TreeSet set = new TreeSet();
TreeSet set = new TreeSet();
set.addAll(userrs);
// 因为指定了 Comparable 在 lsh 字段上 so 这条插入无反应 返回 false
boolean da = set.add(new Userr(1, "da"));
assertFalse(da);
// 虽然 x =x 到处都是 但是 没有这个比较条件 随便插入
boolean x = set.add(new Userr(222, "x"));
assertTrue(x);
System.out.println(set);
}
@Test
public void addd() {
List<Userr> userrs = Arrays.asList(new Userr(11, "x"), new Userr(2, "x"), new Userr(1, "x"),
new Userr(1, "x"), new Userr(2, "x"), new Userr(3, "x"), new Userr(1, "x"),
new Userr(31, "x"), new Userr(3, "x"));
TreeSet set = new TreeSet();
set.addAll(userrs);
set.add(new Userr(1, "da"));
System.out.println(set);
}
/**
* 倒序
*/
@Test
public void sortR() {
List<Userr> userrs = Arrays.asList(new Userr(11, "x"), new Userr(2, "x"), new Userr(1, "x"),
new Userr(1, "x"), new Userr(2, "x"), new Userr(3, "x"), new Userr(1, "x"),
new Userr(31, "x"), new Userr(3, "x"));
// 花里胡哨 略过
TreeSet<Userr> set = new TreeSet(userrs);
Iterator iterator = set.descendingIterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
// 高大上 stream
List<Userr> collect = set.stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList());
System.out.println(collect);
// 获取内容 [ x.x.x.x.x ) 包左不包右
SortedSet<Userr> userrs1 = set.subSet(new Userr(1, "x"), new Userr(3, "x"));
System.out.println(userrs1);// [Userr(lsh=1, x=x), Userr(lsh=2, x=x)]
// 获取 小于 head 取名很明了
SortedSet<Userr> xx = set.headSet(new Userr(2, "xx"));
System.out.println(xx);//[Userr(lsh=1, x=x)]
// 获取 大于 tail 取名很明了
SortedSet<Userr> xx1 = set.tailSet(new Userr(2, "xx"));
System.out.println(xx1);
// TreeSet bu允许有 null 因为 会调用比较器 比较null 会检查 null的值 报空指针
// 但是 我在比较器里写校验呢? 结果当然是美滋滋得啦
boolean add = set.add(new Userr(null, null));
assertTrue(add);
System.out.println(set);
}