Collection output takes you to learn advanced features of Java language

【Objective of this program】

In this section, you need to master four forms of collection output: Iterator iterative output, ListIterator bidirectional iterative output, Enumeration enumeration output, foreach output.

collection output
Collection output has actually provided a forEach() method in the Iterable interface since JDK1.8, but this method output is not in the traditional sense of collection output, and it is difficult to appear in actual development. For collections In terms of operation, there are four output forms: Iterator iterative output (95%), ListIterator bidirectional iterative output (0.1%), Enumeration enumeration output (4.9%), foreach output (equivalent to Iterator).

Iterator iteration output
Through the inheritance relationship of the Collection interface, it can be found that since JDK1.5, it has inherited an Iterable parent interface, and an iterator() operation method is defined in this interface, through which the Iterator interface object can be obtained (in JDK1.5 Previously, this method was directly defined in the Collection interface).

Get the Iterator interface object: public Iterator iterator​();

However, special attention should be paid to the use of the remove() method in the Iterator interface (if it is not necessary, do not use it). In fact, there is a data deletion operation method defined in the Collection interface, but if you use the remove() method in the Collection during the iterative output process, the iteration will fail.
Example: Use the remove() method in the Collection to delete

At this time, there is no error after the program is executed, and the data in the original collection can be successfully deleted.

Interview questions:

Please explain the difference between Collection.remove() and Iterator.remove()?

When performing iterative output, if Collection.remove() is used, it will cause concurrent update exceptions, resulting in program deletion errors. At this time, only the remove() method in the Iterator interface can be used to implement normal deletion processing.

ListIterator bidirectional iterative output

The iterative output operation using Iterator has a feature: it only allows output from front to back, and if bidirectional iterative processing is required now, it must be implemented by relying on Iterator's sub-interface: ListIterator interface. It should be noted that if you want to obtain the ListIterator interface object, the related processing method is not defined in the Collection, but the List sub-interface does, that is to say, the output interface is specially prepared for the List collection.

The following operation methods are defined in the ListIterator interface:

Determine whether there is a previous element: public boolean hasPrevious()
Get the current element: public E previous()

Example: Implementing Bidirectional Iteration

If you want to implement traversal from back to front, then the first thing you need to do is to implement traversal processing from front to back.

Enumeration output

Enumeration is the output interface used in JDK1.0. This output interface is mainly to provide services for the Vector class. Until the subsequent development of JDK, Enumeration still only serves the Vector class, so if you want to get the Enumeration interface object , then you must rely on the methods provided by the Vector class:

Get Enumeration: public Enumeration elements()
There are two operation methods defined in the Enumeration interface:
Determine whether there is a next element: public boolean hasMoreElements()
Get the current element: public E nextElement()

Since this interface has appeared for a long time, in some earlier development processes, some methods only support Enumeration output operations, but with the continuous improvement of class methods, most operations can be directly implemented using Iterator .

foreach output

In addition to using the iterative interface to realize the output, starting from JDK1.5, the enhanced for loop can also realize the output of the collection. The form of this output is similar to the form of the output operation of the array.

Example: output using foreach

When this output first appeared, many people did not recommend using it, because the standard collection operation is still based on Iterator, but after all, JDK1.5 has been released for more than ten years, and many syntaxes have begun to be used by most people.

Related Articles

Explore More Special Offers

  1. Short Message Service(SMS) & Mail Service

    50,000 email package starts as low as USD 1.99, 120 short messages start at only USD 1.00

phone Contact Us