TreeMap与Hashtable的使用

TreeMap与Hashtable的使用

TreeMap

向TreeMap中添加key-value,要求key必须由同一个类创建的对象(要按照key进行排序)

两种排序方式

与TreeSet类似

https://www.cnblogs.com/CrabDumplings/p/13390443.html

(可查看TreeSet排序具体问题和要求事项)

自然排序

public class Students implements Comparable{
    private String name;
    private int age;

    public Students() {
    }

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

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

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

    @Override
    public String toString() {
        return "Students{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Students students = (Students) o;
        return age == students.age &&
                Objects.equals(name, students.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }

    //按照名字由大到小进行排序
    @Override
    public int compareTo(Object o) {
        if(o instanceof Students){
            Students s = (Students)o;
            return - this.name.compareTo(s.name);
        }else{
            throw new RuntimeException("传入数据类型不一致");
        }
    }
}


public void test4(){
        TreeMap map = new TreeMap();

        map.put(new Students("Lisa",18),90);
        map.put(new Students("Ann",19),100);
        map.put(new Students("Tom",17),50);
        map.put(new Students("Jack",20),80);

        Set set = map.entrySet();
        Iterator iterator = set.iterator();
        while(iterator.hasNext()){
            System.out.println(iterator.next());
        }
    }

定制排序

public void test5(){

        //名字从小到大排列,名字相同的时候年龄从大到小排列
        Comparator com = new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                if(o1 instanceof Students || o2 instanceof Students){
                    Students s1 = (Students)o1;
                    Students s2 = (Students)o2;
                    if(!s1.getName().equals(s2.getName())){
                        return s1.getName().compareTo(s2.getName());
                    }else{
                        return - Integer.compare(s1.getAge(),s2.getAge());
                    }
                }else{
                    throw new RuntimeException("传入数据类型不一致");
                }
            }
        };

        TreeMap map = new TreeMap(com);

        map.put(new Students("Lisa",18),90);
        map.put(new Students("Ann",19),100);
        map.put(new Students("Tom",17),50);
        map.put(new Students("Jack",20),80);
        map.put(new Students("Ann",22),70);

        Set set = map.entrySet();
        Iterator iterator = set.iterator();
        while(iterator.hasNext()){
            System.out.println(iterator.next());
        }
    }

Properties处理属性文件

  • properties类是Hashtable的子类,用于处理属性文件
  • 其中的key与value都是字符串类型
  • 存取数据时,使用setProperties()方法和getProperties()方法
Properties properties = new Properties();
FileInputStream fis = new FileInputStream("jdbc.properties");
properties.load(fis);//加载流对应的文件

System.out.println(properties.getProperty("name"));
System.out.println(properties.getProperty("password"));

具体应用与IO流有关

你可能感兴趣的:(TreeMap与Hashtable的使用)