Collections and Arrays

Source: Internet
Author: User
Tags set set

First, let's look at the concept.

Array: A container for storing objects (which can store basic data types), fixed in length, and not suitable for situations where the number of objects is unknown.

Collections: Containers that can hold multiple object types (objects only), variable in length.

The difference between an array and a collection

1. The array declares the data type, and then only the data type is stored. A collection can be stored in multiple (non-generic, type is Object).

2, the array is static, an array instance has a fixed size, once created can not change the capacity. Collections are dynamically scaled capacity and can be dynamically resized as needed.

3. Arrays are the data types built into the Java language and are linear, with the fastest execution efficiency or type checking.

The collection can only hold objects, how is the basic data type resolved?

You can use the wrapper class to convert the base data type to the object type and hold the reference. Because of the auto-boxing and auto-unpacking function, I want to put the data type into the collection and save it directly. It is automatically boxed into a wrapper class and then put into the collection. (if generics are not specified, all objects in the collection are of type Object)

So the reason for using arrays is that arrays are faster than collections? The reason for using collections is that you can store multiple types of objects?

Generally used ArrayList more, it is based on the array, but also implemented the collection and list interface. Can be said to be combined with these two.

Collection class System

Single-Column collection

Collection
├list (ordered set, allowing the same element and NULL)
│├linkedlist (non-synchronous (thread insecure), allow the same element and null, and delete and change the fast check to slow)
│├arraylist (non-synchronous (thread insecure), allows the same element and NULL, implements a dynamic size array, and delete and change the slow to find faster)
│└vector (Synchronous (thread safe), allowing the same element and null, inefficient)
│└stack (inherits from Vector, implements a last-in-first-out stack)
└set (unordered collection, not allowing the same element, with up to one null element)

|-hashset (unordered collection, not allowing the same element, with up to one null element)

Two-column collection
Map (does not implement collection interface, key cannot be duplicated, value can be repeated, a key maps a value)
├hashtable (Implement the Map interface, synchronize (thread safe), do not allow NULL as key and value, use a custom class as key if you want to replicate the Hashcode and Eques methods,)
├hashmap (Implement map interface, non-synchronous (thread insecure), allow NULL as key and value, use more)
└weakhashmap (Implement Map interface)

The Collection interface is the root interface of the collection class, and there is no direct implementation class in Java that provides this interface. But let it be inherited produced two interfaces, that is, set and list. The set cannot contain duplicate elements. A list is an ordered collection that can contain duplicate elements that provide a way to access by index.

the difference between collection and collectionscollection is the ancestor interface of the collection class, and the interfaces that inherit from him are mainly list and setcollections is a wrapper class that contains a variety of static polymorphic methods related to set operations. This class cannot be instantiated, just like a tool class that serves the Java collection framework.

Map is another interface in the Java.util package that is not related to the collection interface and is independent of each other, but is part of the collection class. Map contains the key-value pair. A map cannot contain duplicate keys, but it can contain the same value.

Iterator, all of the collection classes implement the Iterator interface, which is an interface for iterating through the elements in the collection. (You cannot delete a collection element when using For/foreach loop traversal)

The following three methods are mainly included:

①.hasnext () whether there is a next element.
②.next () returns the next element.
③.remove () deletes the current element.

  What is the difference between Iterator and listiterator?

Iterator can be used to traverse the set and list collection, but Listiterator can only traverse the list.

Iterator to a collection can only be forward traversal, listiterator can be forward or back.

The Listiterator implements the Iterator interface. and extended other functions (such as adding elements, replacing elements, getting the previous index of the last element, etc.)

1. List (ordered, repeatable)
List of objects are ordered, but also can be repeated, list is concerned about the index, has a series of index-related methods, query fast. Since inserting or deleting data into the list collection is accompanied by the movement of the subsequent data, all insertions are slow to delete data.

  The difference between ArrayList and LinkedList

LinkedList often used in the case of more additions and deletions and very few query operations, ArrayList is the opposite.

LinkedList is a linked list structure, when the query is moving the pointer one by one to find the past, relatively slow, and increase only need to connect up on it.

ArrayList is the array structure, the query is random query according to the index, the comparison is fast, and the additions and deletions need to update the index, so slower.

If you insert or delete only a single piece of data, the ArrayList speed is better than LinkedList. But if bulk random insert deletes data, LinkedList speed is much better than ArrayList. Because ArrayList each insertion of data, you move the insertion point and all subsequent data.

2. Set (unordered, cannot repeat)
The objects stored in the set are unordered and cannot be duplicated, and the objects in the collection are not sorted in a particular way, simply by adding the object to the collection.

  How can a set not be added to a duplicate value?

The Add () method of the set is the map.put (key,value) method, and the object we add is placed in the location of the key in the map.

The key of the map cannot be duplicated. This is by rewriting the hashcode () and Equals () methods.

3. MAP (key value pair, key unique, value not unique)
The Map collection stores key-value pairs, keys cannot be duplicated, and values can be duplicated. According to the key to get the value, the map set to traverse the first get the set set of keys, the set set to traverse, to get the corresponding value.

Hashtable and HashMap
Synchronization: Hashtable is thread-safe, that is, synchronous, and HashMap is not a secure line program, not synchronous.
HashMap allows for the existence of a null key, with multiple null value.
Both the Hashtable key and value are not allowed to be null.

Finally, let's focus on ArrayList.

It is actually a dynamic array that provides dynamic increment and decrease elements

Implement the collection and list interface, you can set the size of the array flexibly.

Threads are not secure, so it is generally recommended to use ArrayList in a single thread.

ArrayList is an array-based, fixed length (default is 10 when not set), what if the data to be populated exceeds the length? A: ArrayList will increase the length, the bottom layer will create a new array, the length is newlength = Oldlength + OLDLENGTH/2, if the initial value is 10, then 15,22,33,49,73......

Collections and Arrays

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.