Java data array and collection list, set, map

Source: Internet
Author: User
Tags set set

The array,list in Java has not been known before. At the same time the use of set,map,list completely confused, until the reading of this article, explained very clearly.

There is no collection in the world, (only the array reference C language) but someone wants it, so there's a set of people who  want to have an array that can be automatically expanded, so there's a list of  people who want to have no duplicate arrays, so there's set  someone who wants to have the number of automatically sorted groups, So with treeset,treelist,tree**    , almost all of the collections are based on arrays.  Because a collection is an array of arrays, it is always faster than any collection,    but any collection is one more than the array provides    : The array declares the type of the element it holds, and the collection does not declare it. This is because the collection stores their elements in object form.    Two: An array instance has a fixed size and cannot be scaled. The collection can dynamically change the size as needed.    Three: An array is a readable/writable data structure--there is no way to create a reading group. However, the collection can be used in a read-only manner using the ReadOnly method provided by the collection. The method returns a read-only version of a collection.

The above text clearly explains the relationship between the data and the set, as well as the characteristics of the various collections.

Here is my simple understanding:

The most basic is the array, and all the collections are implemented by array.

With random access and storage in the code, array is the most efficient, but the array is fixed and cannot be changed dynamically, and an array can only hold the same data type. For the above shortcomings, there is a set is List,set,map.

A Java collection can store and manipulate a set of data that is not fixed, but can only hold data of a reference type and cannot be placed on a base data type.

The Java collection is located in Java.util.

--------------------------------------------------------------------------------------------------------------- --------------------------------------------------------------------------

Arrays: Array

Collection:

Collection:set (set), List (lists)

Map (map): Hashmap, Hashtable

Method of array:

1 equals (): Compares two arrays for equality. Array has the same number of elements, and all corresponding element 22 is equal.  2fill (): Fills the value into the array.  3sort (): Used to sort the array.  4binarysearch (): Looks for elements in a sorted array.  5 system.arraycopy (): Copy of Array.

Collection interface:

Collection implements the most basic interface for a collection, declaring a common approach to Java collections (set and list)

Collection interface methods: (Set and list also have the following method)

12 3 456   78 object[] ToArray (): Returns an array that includes all the elements in the collection

Iterator () and ToArray () are all elements in the Operation collection, iterator () returns a Iterator object, and ToArray () returns an array of all the collections

The iterator interface declares the following methods:

1 Hasnext (): Determines whether the elements in the collection are traversed, and if not,    returns True 2 Next (): Returns the next element    3 Remove (): Removes the last element returned from the collection with the next () method.  

1.set (Collection)

Set is the simplest collection in which objects in the collection are not duplicated and are not sorted in a particular way.

The set interface mainly implements two implementation classes:

The Hashset:hashset class accesses the objects in the collection according to the hashing algorithm, and the access speed is relatively fast.

The Treeset:treeset class implements the SortedSet interface and is able to sort the objects in the collection.

Set usage: Holds reference to object, no duplicate object

1Set set=NewHashSet (); 2String s1=NewString ("Hello");//reference type-object, cannot be the underlying data type3String s2=S1; 4String s3=NewString ("World"); 5 Set.add (S1); 6 set.add (S2); 7 Set.add (S3); 8System.out.println (Set.size ());//the number of objects in the Print collection is 2. 

How does the Add () method of set determine if an object has been stored in the collection?

1 Booleanisexists=false; 2Iterator iterator=Set.iterator (); 3  while(It.hasnext ())4 {  5String oldstr=It.next (); 6     if(Newstr.equals (oldstr)) {7isexists=true; 8     }  9}

function method of Set


The set has exactly the same interface as the collection, so there is no additional functionality, and the set is actually collection, but behaves differently. (This is a typical application of inheritance and polymorphic thinking: behave differently) set does not save duplicate elements
Set: Each element that is stored in the set must be unique because set does not save duplicate elements. The element that joins the set must define the Equals () method to ensure the uniqueness of the object. The set has exactly the same interface as the collection. The set interface does not guarantee the order in which elements are maintained.
HashSet: Set for quick find design. The object that is deposited into the hashset must define HASHCODE ().
TreeSet: The set of the save order, the underlying tree structure. Use it to extract ordered sequences from the set.
Linkedhashset: has hashset query speed, and internally uses the chain list to maintain the order of elements (the Order of insertions). The result is displayed in the order in which the elements are inserted when iterating through the set using Iterators.

2.list (list)

A list is characterized by its elements being stored in a linear fashion, in which duplicate objects can be stored in the collection.

The main implementation classes of the list interface include: (ArrayList and LinkedList)
ArrayList (): Represents the length can be changed by the group. Elements can be randomly accessed, and it is slow to insert and delete elements into ArrayList ().
LinkedList (): A linked list data structure is used in the implementation. Fast insertion and deletion, and slow access times.


For random access to a list, it is just random to retrieve the element at a particular location. The Get (int index) method of the List returns the object in the collection at the index location specified by the parameter index, starting with "0". The two most basic ways to retrieve all objects in a collection are:

For loop and Get () method:

1  for (int i=0; i<list.size (); i++) {  2System.out.println (List.get (i));   3

Using Iterators (Iterator):

1 Iterator it=list.iterator ();   2  while (It.hashnext ()) {  3System.out.println (It.next ());    4 }  

There are actually two kinds of lists: One is the basic ArrayList, the advantage is the random access element, the other is the more powerful linkedlist, it is not for the fast random access design, but has a more general method.

    • List: Order is the most important feature of the list: it ensures that the elements are maintained in a specific order. The list adds a number of methods to collection, allowing you to insert and remove elements to the middle of the list (this is recommended for linkedlist use only. A list can generate Listiterator, which can be used to traverse a list in two directions or to insert and remove elements from the middle of a list.
    • ArrayList: A list implemented by an array. Allows fast random access to elements, but inserts and removes elements in the middle of the list is slow. Listiterator should only be used to traverse the ArrayList backward, not to insert and remove elements. Because that's much more expensive than linkedlist.
    • LinkedList: Sequential access is optimized, and the cost of inserting and deleting to the list is small. Random access is relatively slow. (use ArrayList instead.) ) also has the following methods: AddFirst (), AddLast (), GetFirst (), GetLast (), Removefirst (), and Removelast (), which are not defined in any interface or base class Enables LinkedList to be used as stacks, queues, and bidirectional queues.

3.MAP (map)

Java data array and collection list, set, map

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.