Home > Java > javaTutorial > Iterate over the elements of a tree collection in Java using iterator() method of TreeSet class

Iterate over the elements of a tree collection in Java using iterator() method of TreeSet class

PHPz
Release: 2023-07-24 15:49:46
Original
1662 people have browsed it

Use the iterator() method of the TreeSet class to traverse the elements of the tree collection in Java

TreeSet is an ordered collection class in Java. It implements the Set interface and uses a red-black tree data structure to store elements. TreeSet maintains the natural order of the elements, or sorts them based on the passed comparator. In actual development, we often need to traverse the elements of TreeSet. At this time, we can use the iterator() method of TreeSet to traverse the elements in the collection.

Using the iterator() method of TreeSet, we can access the elements in the set one by one through the iterator. Iterator is a design pattern that provides a way to sequentially access the elements in a container object without knowing the internal implementation of the container.

The following is a sample code that uses the iterator() method of TreeSet to traverse the collection:

import java.util.TreeSet;
import java.util.Iterator;

public class TreeSetIteratorExample {
    public static void main(String[] args) {
        // 创建一个TreeSet对象
        TreeSet<String> treeSet = new TreeSet<>();
        
        // 往TreeSet中添加元素
        treeSet.add("Apple");
        treeSet.add("Banana");
        treeSet.add("Orange");
        treeSet.add("Grape");
        
        // 使用iterator()方法获取迭代器对象
        Iterator<String> iterator = treeSet.iterator();
        
        // 使用while循环遍历TreeSet的元素
        while (iterator.hasNext()) {
            // 使用next()方法获取当前元素,并且将指针移动到下一个元素
            String element = iterator.next();
            System.out.println(element);
        }
    }
}
Copy after login

Run the above code, you will find that the output is:

Apple
Banana
Grape
Orange
Copy after login

We can As you can see, the elements of TreeSet are sorted alphabetically. After using the iterator() method to obtain the iterator object, we can use the while loop and the hasNext() method of the iterator to determine whether there are still elements that can be iterated. Then, we use the iterator's next() method to get the current element and move the pointer to the next element.

It should be noted that the elements returned by the TreeSet iterator are ordered. This is because TreeSet uses a red-black tree data structure to store elements and maintains the natural order of the elements.

To summarize, using the iterator() method of TreeSet can easily traverse the elements of the tree collection. Iterators provide a way to sequentially access the elements in a collection without knowing the internals of the collection. We can determine whether there are still elements that can be iterated by judging the iterator's hasNext() method, and then use the next() method to get the current element and move the pointer to the next element.

The above is the detailed content of Iterate over the elements of a tree collection in Java using iterator() method of TreeSet class. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template