Skip to main content
1 of 2
candied_orange
  • 114.7k
  • 27
  • 222
  • 352

Consider TreeSet.

From javatpoint.com : treeset

import java.util.*;  
class TestCollection11{  
 public static void main(String args[]){  
  //Creating and adding elements  
  TreeSet<String> al=new TreeSet<String>();  
  al.add("Ravi");  
  al.add("Vijay");  
  al.add("Ravi");  
  al.add("Ajay");  
  //Traversing elements  
  Iterator<String> itr=al.iterator();  
  while(itr.hasNext()){  
   System.out.println(itr.next());  
  }  
 }  
}  

outputs

Ajay
Ravi
Vijay

Indeed, choosing the best data structure makes life easier.

candied_orange
  • 114.7k
  • 27
  • 222
  • 352