Java Collection Interview Summary __java

Source: Internet
Author: User
Java Collection interview summary

★★★★★ Collection Framework: a container for storing data.

Characteristics:

1: Object encapsulation data, more objects need to store. Collection is used to store objects.

2: The number of objects determines that an array can be used, but not sure what to do. You can use a collection. Because the collection is variable length.

Collection and Array differences:

1: The array is of fixed length, and the set is variable in length.

2: Arrays can store basic data types, or they can store reference data types, and collections can store only reference data types.

3: The elements stored by the array must be of the same data type; The collection can store objects of different data types.

Data structure: Is the way in which a container is stored.

There are many kinds of collection containers. Because each container's own characteristics are different, in fact, the principle is that each container's internal data structure is different.

The collection container is in the process of being extracted continuously upwards. There is a set system.

When using a system, the principle: refer to the top level content. Create the underlying object.


1 Iterator Interface 1.1 iterator

< Java.util >--iterator: is an interface-iterator interface, its role: to take the elements of the collection.

Three methods are defined in the iterator interface:

Boolean

Hasnext () Returns True if there are still elements that can be iterated.

E

Next () returns the next element of the iteration.

void

Remove () removes the last element returned by the iterator (optional operation) from the collection pointed to by the iterator.

Each collection has its own data structure (the way in which the data is stored in the container) and has a specific way of extracting its own internal elements. To facilitate the operation of all containers, remove the elements. The inside of the container is provided in accordance with a uniform rule, this rule is the iterator interface, so that the traversal of the container and its specific underlying implementation of isolation, to achieve decoupling effect.

Also said, as long as the interface can be removed from the collection set of elements, as for each specific container according to their own data structure, how to achieve the specific removal of details, this does not care, so that the removal of elements and specific sets of the coupling.

Iterator it = coll.iterator ()//Gets the iterator object in the container, as to what this object is not important. This object must conform to a regular iterator interface.

[java]  View Plain  copy public static voidmain (string[] args)  {           collection coll = new arraylist ();           coll.add ("abc0");           Coll.add ("ABC1");          coll.add ("ABC2");           //--------------Mode 1----------------------           iterator it = coll.iterator ();           while (It.hasnext ()) {               system.out.println (It.next ());          }           //---------------Mode 2 with this----------------------         &Nbsp; for (Iterator it =coll.iterator (); It.hasnext (); ) {               system.out.println (It.next ());           }       }  

If you use the iterator iterator for deletion, there will be no concurrent modification exceptions.

Because: When the remove operation is performed, the Checkforcomodification () is also executed first, followed by the ArrayList remove () method, which adds the Modcount value to 1, where we will Expectedmodcount =modcount, keeping it unified. 1.2 Listiterator

As you can see above, iterator only provides a way to delete elements, if we want to add elements as we iterate through them.

The Listiterator interface inherits the iterator interface, allowing the programmer to traverse the list in either direction, modify the list during the iteration, and get the iterator's current position in the list.

Use Listiterator to add element operations to the list on the Edge traversal edge:

[Java] View plain copy

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.