Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is set output

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "what is set output". In the daily operation, I believe that many people have doubts about what is set output. The editor consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the questions of "what is set output"! Next, please follow the editor to study!

Set output

Collection output actually provides a forEach () method in the Iterable interface from JDK1.8, but this method output is not in the traditional form of collection output, and it is difficult to appear in actual development, for collection operations. There are four output forms: Iterator iterative output (95%), ListIterator bidirectional iterative output (0.1%), Enumeration enumerated output (4.9%), and foreach output (equivalent to Iterator).

Iterator iterative output

Through the inheritance relationship of the Collection interface, it can be found that it inherits one more Iterable parent interface from JDK1.5, and there is an iterator () operation method defined in this interface, through which the Iterator interface object can be obtained (before JDK1.5, this method is directly defined in the Collection interface).

Get the Iterator interface object: public Iterator iterator ()

The following methods are defined in the Iterator API:

No. Method name type description 01public boolean hasNext () General judgment whether there is data 02public E next () current data 03default void remove () General deletion

The java.util.Scanner class used previously is a subclass of the Iterator interface, so the class inheritance relationship is as follows:

Iterator interface

Example: using Iterator output

Import java.util.Set;import java.util.Iterator;public class JavaAPIDemo {public static void main (String [] args) throws Exception {Set all = Set.of ("Hello", "World", "MLDN"); Iterator iter = all.iterator (); / / instantiate the Iterator interface object while (iter.hasNext ()) {String str = iter.next (); System.out.println (str); / / World Hello MLDN}

However, special attention should be paid to the use of the remove () method in the Iterator interface (if not necessary). The data deletion method is actually defined in the Collection interface, but if you use the remove () method in Collection during the iterative output, the iteration will fail.

Example: delete using the remove () method in the Collection collection

Import java.util.Set;import java.util.Iterator;import java.util.HashSet;public class JavaAPIDemo {public static void main (String [] args) throws Exception {Set all = new HashSet (); all.add ("Hello"); all.add ("World"); all.add ("MLDN"); Iterator iter = all.iterator (); / / instantiate Iterator interface object while (iter.hasNext ()) {String str = iter.next () If ("World" .equals (str)) {all.remove ("World"); / / Collection collection method} else {System.out.println (str); / / Hello Exception in thread "main" >

If the data deletion operation cannot be performed at this time, it can only be deleted by using the remove () method in the Iterator interface.

Example: delete method using Iterator interface

Import java.util.Set;import java.util.Iterator;import java.util.HashSet;public class JavaAPIDemo {public static void main (String [] args) throws Exception {Set all = new HashSet (); all.add ("Hello"); all.add ("World"); all.add ("MLDN"); Iterator iter = all.iterator () / / instantiate the Iterator interface object while (iter.hasNext ()) {String str = iter.next (); if ("World" .equals (str)) {iter.remove (); / / Collection collection method} else {System.out.println (str); / / Hello Exception in thread "main" java.util.ConcurrentModificationException}} System.out.println ("*" + all) }} / / Hello//MLDN//*** [Hello, MLDN]

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

Interview questions:

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

In the iterative output, if Collection.remove () is used, it will cause the exception of concurrent updates, resulting in program deletion errors. At this time, the normal deletion processing can only be achieved by using the remove () method in the Iterator interface.

ListIterator bidirectional iterative output

Iterative output operations using Iterator have one feature: only front-to-back output is allowed, and if two-way iterative processing is needed now, it must be implemented by the ListIterator interface, a subinterface of Iterator. It is important to note that if you want to get the ListIterator interface object, there is no relevant processing method defined in Collection, but the list subinterface does, that is, the interface of this output is specially prepared for the List collection.

ListIterator interface

The following operation methods are defined in the ListIterator API:

Determine whether there is a previous element: public boolean hasPrevious ()

Get the current element: public E previous ()

Example: implementing two-way iteration

Import java.util.ArrayList;import java.util.List;import java.util.ListIterator;public class JavaAPIDemo {public static void main (String [] args) throws Exception {List all = new ArrayList (); all.add ("Hello"); all.add ("World"); all.add ("MLDN"); ListIterator iter = all.listIterator (); System.out.print ("output from front to back:") While (iter.hasNext ()) {System.out.print (iter.next () + ",");} System.out.print ("\ noutput from back to front:"); / / output from front to back: Hello, World, MLDN, while (iter.hasPrevious ()) {System.out.print (iter.previous () + ","); / / output from back to front: MLDN, World, Hello,}

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

Enumeration output

Enumeration uses the output interface when JDK1.0, and this output interface is mainly for the Vector class. Until the subsequent development of JDK, Enumeration still serves only one class for Vector, so if you want to get the Enumeration interface object, 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 ()

Example: using Enumeration to implement output

Import java.util.Enumeration;import java.util.Vector;public class JavaAPIDemo {public static void main (String [] args) throws Exception {Vector all = new Vector (); all.add ("Hello"); all.add ("World"); all.add ("MLDN"); Enumeration enu = all.elements (); while (enu.hasMoreElements ()) {String str = enu.nextElement () System.out.print (str + ","); / / Hello, World, MLDN,}

As the interface has been around 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 of the operations can be directly implemented using Iterator.

Foreach output

In addition to using iterative interfaces to implement output, enhanced for loops starting with JDK1.5 can also implement the output of collections. The form of this output is similar to the output operation of an array.

Example: using foreach output

Import java.util.ArrayList;import java.util.List;public class JavaAPIDemo {public static void main (String [] args) throws Exception {List all = new ArrayList (); all.add ("Hello"); all.add ("World"); all.add ("MLDN"); for (String str: all) {System.out.print (str+ ","); / / Hello, World, MLDN,}

When this kind of output first appeared, many people did not recommend it, because the standard collection operation is still based on Iterator, but after all, JDK1.5 has been introduced for more than a decade, and a lot of syntax is beginning to be used by most people.

At this point, the study of "what is collective output" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report