Java Collection-map

Source: Internet
Author: User

Interviews often have been asked to understand the Java collection, has been stuck in the Java interview answer level, this time finally want to study well.

The collection of Java is generally compared to the array, the array is fixed size and the same array can only hold the same type of data. An array is defined as a set of data of the same type, fixed length, and, once initialized, cannot be changed in length. So if the length is not certain, one method is to initialize the length of the application is large enough, but it is easy to create a waste of memory, the other is to use the collection. There are three main types of Java collections:

    • Set (set), interface collection

    • List, interface collection

    • Map (map), interface map

Note: The iterator iterator is a design pattern that traverses and selects objects in a sequence, and the developer does not need to understand the underlying structure of the sequence. He is a lightweight object, and the cost of creating it is small.

The iterator functionality in Java is relatively simple and can only be moved one way:

(1) Use method iterator () requires the container to return a iterator. The first time you call Iterator's next () method, it returns the first element of a sequence. Note: The iterator () method is an Java.lang.Iterable interface that is inherited by collection. This method is available in the collection interface.

(2) Use Next () to get the next element in the sequence.

(3) Use Hasnext () to check if there are elements in the sequence.

(4) use remove () to delete the newly returned element of the iterator.

Iterator is the simplest implementation of Java iterators, with more functionality for the listiterator of list design, which can traverse the list in two directions or insert and delete elements from a list.

Iterator Applications:
List L = new ArrayList ();
L.add ("AA");
L.add ("BB");
L.add ("CC");
for (Iterator iter = L.iterator (); Iter.hasnext ();) {
String str = (string) iter.next ();
System.out.println (str);
}
/* Iterator for While loop
Iterator iter = L.iterator ();
while (Iter.hasnext ()) {
String str = (string) iter.next ();
System.out.println (str);
}
*/

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/8D/1D/wKiom1iG_LqzejqJAAGML79xtZQ576.png "title=" 1.png " alt= "Wkiom1ig_lqzejqjaagml79xtzq576.png"/>

Take HashMap as an example, HASHMAP data structure see http://blog.chinaunix.net/uid-25304914-id-4858568.html

The hash table consists of an array + chain list, and HashMap is actually implemented as a linear array. HashMap inside the implementation of a static internal class entry, its important properties Key,value,next, the above-mentioned linear array is actually entry[], the contents of the map are stored in entry[].

This article from the "12464580" blog, reproduced please contact the author!

Java Collection-map

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.