HashMap,TreepMap

import java.util.Set;
import java.util.TreeMap;
class People implements Comparable{
    private int age;
    private String name;
    
    public People(int age, String name) {
        super();
        this.age = age;
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    
    @Override
    public String toString() {
        return "People [age=" + age + ", name=" + name + "]";
    }
    @Override
    public int compareTo(Object arg0) {
        // TODO Auto-generated method stub
        People p=(People)arg0;
        if(arg0.getClass()!=People.class){
            throw new ClassCastException();
        }
        int temp=p.age-this.age;
        return temp==0? p.getName().compareTo(this.getName()):temp;
    }
}
public class JiHeDemo{
    

    public static void main(String[] args) {
        TreeMap hm=new TreeMap();
        hm.put(new People(16,"lishuai"), "anhui");
        hm.put(new People(8,"lish"), "alkjfi");
        hm.put(new People(17,"lishuai"), "asnjhui");
        Set st=hm.keySet();
        for (Object  key :st ) {
            Object value =hm.get(key);
            System.out.println(key+"''''''"+value);
        }
    }
}

import java.util.HashMap;
import java.util.Set;
class People {
    private int age;
    private String name;

    public People(int age, String name) {
        super();
        this.age = age;
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "People [age=" + age + ", name=" + name + "]";
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + age;
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        People other = (People) obj;
        if (age != other.age)
            return false;
        if (name == null) {
            if (other.name != null)
                return false;
        } else if (!name.equals(other.name))
            return false;
        return true;
    }

}

public class JiHeDemo {

    public static void main(String[] args) {
        HashMap hm = new HashMap();
        hm.put(new People(16, "lishuai"), "anhui");
        hm.put(new People(8, "lish"), "alkjfi");
        hm.put(new People(17, "lishuai"), "asnjhui");
        Set st = hm.keySet();
        for (Object key : st) {
            Object value = hm.get(key);
            System.out.println(key + "''''''" + value);
        }
    }
}

你可能感兴趣的:(HashMap,TreepMap)