How do I get the first item of SortedSet?

package org.kodejava.example.util;

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

public class FirstSetElement {
    public static void main(String[] args) {
        SortedSet set = new TreeSet();
        set.add("One");
        set.add("Two");
        set.add("Three");
        set.add("Four");
        set.add("Five");

        //
        // SortedSet orders the items it contains. After that we get the first
        // item from the set using first() method. The fist item will be "Five".
        //
        String firstItem = set.first();
        System.out.println("firstItem = " + firstItem);
    }
}

Wayan Saryada

Programmer, runner, recreational diver, live in the island of Bali, Indonesia. Mostly programming in Java, creating web based application with Spring Framework, Hibernate / JPA.

Leave a Reply