hashcode method in java and Set List On the difference in the difference between Java and Java in the Vector of ArrayList

 a. Characteristics

  Two interfaces are inherited from the Collection, is a collection of commonly used to store data items, the main differences are as follows:

      ① between List and Set very important difference is whether to allow duplicate elements exist, allowing the insertion of duplicate elements in the List, but does not allow duplicate elements exist in the Set.

② and has storage elements related to the sequence, List is an ordered set, the order will be retained when the element is inserted, Set unordered collection.

③ List can be accessed through the index, but not Set.

b. a common implementation class

(1) List Interface

  Common implementation class as follows:

ArrayList (Array implementation) : allows fast random access to the elements, from the intermediate position ArrayList insert or delete elements of the array needs to be copied, moved, the cost is relatively high. Therefore, it is suitable for random search and traversal, not suitable for insertion and deletion.

Vector (array implementation) : Support for thread synchronization, only one thread at a time can write Vector, avoid multi-threading while writing the inconsistency caused, but synchronization requires a very high cost, therefore, access it slower than accessing ArrayList . Vector belong thread-safe level, but in most cases without the use of Vector, because of the need for greater security thread overhead (associated with ArrayList method is very similar, with synchronized modification in the method).

We find that when the array is not large enough to re-establish the array elements then copied to the new array, the ArrayList (+ 1 for 1.5 times) , and Vector (2-fold) different array size expansion.

  LinkedList (list implementation) : It is suitable for dynamic insertion and deletion of data, random access and traverse speed is slower. Also provides a process not defined List interface, designed for operation header and tail elements, can be used as stacks, queues, queue and bidirectional use.

(2) Set Interface

 Common implementation class has HashSet, TreeSet and LinkedHashSet:

  HashSet : When stored in a binding element to the HashSet, HashSet will invoke the object's hashCode () method to get the value of the object hashCode, hashCode value then determined according to the object storage locations in the HashSet (Why how HashSet guarantees of non-repetition). That is, determination HashSet set equal to two standard elements equals method by comparing two objects are equal, and the two objects hashCode () method returns the values are equal. Can not guarantee the order of the elements, the order may change; collection elements may be null, but only into a null;

  A LinkedHashSet : set a LinkedHashSet same storage location is determined according hashCode element value of the element, but it also used to maintain order in the list of elements. Such that the insertion elements look like in order to save, that is, when traversing the set time, will be to add a LinkedHashSet sequential access of elements of the set of elements. LinkedHashSet when iteration access to all elements of Set, the performance is better than HashSet, but when you insert performance slightly inferior to HashSet.

TreeSet  : TreeSet SortedSet is the only class that implements the interface, the underlying data structure is a red-black tree, TreeSet collection element can be ensured in the sequencing state. TreeSet supports two sort of natural sorting and custom ordering in which the natural order is the default sort order, the following example:

Natural ordering - to be sorted using the sort of natural elements CompareTo (Object obj) method to compare the magnitude relationship between elements, and the elements are arranged in ascending order.

Custom Sort - natural ordering is based on the size of the collection of elements, in ascending order, if you want to customize the ordering Comparator interface should be used to achieve int compare (T o1, T o2) Method

 The following example:

package javase.collection;

import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;


class Person implements Comparable<Person>{
    private int age;
    private String name;
    public Person(String name,int age) {
        this.name = name;
        this.age = age;
    }
    public String toString() {
        return  "[" + name + " : " + age + "]";
    }
    public int getAge() {
        return age;
    }
    public String getName() {
        return name;
    }
    @Override
    public int compareTo(Person person) {
        //返回0表示相等,则不加入TreeSet
    @Override
    }
}
public class CollectionTest {
    public static void main(String[] args) {
//        TreeSet<Person> psets = new TreeSet<>(new MyComp());
//        psets.add(new Person("Bob",23));
//        psets.add(new Person("Alice",36));
//        psets.add(new Person("Tom",18));
//        Iterator<Person> it = psets.iterator();
//        while (it.hasNext()){
//            System.out.println(it.next());
//        }
        TreeSet<Person2> psets = new TreeSet<>(new MyComp());
        psets.add(new Person2("Bob",23));
        psets.add(new Person2("Alice",36));
        psets.add(new Person2("Tom",18));
        Iterator<Person2> it = psets.iterator();
        while (it.hasNext()){
            System.out.println(it.next());
        }
    }
}

c. interconversion

  Because the List and the Set have achieved addAll Collection interface (Collection <? Extends E> c) method, it is possible to use the addAll () method List and the Set mutual conversion; Further, List and Set also provides Collection <extends E?> c as a constructor parameter, it is often in the form of complete constructor interconversions.

//List转Set
Set<String> set = new HashSet<>(list);
//Set转List
List<String> list1 = new ArrayList<>(set);

 

Reference material

1. the Java Array, List, the Set of mutual transformation

2.  On the hashcode method in Java

3. The  difference in Java ArrayList and the Vector

Guess you like

Origin www.cnblogs.com/helloworldcode/p/12122072.html