TreeSet的使用方法总结、实现原理、使用示例

TreeSet的使用方法总结、实现原理、使用示例HashMap的使用方法总结、实现原理、使用示例

目录

存储特点:有序,不重复;key不能为空,value可以为null(总结一点:凡是有Tree的集合,都是有序的,凡是有Set的就是不重复的)

底层原理:使用NavigableMap,但NavigableMap只是一个接口,最终是使用TreeMap

构造函数

public TreeSet():底层创建新的TreeMap

public TreeSet(Collection c):通过集合构造TreeSet

public TreeSet(Comparator comparator):自定义比较器的TreeSet

如果Treeset存储的是对象,可以通过对象实现Comparator,实现自定义对象比较器

public TreeSet(SortedSet s):通过set构造TreeSet

常用方法

public boolean add(E e):添加元素

public  boolean addAll(Collection c):通过集合添加元素

public E ceiling(E e):返回大于或等于给定键值的最小键值

public void clear():清空集合

public Object clone():克隆集合

public Comparator comparator():用于在此树映射中维护顺序的比较器,如果使用其键的自然顺序,则为null

public boolean contains(Object o) :是否包含摸个元素

public Iterator descendingIterator():用于按降序迭代元素。

public NavigableSet descendingSet():?

public E first():获取首点

public E floor(E e):返回小于或等于给定键值的最大键值

public SortedSet headSet(E toElement):返回key<=toElement集合

public NavigableSet headSet(E toElement, boolean inclusive):返回key<=toElement集合,inclusive=true返回的集合在原set中,会包含自己,否则不会包含

public E higher(E e):返回严格大于给定键值的最小键值

public boolean isEmpty():判断集合是否为空

public Iterator iterator() :迭代输出

public E last():获取最后的值

public E lower(E e):返回严格小于给定键值的最大键值

public E pollFirst():获取第一个值并移除第一个值

public E pollLast():获取最后值并移除这个值

public boolean remove(Object o):移除元素

public int size() :当前set容量

public Spliterator spliterator() : 方法用于拆分set元素,并逐个迭代它们。

public NavigableSet subSet(E fromElement, boolean fromInclusive, E toElement,   boolean toInclusive):返回from到to之间的值,fromInclusive和toInclusive代表是否包含当前值

public SortedSet subSet(E fromElement, E toElement):返回from到to之间的值,包含from,不包含to,即【左闭右开)

public SortedSet tailSet(E fromElement):返回>=fromElement值的集合元素

public NavigableSet tailSet(E fromElement, boolean inclusive)返回>=fromElement值的集合元素,inclusive=true包含自身,=false就不包含


存储特点:有序,不重复;key不能为空,value可以为null(总结一点:凡是有Tree的集合,都是有序的,凡是有Set的就是不重复的)

demo

public class Test {
    public static void main(String[] args) {
        TreeSet set = new TreeSet();
        set.add(3);
        set.add(2);
        set.add(5);
        set.add(1);
        set.add(3);
        System.out.println(set);
    }
}

结果:

[1, 2, 3, 5]

底层原理:使用NavigableMap,但NavigableMap只是一个接口,最终是使用TreeMap

构造函数

public TreeSet():底层创建新的TreeMap

public TreeSet(Collection<? extends E> c):通过集合构造TreeSet

demo

public class Test {
    public static void main(String[] args) {
        List list = new ArrayList();
        list.add(3);
        list.add(3);
        list.add(2);
        list.add(1);
        list.add(4);
        TreeSet set = new TreeSet(list);

        System.out.println("list:"+list);
        System.out.println("set:"+set);
    }
}

结果:

list:[3, 3, 2, 1, 4]
set:[1, 2, 3, 4]

public TreeSet(Comparator<? super E> comparator):自定义比较器的TreeSet

如果Treeset存储的是对象,可以通过对象实现Comparator,实现自定义对象比较器

实体类Person,实现Comparator接口

public class Person implements Comparable {
    private String name;
    private Integer age;

