- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
Fetch elements of Java TreeSet using Iteration
Use Iterator class to fetch elements of TreeSet.
Create a TreeSet and add elements to it
TreeSet set = new TreeSet(); set.add("13"); set.add("11"); set.add("12"); set.add("16"); set.add("19"); set.add("23"); set.add("21"); set.add("20"); set.add("30");
Now, to display the elements, use Iterator class
Iterator<String> i = set.iterator(); while(i.hasNext()){ System.out.println(i.next()); }
The following is an example that fetch elements of TreeSet using Iteration
Example
import java.util.*; public class Demo { public static void main(String args[]){ TreeSet<String> set = new TreeSet<String>(); set.add("13"); set.add("11"); set.add("12"); set.add("16"); set.add("19"); set.add("23"); set.add("21"); set.add("20"); set.add("30"); System.out.println("TreeSet elements ..."); Iterator<String> i = set.iterator(); while(i.hasNext()){ System.out.println(i.next()); } } }
Output
TreeSet elements ... 11 12 13 16 19 20 21 23 30
- Related Articles
- Iterate through elements of TreeSet in Java
- Remove all elements from TreeSet in Java
- Traverse TreeSet elements in descending order in java
- Copy all elements in Java TreeSet to an Object Array
- How to save the elements of a TreeSet to a file in Java?
- TreeSet in Java
- How to fetch elements with iterator in Java?
- Iteration of for loop inside object in JavaScript to fetch records with odd CustomerId?
- Get Size of TreeSet in Java
- All permutations of a string using iteration?
- Create a TreeSet in Java
- Get last value in Java TreeSet
- Remove lowest element in Java TreeSet
- Remove highest element in Java TreeSet
- Sort items in a Java TreeSet

Advertisements