51. TreeSet

Collection classification:
-------------------| Collection The root interface of a single-column collection   
---------------| List If List is implemented The collection class of the interface has the following characteristics: orderly and repeatable
-----------| ArrayList The bottom layer of ArrayList maintains an array of Object type, which is characterized by: fast query, slow addition and deletion.
Usage scenarios: If the current data query is large and the addition and deletion are few, then use ArrayList to store data
. Note: When using the ArrayList no-parameter construction method, the default length of Object is 10, and if it is not enough, it will automatically increase by 0.5 times
------------- | LinkedList The bottom layer of LinkedList is implemented using the linked list data structure. The characteristics are: slow query, fast addition and deletion.
Usage scenario: When there are few data queries and many additions and deletions, use LinkedList to store

---------------| Set If the collection class that implements the Set interface, it has The characteristics are: unordered, non-repeatable
Note: The Set interface does not have a get() method, the iterator is the general method of traversing the collection, so we'd better use the iterator to traverse
-------------| HashSet

The implementation principle of HashSet:
    When adding elements to HashSet, HashSet will automatically call the hashCode method of the element to obtain the hash table of the element,
    and then through the hash value of the element through the shift and other operations, the element in the hash table can be calculated. Storage location
    
    in case 1: If the storage location of an element is calculated and there is no element at present, then the element can be directly stored in this location
    
    Case 2: If there are other elements in the storage location of the calculated element, the equals method will be automatically called to compare again.
    If equals returns true, it is regarded as a duplicate element and cannot be added. If it returns false, then you can add
    
    Note: hashCode and The equals method is called automatically, not by us manually (note the difference between the List interface and the Set implementation principle)
    
-------------| ThreeSet

ThreeSet Notes:
    1.ThreeSet is adding elements At the time, if the data has natural ordering rules, then it is sorted and stored according to the characteristics of the natural order of
    elements. 2. When adding elements to ThreeSet, if the data does not have natural ordering rules, the class to which the element belongs must implement the Comparable interface, and
    the elements The comparison rules are written on the compareTo method.
    3. When adding elements to ThreeSet, if the element itself does not have natural data characteristics, and the element does not implement the Comparable interface,
    then a comparator must be passed in when ThreeSet is created.
    4. If When comparing elements, calling compareTo returns 0, then the element is regarded as a duplicate element and cannot be added (note: it has nothing to do with hashcode and equals)

 

Natural characteristics:

    For example, numbers , letters , etc., because they all implement the Comparable interface, we don't need to define the sorting rules ourselves.

 

String comparison rules:

    Case 1: There are different characters in the corresponding position, then the characters in the corresponding position are compared

    Case 2: The same character appears in the corresponding position, then the length of the character is compared


比较器定义规则:
    自定义一个类,实现Comparable接口,把比较规则写在compareTo方法中

    注意:ThreeSet是在排好序再进行存储的,并不是在输出的时候再排序输出的

 

推荐使用:比较器,这样提高了代码的重用性

ThreeSet的存储原理:
    底层是使用红黑数(二叉树)数据结构实现的     储存规则:左小右大

首先第一个是老钟,因为没有比较的对象,所以老钟排在最顶端

第二是老陆先跟老钟比,比老钟小,所以排在老钟的左边

第三是老汤先跟老钟比,比老钟大,所以排在老钟的右边

第二是老黎先跟老钟比,比老钟大,所以排在老钟的右边,再跟老汤比比老汤大所以在老汤的右边

最后的结果是:老陆 老钟  老汤  老黎

注意:如果三个数据还没有构成二叉树数据结构,那么就会自动调增为二叉树数据结构

也就是:

  先添加老陆后添加老钟那么老钟在老陆右边

  再添加老汤,老汤比老陆和老钟都大,再老钟右边

  这时就是没有构成二叉树数据结构,那么它会自动调增为

  老钟在最上面,老陆在左边,老汤在右边

 

不具备自然规则的实例:

 

class People {
    int id;
    String name;
    
    public People(int id , String name) {
        this.id = id;
        this.name = name;
    }
    @Override
    public String toString() {
        
        return "{ 身份证"+this.id+" 姓名:"+this.name+" }";
    }
}

class PeopleComparator implements Comparator{

    @Override
    public int compare(Object o1, Object o2) {
        People p1 = (People)o1;
        People p2 = (People)o2;
        return p1.id-p2.id;
    }

    
    
}
public class Demo2 {
    public static void main(String[] args) {
        PeopleComparator peocom = new  PeopleComparator();
        TreeSet set = new TreeSet(peocom);
        set.add(new People(1003,"狗蛋"));
        set.add(new People(1001,"狗娃"));
        set.add(new People(1002,"老李"));
        //添加一个身份证一样的,姓名不一样的人
        set.add(new People(1001,"老八"));
        //使用迭代器遍历
        Iterator it =  set.iterator();
        while(it.hasNext()) {
            System.out.println(it.next());
        }
    }

}

 

 

 

具备规则的实例:

public class Demo2 {
    public static void main(String[] args) {    
        TreeSet set = new TreeSet();
        set.add(10);
        set.add(5);
        set.add(9);
        TreeSet set1 = new TreeSet();
        set1.add("b");
        set1.add("c");
        set1.add("a");
        //使用迭代器遍历
        Iterator it =  set.iterator();
        while(it.hasNext()) {
            System.out.print(it.next()+",");
        }
        System.out.println(" ");
        Iterator it1 =  set1.iterator();
        while(it1.hasNext()) {
            System.out.print(it1.next());
        }
    }
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325320281&siteId=291194637