⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fig7-34.java

📁 数据结构用java实现的一些源代码
💻 JAVA
字号:
import java.io.*;
import java.util.*;

class Person {
    protected String name;
    protected int age;
    public Person(String s, int i) {
        name = s; age = i;
    }
    public Person() {
        this("",0);
    }
    public String toString() {
        return "(" + name + "," + age  + ")";
    }
}

class PersonByName extends Person implements Comparable {
    public PersonByName(String s, int i) {
        super(s,i);
    }
    public PersonByName() {
        super();
    }
    public PersonByName(Person p) {
        super(p.name,p.age);
    }
    public int compareTo(Object p) {
        return name.compareTo(((Person)p).name);
    }
}

class PersonByAge extends Person implements Comparable {
    public PersonByAge(String s, int i) {
        super(s,i);
    }
    public PersonByAge() {
        super();
    }
    public PersonByAge(Person p) {
        super(p.name,p.age);
    }
    public int compareTo(Object p) {
        return age - ((Person)p).age;
    }
}

class PersonComparator implements java.util.Comparator {
    public int compare(Object ob1, Object ob2) {
        if (ob1 == ob2)
             return 0;
        else if(ob1 == null)
             return -1;
        else if (ob2 == null)
             return 1;
        else return ((Person)ob1).name.compareTo(((Person)ob2).name);
    }
}
             
class TestTreeMap {
    public static void main(String[] a) {
        TreeMap cities = new TreeMap();
        cities.put(new PersonByName("Gregg",25),"Pittsburgh");
        cities.put(new PersonByName("Ann",30),"Boston");
        cities.put(new PersonByName("Bill",20),"Belmont");
        System.out.println(cities);
        // {(Ann,30)=Boston, (Bill,20)=Belmont, (Gregg,25)=Pittsburgh}
        cities.put(new PersonByName("Gregg",30),"Austin");
        System.out.println(cities);
        // {(Ann,30)=Boston, (Bill,20)=Belmont, (Gregg,25)=Austin}
        System.out.println(cities.containsKey(new PersonByName("Ann",30)));// true
        System.out.println(cities.containsValue("Austin"));                // true
        System.out.println(cities.firstKey() + " " + cities.lastKey());
        // (Ann,30) (Gregg,25)
        System.out.println(cities.get(new PersonByName("Ann",30))); // Boston
        System.out.println(cities.entrySet()); 
        // [(Ann,30)=Boston, (Bill,20)=Belmont, (Gregg,25)=Austin]
        System.out.println(cities.keySet()); 
        // [(Ann,30), (Bill,20), (Gregg,25)]
        System.out.println(cities.remove(new PersonByName("Bill",20))); // Belmont
        System.out.println(cities); 
        // {(Ann,30)=Boston, (Gregg,25)=Austin}

        Map.Entry me = (Map.Entry)cities.entrySet().iterator().next(); // first key
        System.out.println((PersonByName)me.getKey());  // (Ann,30)
        System.out.println(me.getValue());              // Boston
        System.out.println(me.setValue("Harrisburg"));  // Boston
        System.out.println(cities); 
        // {(Ann,30)=Harrisburg, (Gregg,25)=Austin}

        TreeMap SSN = new TreeMap();
        SSN.put(new Integer(123456789),new PersonByName("Gregg",25));
        SSN.put(new Integer(111111111),new PersonByName("Ann",30));
        SSN.put(new Integer(222222222),new PersonByName("Bill",20));
        System.out.println(SSN);
        // {111111111=(Ann,30), 123456789=(Gregg,25), 222222222=(Bill,20)}
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -