- 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
Get last value in Java TreeSet
To get the last value in TreeSet, use the last() 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.last()
The following is an example to get the last 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<String> i = tSet.iterator(); while(i.hasNext()){ System.out.println(i.next()); } System.out.println("Last Value = " + tSet.last()); } }
Output
TreeSet elements... 10 20 30 40 50 60 Last Value = 60
- Related Articles
- Get the element ordered last in Java TreeSet
- Get first value in Java TreeSet
- Java Program to get the lowest and highest value in TreeSet
- Get Size of TreeSet in Java
- Get Sub Set from TreeSet in Java
- Get Tail Set from TreeSet in Java
- Get Head Set from Java TreeSet
- Get the element ordered first in Java TreeSet
- Java Program to get headset from TreeSet
- Check if a particular value exists in Java TreeSet
- Java Program to get Sub Set from TreeSet
- Java Program to get Tail Set from TreeSet
- TreeSet in Java
- Get last key from NavigableMap in Java
- Get last entry from NavigableMap in Java

Advertisements