    public Person(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    //实现比较方法
    @Override
    public int compareTo(Object o) {
        Person p = (Person) o;
        return this.age - p.getAge();
    }
}

测试类

public class Test {
    public static void main(String[] args) {
        TreeSet set = new TreeSet();
        set.add(new Person("成龙",13));
        set.add(new Person("胡歌",22));
        set.add(new Person("刘亦菲",11));
        set.add(new Person("周星驰",16));
        set.add(new Person("李连杰",56));
        System.out.println("set:"+set);
    }
}

结果:年龄从低到高排列

set:[Person{name='刘亦菲', age=11}, 
Person{name='成龙', age=13}, 
Person{name='周星驰', age=16}, 
Person{name='胡歌', age=22}, 
Person{name='李连杰', age=56}]

public TreeSet(SortedSet<E> s):通过set构造TreeSet

demo

public class Test {
    public static void main(String[] args) {
        TreeSet set = new TreeSet();
        set.add(new Person("成龙",13));
        set.add(new Person("胡歌",22));
        set.add(new Person("刘亦菲",11));
        set.add(new Person("周星驰",16));
        set.add(new Person("李连杰",56));

        TreeSet t = new TreeSet(set);

        System.out.println("set:"+set);

        t.add(new Person("杨超越",18));
        System.out.println("t:"+t);
    }
}

结果

set:[Person{name='刘亦菲', age=11}, Person{name='成龙', age=13}, Person{name='周星驰', age=16}, Person{name='胡歌', age=22}, Person{name='李连杰', age=56}]
t:[Person{name='刘亦菲', age=11}, Person{name='成龙', age=13}, Person{name='周星驰', age=16}, Person{name='杨超越', age=18}, Person{name='胡歌', age=22}, Person{name='李连杰', age=56}]

常用方法

public boolean add(E e):添加元素

public  boolean addAll(Collection<? extends E> c):通过集合添加元素

public class Test {
    public static void main(String[] args) {
        List list = new ArrayList();
        list.add(3);
        list.add(3);
        list.add(2);
        list.add(1);
        list.add(4);
        TreeSet set = new TreeSet(list);

        System.out.println("list:"+list);
        System.out.println("set:"+set);
    }
}

结果:

list:[3, 3, 2, 1, 4]
set:[1, 2, 3, 4]

public E ceiling(E e):返回大于或等于给定键值的最小键值

demo

public class Test {
    public static void main(String[] args) {
        TreeSet set = new TreeSet();
        Person person = new Person("古天乐",12);
        set.add(new Person("成龙",13));
        set.add(new Person("胡歌",22));
        set.add(new Person("刘亦菲",11));
        set.add(new Person("周星驰",16));
        set.add(new Person("李连杰",56));

        System.out.println("set:"+set);
        System.out.println(set.ceiling(person));
    }
}

结果:最接近古天乐年纪的是成龙,所有结果是成龙

set:[Person{name='刘亦菲', age=11}, Person{name='成龙', age=13}, Person{name='周星驰', age=16}, Person{name='胡歌', age=22}, Person{name='李连杰', age=56}]
Person{name='成龙', age=13}

public void clear():清空集合

public Object clone():克隆集合

demo

public class Test {
    public static void main(String[] args) {
        TreeSet set = new TreeSet();
        Person person = new Person("古天乐",12);
        set.add(person);
        set.add(new Person("胡歌",22));
        set.add(new Person("刘亦菲",11));
        set.add(new Person("周星驰",16));
        set.add(new Person("李连杰",56));

        TreeSet clone = (TreeSet)set.clone();

        System.out.println("set:"+set);
        clone.remove(person);
        System.out.println("clone:"+clone);
    }
}

结果:

set:[Person{name='刘亦菲', age=11}, Person{name='古天乐', age=12}, Person{name='周星驰', age=16}, Person{name='胡歌', age=22}, Person{name='李连杰', age=56}]
clone:[Person{name='刘亦菲', age=11}, Person{name='周星驰', age=16}, Person{name='胡歌', age=22}, Person{name='李连杰', age=56}]

public Comparator<? super E> comparator():用于在此树映射中维护顺序的比较器,如果使用其键的自然顺序,则为null

public boolean contains(Object o) :是否包含摸个元素

public Iterator<E> descendingIterator():用于按降序迭代元素。

demo

public class Test {
    public static void main(String[] args) {
        TreeSet set = new TreeSet();
        Person person = new Person("古天乐",23);
        Person person2 = new Person("杨超越",18);
        set.add(person);
        set.add(person2);
        set.add(new Person("胡歌",22));
        set.add(new Person("刘亦菲",11));
        set.add(new Person("周星驰",18));
        set.add(new Person("李连杰",56));


        System.out.println("set:"+set);
        Iterator iterator = set.descendingIterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }
    }
}

