Table of Contents

Τα interfaces SortedSet και SortedMap

java.util.SortedSet

Τo interface SortedSet είναι ένα Set που διατηρεί τα στοιχεία του σε αύξουσα σειρά με βάση την υλοποίηση του interface Comparable ή ενός Comparator για τον συγκεκριμένο τύπο δεδομένων. Οι επιπλέον μέθοδοι του συγκεκριμένου interface είναι οι εξής

NameSortedSet.java
iimport java.util.*;
 
public class NameSortedSet {
   static final Comparator<Name> NameOrdering = 
                                        new Comparator<Name>() {
            public int compare(Name n1, Name n2) {
                int cmp = n1.firstName().compareTo(n2.firstName() );
                return (cmp != 0 ? cmp : n1.lastName().compareTo(n2.lastName() ));
            }
    };
 
    public static void main(String[] args) {
 
        SortedSet<Name> names = new TreeSet<Name>(NameOrdering);
        names.add(new Name("Bob", "Travolta"));
        names.add(new Name("John", "Smith"));
        names.add(new Name("Karl", "Ng"));
        names.add(new Name("Jeff", "Smith"));
        names.add(new Name("Tom", "Rich"));
        for(Name n: names)
          System.out.println(n);
 
        System.out.println("\n***Print headset***");
 
        SortedSet<Name> subnames = names.headSet(new Name("Karl", "Ng"));
        for(Name n: subnames)
          System.out.println(n);
 
        System.out.println("\n***Print tailset***");
 
        subnames = names.tailSet(new Name("Karl", "Ng"));
        for(Name n: subnames)
          System.out.println(n);
    }
}

Στο παραπάνω παράδειγμα δείτε πως αλλάζει η κατάταξη αν αφαιρέσετε τον Comparator από τον κατασκευαστή του αντικειμένου names.

java.util.SortedMap

Τo interface SortedMap είναι ένα Map που διατηρεί τα κλειδιά του σε αύξουσα σειρά με βάση την υλοποίηση του interface Comparable ή της κλάσης Comparator τα κλειδιά. Οι επιπλέον μέθοδοι του συγκεκριμένου interface είναι οι εξής

NameProperty.java
import java.util.*;
 
public class NameProperty {
  private String email;
  private String address;
 
  public NameProperty(String mail, String address) {
    this.email = mail;
    this.address = address;
  }
 
  public String getEmail() { return email; }
  public String getAddress() { return address; }
 
  public void setEmail(String mail) { email = mail; }
  public void setAddress(String address) { this.address = address; }
 
  public String toString() { return "email: "+email+", address: "+address; }
}
NameSortedMap.java
import java.util.*;
 
public class NameSortedMap {
   static final Comparator<Name> NameOrdering = 
                                        new Comparator<Name>() {
            public int compare(Name n1, Name n2) {
                int cmp = n1.firstName().compareTo(n2.firstName() );
                return (cmp != 0 ? cmp : n1.lastName().compareTo(n2.lastName() ));
            }
    };
 
    public static void main(String[] args) {
 
        SortedMap<Name,NameProperty> users = new TreeMap<Name,NameProperty>(NameOrdering);
        users.put(new Name("Bob", "Travolta"), new NameProperty("bob@travolta.com", "Gklavani 37, 1st floor") );
        users.put(new Name("John", "Smith"), new NameProperty("john@smith.com", "Gklavani 37, 2nd floor") );
        users.put(new Name("Karl", "Ng"), new NameProperty("karl@ng.com", "Gklavani 37, 3rd floor") );
        users.put(new Name("Jeff", "Smith"), new NameProperty("jeff@smith.com", "Gklavani 37, 4th floor") );
        users.put(new Name("Tom", "Rich"), new NameProperty("tom@rich.com", "Gklavani 37, 5tht floor") );
        for(Map.Entry<Name,NameProperty> u: users.entrySet())
          System.out.println(u);
 
        System.out.println("\n***Print headmap***");
 
        SortedMap<Name,NameProperty> subusers = users.headMap(new Name("Karl", "Ng"));
        for(Map.Entry<Name,NameProperty> u: subusers.entrySet())
          System.out.println(u);
 
        System.out.println("\n***Print tailmap***");
 
        subusers = users.tailMap(new Name("Karl", "Ng"));
        for(Map.Entry<Name,NameProperty> u: subusers.entrySet())
          System.out.println(u);
    }
}