java foundation: TreeSet

1.TreeSet Introduction

TreeSet SortedSet interface class is implemented, to ensure that the collection element in the sorted state, added to TreeSet elements will be arranged according to a certain order (natural order or custom order)

2.TreeSet basic usage

For chestnut:
 

import java.util.TreeSet;

public class BaseCharacter {
    public static void main(String[]args){
        TreeSet nums=new TreeSet();
        nums.add(5);
        nums.add(7);
        nums.add(1);
        nums.add(3);
        //按顺序输出
        System.out.println("按顺序输出"+nums);
        //输出第一个元素
        System.out.println("输出第一个元素"+nums.first());
        //输出第二个元素
        System.out.println("输出最后一个元素"+nums.last());
        //返回小于4的子集
        System.out.println("返回小于4的子集"+nums.headSet(4));
        //返回大于2的子集
        System.out.println("返回大于2的子集"+nums.tailSet(2));
        //返回在某个区间中的数字
        System.out.println(nums.subSet(2,6));


    }
}

TreeSet first element may be output, the output may be performed tail data, and can set the interval, the TreeSet filled in a sequential output parameters

 

Natural ordering of 3.TreeSet

3-1: Why can sort TreeSet does it work?

java provides Comparable interface that defines a compareTo method, calls the collection element TreeSet compareTo (Object obj) method to compare between the elements (the method returns an int value), if the return is a positive integer prior to a after more than one, if the return is equal to 0, if the negative integer, larger than the previous one.

The code is a natural (ascending operation)

3-2: natural ordering Notes

* Implement Comparable common class, if a class does not implement the interface, the program will throw an exception

* If you want to go to the proper functioning of TreeSet TreeSet can only add objects of the same type

* Best not to modify the values ​​that have been added to the TreeSet (HashSet and the principle is similar, if modified to the same elements again so severely reduced efficiency)

* TreeSet two equal objects determination criterion is 0 can look through it two objects returned by the overridden method compareTo method:

import java.util.Objects;
import java.util.TreeSet;

/**
 * 自然排序
 * TreeSet会调用元素的comapareTo方法比较元素之间的大小关系 然后将集合元素按照升序排列(自然排序)
 * 在java提供的Comparable接口中提供了compareTo(Object obj)方法,该方法返回一个整数值,如果返回的是0 表面相等
 * 如果返回的是正整数,表明obj1>obj2 如果返回的是负整数 表明obj1<obj2
 * BigDecimal Character Boolean String Date Time
 **如果没有实现Comparable接口 程序抛出异常
 * 如果希望去正常运作TreeSet TreeSet只能添加同一种类型的对象
 *
 * TreeSet判断两个对象相等的标准是,两个对象通过compareTo方法返回0
 * */
public class NatureSort {
    static class Z implements Comparable{
        int age;
        public Z(int age){
            this.age=age;
        }
        @Override
        public int compareTo(Object o) {
            return 1;
        }
    }
    public static void main(String[]args){
        TreeSet set=new TreeSet();
        Z z1=new Z(6);
        //加入元素 虽然两个元素一样 但是通过重写方法 依旧可以进行添加
        set.add(z1);
        set.add(z1);
        ((Z)set.first()).age=9;//修改第一个元素的变量
        System.out.println(((Z)set.first()).age);
        System.out.println(((Z)set.last()).age);



    }
}

In this method, although the added twice z1, but because compareTo method always 1, so adding still like elements. (By changing the value of an element, the second is also modified, so that it was two elements to reference the same object variable)

[But this practice is very dangerous to use only the test of time! ! ]

The custom sort 3.TreeSet

3-1: What is the custom ordering?

By Lambda expressions, Comparator modify the rule (the following example is changed to the ascending descending), to achieve a custom collation

import java.util.TreeSet;

//定制排序
public class MyTreeSort {
    int age;
    public MyTreeSort(int age){
        this.age=age;
    }
    public String toString(){
        return "MyTreeSort[age:"+age+"]";
    }
    public static void main(String[]args){
        //使用正则表达式对Comparator规则进行修改
        TreeSet ts=new TreeSet((o1, o2) -> {
            MyTreeSort m1=(MyTreeSort)o1;
            MyTreeSort m2=(MyTreeSort)o2;
            //具体改变规则
            return m1.age>m2.age? -1:m1.age<m2.age?1:0;
        });
        ts.add(new MyTreeSort(4));
        ts.add(new MyTreeSort(6));
        ts.add(new MyTreeSort(3));
        ts.add(new MyTreeSort(2));
        System.out.println(ts);
    }
}

 

发布了193 篇原创文章 · 获赞 70 · 访问量 12万+

Guess you like

Origin blog.csdn.net/Lzinner/article/details/89489250