0

I was trying to create a TreeSet instance using the below code.

Set<Integer> treeSet = new TreeSet<>();
treeSet.last() //gives compilation error
//solution here is either to cast the treeSet instance  
//Or create treeSet using TreeSet concrete class. Which is not a best practice.

What to follow here ? Thanks in advance.

7 Answers7

4

Since you require methods of the SortedSet interface, change the type of treeSet to SortedSet:

SortedSet<Integer> treeSet = new TreeSet<>();
treeSet.last(); 
Eran
  • 387,369
  • 54
  • 702
  • 768
3

The Set interface doesn't have those methods. Other data structures, like HashSet, implements the Set interface and they don't guarantee order so there is no use for those methods.

You can use the class instead of the interface

TreeSet<Integer> treeSet = new TreeSet<>();
Guy
  • 46,488
  • 10
  • 44
  • 88
  • I can add that `Set` is not designed for use `last()`, `first`: `It models the mathematical set abstraction. The Set interface contains only methods inherited from Collection and adds the restriction that duplicate elements are prohibited.` Collection itself doesn't provide those methods – Sergii Getman Sep 06 '17 at 05:03
1

Set data structure behavior is not meant for ordering elements. So SET dont have these methods. Treeset is middle way to achieve this functionality as it has ordering mechanism implemented with the fusion of

Set + Red black Tree

data structure.

SortedSet is another implementation is been developed for this purpose.

Do it using

SortedSet<Integer> set = new TreeSet<>();
set.last();
Pramod S. Nikam
  • 4,271
  • 4
  • 38
  • 62
0

Set Interface does not have first() & last() methods, so use the TreeSet type

import java.util.TreeSet;

public class Main {
    public static void main(String[] args) {
        TreeSet<Integer> treeSet = new TreeSet<>();
        treeSet.last();
    }
}
karthikdivi
  • 3,466
  • 5
  • 27
  • 46
0

A set represents a collection that contains no duplicate elements. Thus the interface allows only the get set and iterate actions.

For your goal continue to use TreeSet (which implements the SortedSet)

TreeSet<Integer> treeSet = new TreeSet<>();
gkatzioura
  • 2,655
  • 2
  • 26
  • 39
0

In your code treeSet is of type Set which doesn't have the first or last methods. If you will work with a TreeSet you will have access to those methods:

TreeSet<Integer> treeSet = new TreeSet<>();
treeSet.last() //gives compilation error
//solution here is either to cast the treeSet instance  
//Or create treeSet using TreeSet concrete class. Which is not a best practice.
Roee Gavirel
  • 18,955
  • 12
  • 67
  • 94
0

The Set interface is a collection that is not considered to be ordered. Although that's not explicit at the class level, it's all over the methods of the class, e.g. iterator():

Returns an iterator over the elements in this set. The elements are returned in no particular order (unless this set is an instance of some class that provides a guarantee).

In order to have a first and last, you need a order or an index, and Set has neither. As many other people have pointed out already, you can use SortedSet if you want an order.

TreeSet implements SortedSet, so you can certainly modify your supplied code to:

SortedSet<Integer> treeSet = new TreeSet<>();
treeSet.last();
Geoffrey Wiseman
  • 5,459
  • 3
  • 34
  • 52