Sets can be ordered and unordered, and they can be allowed to repeat and not allowed to repeat.

Collection name Introduction to the remark
ArrayList Array based, fast query, slow add and delete, repeatable, thread insecurity, high efficiency The LinkedList does not need to be expanded when inserting elements
LinkedList Based on linked list, query is slow, add and delete fast, repeatable, thread is not safe, high efficiency Do not loop over elements in the LinkedList
HashSet Unordered, thread unsafe, null values allowed, elements not allowed to repeat
TreeSet Order, thread unsafe, no NULL values allowed, no duplicate elements allowed The default ascending

List inherits Collection, ArrayList and LinkedList implement List.

Set descends from Collection, and HaseSet, TreeSet, and LinkedHashSet are derived from Set.

ArrayList vs. LinkedList performance comparison

It is wrong to say that arrayLists add and delete faster than linkedLists.

/** * code run environment: win10, JDK8, AMD R7 4800H */
public static void main(String[] args) {
        int count = 10000000;
        ArrayList<Integer> array = new ArrayList<>();
        LinkedList<Integer> linked = new LinkedList<>();

        long begin = System.currentTimeMillis();
        for (int i = 0; i < count; i++) {
            array.add(i);
        }
        System.out.println("ArrayList add " + count + " num cost time : " + (System.currentTimeMillis() - begin));

        begin = System.currentTimeMillis();
        for (int i = 0; i < count; i++) {
            linked.add(i);
        }
        System.out.println("LinkedList add " + count + " num cost time : " + (System.currentTimeMillis() - begin));
    }
Copy the code

Running the code, you can see from the console how long it took to add 10000000 parameters to two lists:

ArrayList add 10000000 num cost time : 4255
LinkedList add 10000000 num cost time : 7929
Copy the code

In contrast, ArrayLists add and delete more efficiently than LinkedLists.

If inserted from the header, a LinkedList is theoretically more efficient than an ArrayList. LinkedList provides the addFirst method, which is fast. ArrayList also involves copying and scaling.

Comparing the efficiency of LinkedList and ArrayList requires a combination of number and location (head, tail, middle).

LinkedList and ArrayList who fast can refer to: www.cnblogs.com/xiaofuge/p/…

HashSet and TreeSet

How does a HashSet keep elements in a collection from repeating themselves?

The HashSet gets the object’s HashCode, which determines where the element is stored. If two objects are equal, obja.equals (ObjB), but the hashcodes of ObjA and ObjB are not equal, they can also be stored in the same HashSet.

/** * construct object, override equals method, make two objects equal but hashCode is not equal */
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class CommonBeanReWrite {
    private Integer integer;
    private Long aLong;

    @Override
    public boolean equals(Object obj) {
        CommonBeanReWrite commonBean = (CommonBeanReWrite) obj;
        return this.aLong.equals(commonBean.getALong()); }}// Test the code
private void testHashSetElementUnique(a) {
        HashSet<CommonBeanReWrite> hashSet = Sets.newHashSet();
        CommonBeanReWrite bean1 = CommonBeanReWrite.builder().integer(1).aLong(1L).build();
        CommonBeanReWrite bean2 = CommonBeanReWrite.builder().integer(2).aLong(1L).build();
        System.err.println("Are two objects equal:" + bean1.equals(bean2));
        System.err.println("Object 1 hashCode:" + bean1.hashCode());
        System.err.println("Object 2 hashCode:" + bean2.hashCode());

        hashSet.add(bean1);
        hashSet.add(bean2);

        System.out.println("Number of elements in a hashSet:" + hashSet.size());
    }
Copy the code

Run the code and observe the console output

Whether two objects are equal:trueNumber of elements in a hashSet:2object1The hashCode:263025902object2The hashCode:438135304
Copy the code

Two objects are equal (a.equals (B)), but HashCode is not, so they can be stored together in the same HashSet.

TreeSet sorting

TreeSet is in ascending order by default, but you can specify the sorting mode when creating a TreeSet.

// Default ascending order
private void testTreeSetDefaultSort(a) {
        TreeSet<Integer> treeSet = Sets.newTreeSet();
        treeSet.add(100);
        treeSet.add(50);
        treeSet.add(200);
        treeSet.add(150);
        treeSet.forEach(item -> System.out.print(item + "\t"));
    }
Copy the code

The console output shows that TreeSet’s default sorting mode is ascending

50	100	150	200
Copy the code

TreeSet can also set custom collation rules,

// Set descending order
private void testTreeSetCustomizeSort(a) {
        TreeSet<Integer> treeSet = new TreeSet<>((o1, o2) -> {
            if(o1.equals(o2)){
                return 0;
            } else if (o1 > o2) {
                return -1;
            } else {
                return 1; }}); treeSet.add(100);
        treeSet.add(50);
        treeSet.add(200);
        treeSet.add(150);
        treeSet.forEach(item -> System.out.print(item + "\t"));
    }
Copy the code

After running, you can see from the console output that TreeSet is stored in a custom sorting manner

200	150	100	50
Copy the code