结果:

set:[Person{name='刘亦菲', age=11}, Person{name='杨超越', age=18}, Person{name='胡歌', age=22}, Person{name='古天乐', age=23}, Person{name='李连杰', age=56}]
Person{name='李连杰', age=56}
Person{name='古天乐', age=23}
Person{name='胡歌', age=22}
Person{name='杨超越', age=18}
Person{name='刘亦菲', age=11}

public NavigableSet<E> descendingSet():?

public E first():获取首点

demo

public class Test {
    public static void main(String[] args) {
        TreeSet set = new TreeSet();
        Person person = new Person("古天乐",12);
        set.add(person);
        set.add(new Person("胡歌",22));
        set.add(new Person("刘亦菲",11));
        set.add(new Person("周星驰",16));
        set.add(new Person("李连杰",56));

        System.out.println("set:"+set);
        System.out.println("set:"+set.first());
    }
}

结果

set:[Person{name='刘亦菲', age=11}, Person{name='古天乐', age=12}, Person{name='周星驰', age=16}, Person{name='胡歌', age=22}, Person{name='李连杰', age=56}]
set:Person{name='刘亦菲', age=11}

public E floor(E e):返回小于或等于给定键值的最大键值

demo

public class Test {
    public static void main(String[] args) {

        TreeSet set = new TreeSet();
        Person person = new Person("古天乐", 23);
        //set.add(person);
        set.add(new Person("胡歌", 22));
        set.add(new Person("刘亦菲", 11));
        set.add(new Person("周星驰", 16));
        set.add(new Person("李连杰", 56));

        System.out.println("set:" + set);
        System.out.println(set.floor(person));
    }
}

结果:比23最小的是22 

set:[Person{name='刘亦菲', age=11}, Person{name='周星驰', age=16}, Person{name='胡歌', age=22}, Person{name='李连杰', age=56}]
Person{name='胡歌', age=22}

public SortedSet<E> headSet(E toElement):返回key<=toElement集合

demo

public class Test {
    public static void main(String[] args) {
        TreeSet set = new TreeSet();
        Person person = new Person("古天乐",18);
        //set.add(person);
        set.add(new Person("胡歌",22));
        set.add(new Person("刘亦菲",11));
        set.add(new Person("周星驰",16));
        set.add(new Person("李连杰",56));

        System.out.println("set:"+set);
        System.out.println("返回age小于person.age的元素set:"+set.headSet(person));
    }
}

结果

set:[Person{name='刘亦菲', age=11}, Person{name='周星驰', age=16}, Person{name='胡歌', age=22}, Person{name='李连杰', age=56}]
返回age小于person.age的元素set:[Person{name='刘亦菲', age=11}, Person{name='周星驰', age=16}]

public NavigableSet<E> headSet(E toElement, boolean inclusive):返回key<=toElement集合,inclusive=true返回的集合在原set中,会包含自己,否则不会包含

demo

public class Test {
    public static void main(String[] args) {
        TreeSet set = new TreeSet();
        Person person = new Person("古天乐",18);
        set.add(person);
        set.add(new Person("胡歌",22));
        set.add(new Person("刘亦菲",11));
        set.add(new Person("周星驰",16));
        set.add(new Person("李连杰",56));

        System.out.println("set:"+set);
        System.out.println("返回age小于person.age的元素set:"+set.headSet(person,false));
    }
}

结果:返回的结果包含person自己,如果是false就不会包含

set:[Person{name='刘亦菲', age=11}, Person{name='周星驰', age=16}, Person{name='古天乐', age=18}, Person{name='胡歌', age=22}, Person{name='李连杰', age=56}]
返回age小于person.age的元素set:[Person{name='刘亦菲', age=11}, Person{name='周星驰', age=16}]

public E higher(E e):返回严格大于给定键值的最小键值

demo:person上面有

public class Test {
    public static void main(String[] args) {
        TreeSet set = new TreeSet();
        Person person = new Person("古天乐",18);
        set.add(person);
        set.add(new Person("胡歌",22));
        set.add(new Person("刘亦菲",11));
        set.add(new Person("周星驰",16));
        set.add(new Person("李连杰",56));

        System.out.println("set:"+set);
        System.out.println("返回严格大于给定键值的最小键值:"+set.higher(person));
    }
}

结果

