This shows you the differences between two versions of the page.
|
java:jfc_intf_sorted_set_map [2017/03/28 23:48] gthanos [java.util.SortedSet] |
java:jfc_intf_sorted_set_map [2020/03/15 18:48] |
||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ====== Τα interfaces SortedSet και SortedMap ====== | ||
| - | ===== java.util.SortedSet ===== | ||
| - | |||
| - | Τo interface // | ||
| - | * **[[http:// | ||
| - | * **[[http:// | ||
| - | * **[[http:// | ||
| - | * **[[http:// | ||
| - | * **[[http:// | ||
| - | |||
| - | <code java NameSortedSet.java> | ||
| - | import java.util.*; | ||
| - | |||
| - | public class NameSortedSet { | ||
| - | | ||
| - | new Comparator< | ||
| - | 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< | ||
| - | names.add(new Name(" | ||
| - | names.add(new Name(" | ||
| - | names.add(new Name(" | ||
| - | names.add(new Name(" | ||
| - | names.add(new Name(" | ||
| - | for(Name n: names) | ||
| - | System.out.println(n); | ||
| - | | ||
| - | System.out.println(" | ||
| - | | ||
| - | SortedSet< | ||
| - | for(Name n: subnames) | ||
| - | System.out.println(n); | ||
| - | | ||
| - | System.out.println(" | ||
| - | | ||
| - | subnames = names.tailSet(new Name(" | ||
| - | for(Name n: subnames) | ||
| - | System.out.println(n); | ||
| - | } | ||
| - | } | ||
| - | </ | ||
| - | |||
| - | <WRAP center round 60% tip> | ||
| - | </ | ||
| - | |||
| - | ===== java.util.SortedMap ===== | ||
| - | |||
| - | Τo interface // | ||
| - | * **[[http:// | ||
| - | * **[[http:// | ||
| - | * **[[http:// | ||
| - | * **[[http:// | ||
| - | * **[[http:// | ||
| - | |||
| - | <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 " | ||
| - | } | ||
| - | </ | ||
| - | |||
| - | <code java NameSortedMap.java> | ||
| - | import java.util.*; | ||
| - | |||
| - | public class NameSortedMap { | ||
| - | | ||
| - | new Comparator< | ||
| - | 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< | ||
| - | users.put(new Name(" | ||
| - | users.put(new Name(" | ||
| - | users.put(new Name(" | ||
| - | users.put(new Name(" | ||
| - | users.put(new Name(" | ||
| - | for(Map.Entry< | ||
| - | System.out.println(u); | ||
| - | | ||
| - | System.out.println(" | ||
| - | | ||
| - | SortedMap< | ||
| - | for(Map.Entry< | ||
| - | System.out.println(u); | ||
| - | | ||
| - | System.out.println(" | ||
| - | | ||
| - | subusers = users.tailMap(new Name(" | ||
| - | for(Map.Entry< | ||
| - | System.out.println(u); | ||
| - | } | ||
| - | } | ||
| - | </ | ||
| - | |||
| - | |Προηγούμενο: | ||