Java Collection-Collection

Source: Internet
Author: User

Java Collection-Collection

Repeat the Java Collection knowledge, which is frequently involved in interviews and project development for record.

 

Declaring an array is to draw a series of consecutive spaces in the memory space. Arrays are a commonly used data structure. However, in project development, a set is required because of some uncertainties. For example:

1. The size of the array is fixed. array does not support capacity expansion;

2. the array element type is the same data type. Different data type elements can be stored in the set.

At the same time, Array and Collection are correlated. The corresponding toArray () and Arrays. asList () methods can be used to recall the conversion. Collection can be understood as a dynamic array. The difference is that the content in the Collection object can be expanded at will.

<1> Collection System

List, Set, and Map are three important interfaces in the Collection system. List and Set have similarities. They all inherit from the Collection interface and are a Set of single-column elements. Map is a Set of double-row elements.

 

<1.1> List Interface
The API writes: List is an ordered Collection, which can be used to precisely control the insert position of each element. You can use an index (the position of an element in the List, similar to an array subscript. The subscript starts from 0) to access the elements in the List, which is similar to an array in Java. List in Java is an effective extension of data. If you do not use generics during creation, you can accommodate any type of elements. If you use a List, you can only accommodate elements of the generic type.

Unlike the Set mentioned below, the List can have the same elements and the element is allowed to be null. An object can be repeatedly stored in the List set. Every time the Add () method is called, this object is inserted into the set. In fact, an index variable is used in the set to point to this object. In addition to getting all elements using the Iterator interface, you can also use get (index I) to specify the first element.
Common classes that implement the List interface include the List, ArrayList, Vector, and Stack.

                List list = new ArrayList() ;Random random = new Random() ;// add random elements to the list;for(int temp =0;temp<5;temp++){list.add(new Integer(random.nextInt(10))) ;}System.out.println(list);Iterator it = list.iterator() ;//iterator the list elements;while(it.hasNext()){System.out.print(it.next()+--);}

<1.1.1> sort list class
The listlist interface allows null elements. In addition to the List interface, the revoke List class also provides a unified naming method for get, remove, and insert elements at the beginning and end of the List. These operations allow the link list to be used as a stack, queue, or dual-end queue.

Note that the synchronized list method is not available. If multiple threads access a List at the same time, they must implement access synchronization by themselves. One solution is to construct a synchronized List when creating a List:
List list = Collections. synchronizedList (new Collections List (...));

<1.1.2> ArrayList class
ArrayList implements an array of variable sizes. ArrayList provides an optional list operation that allows all elements, including null. Like the synchronized list, ArrayList is also non-synchronous (unsynchronized ).

<1.1.3> Vector class
The Vector class can implement a scalable array of objects. Like an array, it contains components that can be accessed using integer indexes. However, the Vector size can be increased or reduced as needed to meet the needs of adding or removing items after the Vector is created. The difference is that the Vector is synchronized by threads.

<1.1.4> Stack class
Stack inherits from Vector to implement a post-import, first-out Stack. Stack provides five additional methods to make the Vector used as a Stack. The basic push and pop methods also include the elements of the peek method to get the top of the stack. The empty method tests whether the stack is empty. The search method checks the position of an element in the stack. The Stack is empty after being created.

<1.2> Set Interface

Set is a Collection that does not contain repeated elements, that is, the two elements e1 and e2 both have e1.equals (e2) = false. Set has a maximum of null elements. Obviously, the Set constructor has a constraint that the imported Collection parameter cannot contain duplicate elements. Set can be roughly divided into two categories: unordered Set and sorted Set. unordered Set includes HashSet and sorted HashSet. Sorted Set mainly refers to TreeSet. HashSet and javashashset can contain null.

<1.3> Map interface

Map stores "key-value pairs". Similar to Set, Map in Java also has two types: sorted and unordered, unordered tasks include HashMap and Hashtable, and sorted tasks include TreeMap and LinkedHashMap.

LinkedHashMap is a subclass of HashMap. It retains the insert sequence. If the output sequence is the same as the input sequence, LinkedHashMap is used.

 

Many topics in the interview

1. Differences between HashMap and Hashtable

All belong to the Map interface class, which maps the unique key to a specific value.

(1). Historical Reasons:

Hashtable is based on the obsolete Dictionary class, And HashMap is an implementation of the Map interface introduced by Java 1.2.

(2). Synchronization:

Hashtable is thread-safe, that is, synchronous, while HashMap is thread-safe, not synchronous

(3). value:

The HashMap class is not classified or sorted. It allows a null key and multiple null values.

Hashtable is similar to HashMap, but does not allow null keys and null values.

(4). Efficiency:

Hashtable is slower than HashMap because it is synchronized.

How to synchronize HashMap

HashMap can achieve synchronization through Map m = Collections. synchronizedMap (hashMap.

2. Differences between ArrayList and Vector

ArrayList and Vector are mainly used in two aspects.

(1). Synchronization:

Vector is thread-safe, that is, synchronous, while ArrayList is not secure or synchronous.

(2). Operation:

Because Vector supports multi-threaded operations, its performance is inferior to that of ArrayList.

(3). Data growth:

Both ArrayList and Vector have an initial capacity. When the number of elements stored in them exceeds the capacity, you need to increase the storage space of ArrayList and Vector, each time a bucket is added, not only one storage unit is added, but multiple storage units are added.

By default, Vector is doubled, and ArrayList is increased by 0.5 times by default.

Vector can be set by ourselves. ArrayList does not provide relevant methods.

3. What is the difference between the rule list and ArrayList?

Both implement the List interface. The difference is:

(1) ArrayList is implemented based on dynamic arrays, And the ArrayList is based on the linked table data structure.

(2) When get accesses any element in the List, ArrayList has better performance than javaslist. The get method in the listing list is to check from one end of the list in sequence until the other end

(3) For the add and delete operations, the revoke list is better than the ArrayList, because the ArrayList needs to move data.

Additional:

The listlist interface allows null elements. In addition, the values list provides additional get, remove, and insert methods at the beginning or end of the values list. These operations enable the queue list to be used as a stack, queue, or two-way queue (deque ).

Note that the synchronized list method is not available. If multiple threads access a List at the same time, they must implement access synchronization by themselves. One solution is to construct a synchronized List when creating a List:

List list = Collections. synchronizedList (new Collections List (...));

 

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.