SHARE
TWEET

Untitled

razvanth21 Jan 25th, 2018 47 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Persoana implements Comparable<Persoana>
  2. {
  3.     private String nume;
  4.     private int varsta;
  5.  
  6.     public Persoana(String n, int v)
  7.     {
  8.         nume = n;
  9.         varsta = v;
  10.     }
  11.  
  12.     public String getNume()
  13.     {
  14.         return nume;
  15.     }
  16.  
  17.     public int compareTo(Persoana p)
  18.     {
  19.         return nume.compareTo(p.getNume());
  20.     }
  21.    
  22.     public String toString()
  23.     {
  24.         return nume;
  25.     }
  26. }
  27.  
  28. class Main
  29. {
  30.     public static void main(String[] args)
  31.     {
  32.         SortedSet<Persoana> set = new TreeSet<Persoana>(
  33.             new Comparator<Persoana>()
  34.             {
  35.                 public int compare(Persoana p1, Persoana p2)
  36.                 {
  37.                     return p1.getNume().compareTo(p2.getNume());
  38.                 }
  39.             });
  40.  
  41.         set.add(new Persoana("B", 10));
  42.         set.add(new Persoana("A", 15));
  43.         set.add(new Persoana("C", 20));
  44.  
  45.         System.out.println(set);
  46.     }
  47. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top