- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Iterate through elements of TreeSet in Java
First, create a TreeSet and add elements −
TreeSet<String> set = new TreeSet<String>(); set.add("34"); set.add("12"); set.add("67"); set.add("54"); set.add("76"); set.add("49");
Now, let us iterate through the elements −
Iterator<String> i = set.iterator(); while(i.hasNext()){ System.out.println(i.next()); }
The following is an example that iterate through elements of TreeSet −
Example
import java.util.*; public class Demo { public static void main(String args[]){ TreeSet<String> set = new TreeSet<String>(); set.add("34"); set.add("12"); set.add("67"); set.add("54"); set.add("76"); set.add("49"); System.out.println("TreeSet elements..."); Iterator<String> i = set.iterator(); while(i.hasNext()){ System.out.println(i.next()); } } }
Output
TreeSet elements... 12 34 49 54 67 76
- Related Articles
- Iterate through elements of Java LinkedHashSet
- Iterate through elements of HashSet in Java
- Java Program to Iterate through Elements of HashMap
- Iterate through elements of a LinkedList using a ListIterator in Java
- How many ways to iterate a TreeSet in Java?
- Iterate through ArrayList in Java
- Golang Program to Iterate through Elements of Dictionary
- Iterate through the values of Java LinkedHashMap in Java
- Iterate through Decade Tuple in Java
- Iterate through Ennead Tuple in Java
- Iterate through Octet Tuple in Java
- Iterate through LabelValue Tuple in Java
- Iterate through KeyValue Tuple in Java
- Iterate through the values of HashMap in Java
- Iterate through the values of TreeMap in Java

Advertisements