
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Get First Value in Java TreeSet
To get the first value in TreeSet, use the first() method.
First, get the TreeSet and add elements to it
TreeSet<String> tSet = new TreeSet<String>(); tSet.add("10"); tSet.add("20"); tSet.add("30"); tSet.add("40"); tSet.add("50"); tSet.add("60");
Now, get the first value
tSet.first()
The following is an example to get the first value in TreeSet
Example
import java.util.*; public class Demo { public static void main(String args[]){ TreeSet<String> tSet = new TreeSet<String>(); tSet.add("10"); tSet.add("20"); tSet.add("30"); tSet.add("40"); tSet.add("50"); tSet.add("60"); System.out.println("TreeSet elements..."); Iterator i = tSet.iterator(); while(i.hasNext()){ System.out.println(i.next()); } System.out.println("First Value = " + tSet.first()); } }
Output
The output is as follows
TreeSet elements... 10 20 30 40 50 60 First Value = 10
Advertisements