set:[Person{name='刘亦菲', age=11}, Person{name='周星驰', age=16}, Person{name='古天乐', age=18}, Person{name='胡歌', age=22}, Person{name='李连杰', age=56}]
返回严格大于给定键值的最小键值:Person{name='胡歌', age=22}

public boolean isEmpty():判断集合是否为空

public Iterator<E> iterator() :迭代输出

demo

public class Test {
    public static void main(String[] args) {
        TreeSet set = new TreeSet();
        Person person = new Person("古天乐",18);
        set.add(person);
        set.add(new Person("胡歌",22));
        set.add(new Person("刘亦菲",11));
        set.add(new Person("周星驰",16));
        set.add(new Person("李连杰",56));

        Iterator iterator = set.iterator();
        while(iterator.hasNext()){
            System.out.println(iterator.next());
        }
    }
}

结果

Person{name='刘亦菲', age=11}
Person{name='周星驰', age=16}
Person{name='古天乐', age=18}
Person{name='胡歌', age=22}
Person{name='李连杰', age=56}

public E last():获取最后的值

demo

public class Test {
    public static void main(String[] args) {
        TreeSet set = new TreeSet();
        Person person = new Person("古天乐",18);
        set.add(person);
        set.add(new Person("胡歌",22));
        set.add(new Person("刘亦菲",11));
        set.add(new Person("周星驰",16));
        set.add(new Person("李连杰",56));


        System.out.println("set:"+set);
        System.out.println("获取最后的值:"+set.last());
    }
}

结果

set:[Person{name='刘亦菲', age=11}, Person{name='周星驰', age=16}, Person{name='古天乐', age=18}, Person{name='胡歌', age=22}, Person{name='李连杰', age=56}]
获取最后的值:Person{name='李连杰', age=56}

public E lower(E e):返回严格小于给定键值的最大键值

demo

public class Test {
    public static void main(String[] args) {
        TreeSet set = new TreeSet();
        Person person = new Person("古天乐",18);
        set.add(person);
        set.add(new Person("胡歌",22));
        set.add(new Person("刘亦菲",11));
        set.add(new Person("周星驰",18));
        set.add(new Person("李连杰",56));


        System.out.println("set:"+set);
        System.out.println("获取"+set.lower(person));
    }
}

结果

set:[Person{name='刘亦菲', age=11}, Person{name='古天乐', age=18}, Person{name='胡歌', age=22}, Person{name='李连杰', age=56}]
获取最后的值:Person{name='刘亦菲', age=11}

public E pollFirst():获取第一个值并移除第一个值

demo

public class Test {
    public static void main(String[] args) {
        TreeSet set = new TreeSet();
        Person person = new Person("古天乐",18);
        set.add(person);
        set.add(new Person("胡歌",22));
        set.add(new Person("刘亦菲",11));
        set.add(new Person("周星驰",18));
        set.add(new Person("李连杰",56));


        System.out.println("set:"+set);
        System.out.println("获取第一个值并移除这个值"+set.pollFirst());
        System.out.println("set:"+set);
    }
}

结果:

set:[Person{name='刘亦菲', age=11}, Person{name='古天乐', age=18}, Person{name='胡歌', age=22}, Person{name='李连杰', age=56}]
获取第一个值并移除这个值Person{name='刘亦菲', age=11}
set:[Person{name='古天乐', age=18}, Person{name='胡歌', age=22}, Person{name='李连杰', age=56}]

public E pollLast():获取最后值并移除这个值

demo

public class Test {
    public static void main(String[] args) {
        TreeSet set = new TreeSet();
        Person person = new Person("古天乐",18);
        set.add(person);
        set.add(new Person("胡歌",22));
        set.add(new Person("刘亦菲",11));
        set.add(new Person("周星驰",18));
        set.add(new Person("李连杰",56));


        System.out.println("set:"+set);
        System.out.println("获取最后值并移除这个值"+set.pollLast());
        System.out.println("set:"+set);
    }
}

结果

set:[Person{name='刘亦菲', age=11}, Person{name='古天乐', age=18}, Person{name='胡歌', age=22}, Person{name='李连杰', age=56}]
获取最后值并移除这个值Person{name='李连杰', age=56}
set:[Person{name='刘亦菲', age=11}, Person{name='古天乐', age=18}, Person{name='胡歌', age=22}]

public boolean remove(Object o):移除元素

public int size() :当前set容量

public Spliterator<E> spliterator() : 方法用于拆分set元素,并逐个迭代它们。

