Values and collections in Java

Source: Internet
Author: User

The difference between arrays array and collection:

(1) The value is fixed, the same array can only hold the same data.

(2) Java collections can hold a set of data that is not fixed

(3) If the program does not know exactly how many objects need to automatically expand capacity when the space is insufficient, you need to use the Container class library, array does not apply

The array is converted to a collection:

Arrays.aslist (Array)

Example:

Int[] arr = {1,3,4,6,6}; Arrays.aslist (arr); for (int i=0;i<arr.length;i++) {System.out.println (arr[i]);}

Collection Conversions to arrays:

Collection. ToArray ();

Example:

List List = new ArrayList (); List.add ("a"); List.add ("B"); List.toarray (); System.out.println (List.tostring ());

I. Architecture of the collection:

list, set, and map are the three most important interfaces in this set system. The list and set inherit from the collection interface. The map also belongs to the collection system, but differs from the collection interface.

Set does not allow elements to be duplicated. HashSet and TreeSet are the two main implementation classes. Set can only be evaluated by a cursor, and the value cannot be duplicated.

The list is ordered and allows elements to be duplicated. ArrayList, LinkedList, and vectors are the three main implementation classes. ArrayList is thread insecure, Vector is thread-safe, these two classes are implemented by arrays of LinkedList is thread insecure, the underlying is implemented by the linked list

Map is a collection of key-value pairs. Where the key column is a collection, key cannot be duplicated, but value can be repeated. HashMap, TreeMap, and Hashtable are the three main implementation classes of map. HashTable is thread-safe and cannot store null values HASHMAP is not thread safe and can store null values

Two. The difference between list and ArrayList

1.List is an interface, and the list feature is ordered, ensuring that elements are preserved in a certain order.

ArrayList is its implementation class, which is a list implemented with arrays.

Map is an interface, and the map attribute is to find objects based on an object.

HashMap is its implementation class, HashMap is implemented with a hash table map, is to use the object's Hashcode (Hashcode () is the method of object) for fast hash lookup. (for hash lookup, see << data structure >>)

2. In general, if not necessary, the recommended code only deals with the List,map interface.

For example: List list = new ArrayList ();

The reason for this is that the list is equivalent to a generic implementation, and if you want to change the type of the list, you only need to:

List List = new LinkedList ();//linkedlist is also the implementation class of the list, and also the brother class of ArrayList

In this way, there is no need to modify other code, which is the elegance of interface programming.

Another example of this is the following declaration in the method of the class:

private void Domyaction (List list) {}

This method can handle all the classes that implement the list interface, and implement generic functions to some extent.

3. If you feel that Arraylist,hashmap performance does not meet your needs when developing, you can customize your custom class by implementing List,map (or collection).

Values and collections in Java

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.