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

How to use the Iterable interface in the Java collection framework

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "how to use the Iterable interface in the Java collection framework". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn how to use the Iterable interface in the Java collection framework.

1. Write 1-cyclic for (int I = 0, len = strings.size (); I < len; iTunes +) {System.out.println (strings.get (I)); 2. Write 2-foreach cyclic for (String var: strings) {System.out.println (var); 3. 3-IteratorIterator iterator = strings.iterator (); while (iterator.hasNext ()) {System.out.println (iterator.next ());}

For the above three traversal methods, I believe everyone is very familiar with it. The for loop can be understood as getting elements through subscripts, which is very similar to arrays. Foreach mainly provides traversal support for decoupling of similar linked lists, which do not have subscripts, and can greatly degrade performance if you use foreach loops. Iterator is the protagonist we're going to talk about today, and it's actually foreach.

So since Java provides several ways to traverse a collection, how do you traverse it if it is a normal object or any object?

IV. Iterable

Iterable means iterator, and its purpose is to provide for-each loop support for collection classes. Because using for loops requires getting elements by location, which is only supported by arrays, many other data structures, such as linked lists, can only get data through queries, which will greatly reduce efficiency. Iterable allows different collection classes to provide the best way to traverse themselves.

Let's take a look at the Iterable document declaration, which has a sentence that says:

Implementing this interface allows an object to be the target of the "for-each loop" statement.

Its purpose is to provide a foreach loop for the Java object, and its main method is to return an Iterator object:

Iterator iterator ()

It means that if you want any Java object to support foreach, just implement the iterable interface, and then you can traverse it like a collection by means of Iterator iterator = strings.iterator ().

5. Iterator

Iterator is the main body of foreach traversal. Let's see how it is implemented.

/ / determines whether an object collection has the next element boolean hasNext (); / / gets the next element E next (); / / deletes the last element. It is not supported by default, because in many cases the results are unpredictable, for example, the data set is modified at this time default void remove () {...} / / mainly sends each element as a parameter to action to perform a specific operation default void forEachRemaining (Consumer)

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