demo:链接

public class Test {
    public static void main(String[] args) {
        TreeSet set = new TreeSet();
        Person person = new Person("古天乐",23);
        Person person2 = new Person("杨超越",18);
        set.add(person);
        set.add(person2);
        set.add(new Person("胡歌",22));
        set.add(new Person("刘亦菲",11));
        set.add(new Person("周星驰",18));
        set.add(new Person("李连杰",56));


        System.out.println("set:"+set);
        Spliterator<Person> spliterator = (Spliterator<Person>) set.spliterator();
        spliterator.forEachRemaining(System.out::println);
    }
}

结果

set:[Person{name='刘亦菲', age=11}, Person{name='杨超越', age=18}, Person{name='胡歌', age=22}, Person{name='古天乐', age=23}, Person{name='李连杰', age=56}]
Person{name='刘亦菲', age=11}
Person{name='杨超越', age=18}
Person{name='胡歌', age=22}
Person{name='古天乐', age=23}
Person{name='李连杰', age=56}

public NavigableSet<E> subSet(E fromElement, boolean fromInclusive, E toElement,   boolean toInclusive):返回from到to之间的值,fromInclusive和toInclusive代表是否包含当前值

demo

public class Test {
    public static void main(String[] args) {
        TreeSet set = new TreeSet();
        Person person = new Person("古天乐",23);
        Person person2 = new Person("杨超越",18);
        set.add(person);
        set.add(person2);
        set.add(new Person("胡歌",22));
        set.add(new Person("刘亦菲",11));
        set.add(new Person("周星驰",18));
        set.add(new Person("李连杰",56));


        System.out.println("set:"+set);
        System.out.println("返回age在18-23之间的值"+set.subSet(person2,true,person,true));
    }
}

结果:

set:[Person{name='刘亦菲', age=11}, Person{name='杨超越', age=18}, Person{name='胡歌', age=22}, Person{name='古天乐', age=23}, Person{name='李连杰', age=56}]
返回age在18-23之间的值[Person{name='杨超越', age=18}, Person{name='胡歌', age=22}, Person{name='古天乐', age=23}]

public SortedSet<E> subSet(E fromElement, E toElement):返回from到to之间的值,包含from,不包含to,即【左闭右开)

public SortedSet<E> tailSet(E fromElement):返回>=fromElement值的集合元素

demo

public class Test {
    public static void main(String[] args) {
        TreeSet set = new TreeSet();
        Person person = new Person("古天乐",23);
        Person person2 = new Person("杨超越",18);
        set.add(person);
        set.add(person2);
        set.add(new Person("胡歌",22));
        set.add(new Person("刘亦菲",11));
        set.add(new Person("周星驰",18));
        set.add(new Person("李连杰",56));


        System.out.println("set:"+set);
        System.out.println("返回age在18-23之间的值"+set.tailSet(person2));
    }
}

结果

set:[Person{name='刘亦菲', age=11}, Person{name='杨超越', age=18}, Person{name='胡歌', age=22}, Person{name='古天乐', age=23}, Person{name='李连杰', age=56}]
返回age在18-23之间的值[Person{name='杨超越', age=18}, Person{name='胡歌', age=22}, Person{name='古天乐', age=23}, Person{name='李连杰', age=56}]

public NavigableSet<E> tailSet(E fromElement, boolean inclusive)返回>=fromElement值的集合元素,inclusive=true包含自身,=false就不包含

demo

public class Test {
    public static void main(String[] args) {
        TreeSet set = new TreeSet();
        Person person = new Person("古天乐",23);
        Person person2 = new Person("杨超越",18);
        set.add(person);
        set.add(person2);
        set.add(new Person("胡歌",22));
        set.add(new Person("刘亦菲",11));
        set.add(new Person("周星驰",18));
        set.add(new Person("李连杰",56));


        System.out.println("set:"+set);
        System.out.println("返回age在18-23之间的值"+set.tailSet(person2,false));
    }
}

结果

set:[Person{name='刘亦菲', age=11}, Person{name='杨超越', age=18}, Person{name='胡歌', age=22}, Person{name='古天乐', age=23}, Person{name='李连杰', age=56}]
返回age在18-23之间的值[Person{name='胡歌', age=22}, Person{name='古天乐', age=23}, Person{name='李连杰', age=56}]

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/35004.html

(0)
编程小号编程小号

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注