How do I get the last element of SortedSet?

package org.kodejava.example.util;

import java.util.SortedSet;
import java.util.TreeSet;

public class LastSetElement {
    public static void main(String[] args) {
        SortedSet<String> set = new TreeSet<>();

        // SortedSet orders the items it contains. After that we get the last
        // item from the set using last() method. The last item will be "Two".
        String lastElement = set.last();
        System.out.println("lastElement = " + lastElement);

Wayan Saryada

Founder at Kode Java Org
I am a programmer, a runner, a recreational diver, currently live in the island of Bali, Indonesia. Mostly programming in Java, Spring Framework, Hibernate / JPA. You can support my works by donating here. Thank you 🥳

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.