| Both sides previous revisionPrevious revisionNext revision | Previous revision |
| java:jfc_intf_sorted_set_map [2015/03/17 13:06] – [java.util.SortedSet] gthanos | java:jfc_intf_sorted_set_map [2020/03/15 18:48] (current) – external edit 127.0.0.1 |
|---|
| ===== java.util.SortedSet ===== | ===== java.util.SortedSet ===== |
| |
| Τo interface //SortedSet// είναι ένα //Set// που διατηρεί τα στοιχεία του σε αύξουσα σειρά με βάση την υλοποίηση του interface //Comparable// ή της κλάσης Comparator για τον συγκεκριμένο τύπο δεδομένων. Οι επιπλέον μέθοδοι του συγκεκριμένου interface είναι οι εξής | Τo interface //SortedSet// είναι ένα //Set// που διατηρεί τα στοιχεία του σε αύξουσα σειρά με βάση την υλοποίηση του interface //Comparable// ή ενός //Comparator// για τον συγκεκριμένο τύπο δεδομένων. Οι επιπλέον μέθοδοι του συγκεκριμένου interface είναι οι εξής |
| * **[[http://docs.oracle.com/javase/7/docs/api/java/util/SortedSet.html#first()|first()]] - ** Επιστρέφει το 1ο στοιχείο από το //Set//. | * **[[http://docs.oracle.com/javase/7/docs/api/java/util/SortedSet.html#first()|first()]] - ** Επιστρέφει το 1ο στοιχείο από το //Set//. |
| * **[[http://docs.oracle.com/javase/7/docs/api/java/util/SortedSet.html#last()|last()]] - ** Επιστρέφει το τελευταίο στοιχείο από το //Set//. | * **[[http://docs.oracle.com/javase/7/docs/api/java/util/SortedSet.html#last()|last()]] - ** Επιστρέφει το τελευταίο στοιχείο από το //Set//. |
| * **[[http://docs.oracle.com/javase/7/docs/api/java/util/SortedSet.html#tailSet(E)|tailSet(E fromElement)]] - ** Επιστρέφει το υποσύνολο του Set που τα στοιχεία του είναι μεγαλύτερα ή ίσα από την τιμή //fromElement//. | * **[[http://docs.oracle.com/javase/7/docs/api/java/util/SortedSet.html#tailSet(E)|tailSet(E fromElement)]] - ** Επιστρέφει το υποσύνολο του Set που τα στοιχεία του είναι μεγαλύτερα ή ίσα από την τιμή //fromElement//. |
| * **[[http://docs.oracle.com/javase/7/docs/api/java/util/SortedSet.html#subSet(E,%20E)|subSet(E fromElement, E toElement)]] - ** Επιστρέφει το υποσύνολο του Set από fromElement (μαζί με το fromElement) έως toElement (χωρίς το toElement). | * **[[http://docs.oracle.com/javase/7/docs/api/java/util/SortedSet.html#subSet(E,%20E)|subSet(E fromElement, E toElement)]] - ** Επιστρέφει το υποσύνολο του Set από fromElement (μαζί με το fromElement) έως toElement (χωρίς το toElement). |
| | |
| | <code java StudentSortedSet.java> |
| | import java.util.*; |
| | |
| | public class StudentSortedSet { |
| | |
| | |
| | public static void main(String[] args) { |
| | |
| | SortedSet<Student> students = new TreeSet<Student>(); |
| | students.add(new Student("John", "Smith")); |
| | students.add(new Student("Stanley", "Peters")); |
| | students.add(new Student("Edgar", "Bloch")); |
| | students.add(new Student("Suzan", "Miles")); |
| | students.add(new Student("Mary", "Poppins")); |
| | students.add(new Student("Tom", "Rich")); |
| | for(Student n: students) |
| | System.out.println(n); |
| | |
| | System.out.println("\n***Print headset***"); |
| | |
| | SortedSet<Student> subStudents = students.headSet(new Student("Mary", "Poppins")); |
| | for(Student s: subStudents) |
| | System.out.println(s); |
| | |
| | System.out.println("\n***Print tailset***"); |
| | |
| | subStudents = students.tailSet(new Student("Mary", "Poppins")); |
| | for(Student s: subStudents) |
| | System.out.println(s); |
| | } |
| | } |
| | </code> |
| | |
| | <WRAP center round 60% tip>Στο παραπάνω παράδειγμα δείτε πως αλλάζει η κατάταξη αν αφαιρέσετε τον //Comparator// από τον κατασκευαστή του αντικειμένου //names//. |
| | </WRAP> |
| |
| ===== java.util.SortedMap ===== | ===== java.util.SortedMap ===== |
| * **[[http://docs.oracle.com/javase/7/docs/api/java/util/SortedMap.html#lastKey()|lastKey()]] - ** Επιστρέφει το τελευταίο κλειδί. | * **[[http://docs.oracle.com/javase/7/docs/api/java/util/SortedMap.html#lastKey()|lastKey()]] - ** Επιστρέφει το τελευταίο κλειδί. |
| * **[[http://docs.oracle.com/javase/7/docs/api/java/util/SortedMap.html#headMap(K)|headMap(K toKey)]] - ** Επιστρέφει το υποσύνολο του Map που τα κλειδιά του είναι μικρότερα από την τιμή //toKey//. | * **[[http://docs.oracle.com/javase/7/docs/api/java/util/SortedMap.html#headMap(K)|headMap(K toKey)]] - ** Επιστρέφει το υποσύνολο του Map που τα κλειδιά του είναι μικρότερα από την τιμή //toKey//. |
| * **[[http://docs.oracle.com/javase/7/docs/api/java/util/SortedMap.html#tailMap(E)|tailMap(E fromKey)]] - ** Επιστρέφει το υποσύνολο του Map που τα κλειδιά του του είναι μεγαλύτερα ή ίσα από την τιμή //fromKey//. | * **[[http://docs.oracle.com/javase/7/docs/api/java/util/SortedMap.html#tailMap(K)|tailMap(K fromKey)]] - ** Επιστρέφει το υποσύνολο του Map που τα κλειδιά του του είναι μεγαλύτερα ή ίσα από την τιμή //fromKey//. |
| * **[[http://docs.oracle.com/javase/7/docs/api/java/util/SortedMap.html#subMap(E,%20E)|subMap(K fromKey, K toKey)]] - ** Επιστρέφει το υποσύνολο του Set από fromKey (μαζί με το fromKey) έως toKey (χωρίς το toKey). | * **[[http://docs.oracle.com/javase/7/docs/api/java/util/SortedMap.html#subMap(K,%20K)|subMap(K fromKey, K toKey)]] - ** Επιστρέφει το υποσύνολο του Set από fromKey (μαζί με το fromKey) έως toKey (χωρίς το toKey). |
| | |
| | <code java 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; } |
| | } |
| | </code> |
| | |
| | <code java 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); |
| | } |
| | } |
| | </code> |
| |
| | |Προηγούμενο: [[:java:jfc_intf_sort | Sorting ]] | [[:toc | Περιεχόμενα ]] | Επόμενο: [[:java:jfc_algorithms| Αλγόριθμοι ]] | |