注册 登录
编程论坛 JAVA论坛

关于set方法的使用和Comparator接口的使用

xbanyue 发布于 2018-06-13 11:50, 2536 次点击
public class Person implements Comparable{   
    int id;
    int age;
    String name;
    public Person(int id, int age, String name) {
        super();
        this.id = id;
        this.age = age;
        this.name = name;
    }
    public String toString() {
        return "Person [id=" + id + ", age=" + age + ", name=" + name + "]";
    }
    public int compareTo(Object o) {
        Person p;
        if(o instanceof Person) {
            p=(Person)o;
        }else {
            return -1;   
        }
        int diff=this.id-p.id;
        if(diff!=0) {
            diff=diff/Math.abs(diff);
        }
        return diff;
    }   
}
//////////上面的是person类/////////
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;
public class Demo  {
    public static void main(String[] args) {
        Set set=new TreeSet();
        Person p1=new Person(1,18,"小明");
        Person p2=new Person(3,17,"小明");
        Person p3=new Person(2,15,"小李");
        set.add(p1);
        set.add(p2);
        set.add(p3);
        System.out.println(set.size());
        Iterator it=set.iterator();
        while(it.hasNext()){
            System.out.println(it.next());
        }
    }
}
//////下面的是dome类(包含主程序入口)//////////
下面是打印结果
3
Person [id=1, age=18, name=小明]
Person [id=2, age=15, name=小李]
Person [id=3, age=17, name=小明]
我想问的是主程序中是如果调用了person类中的方法啊,定义了person对象,但并没有调用方法,整个程序给我感觉一脸懵的感觉。
5 回复
#2
Gband2018-06-14 21:58
Treeset类会自动调用
#3
疯狂的小a2018-06-14 22:39
回复 楼主 xbanyue
程序代码:
package demo;

import static java.lang.System.out;

/*下面是打印结果
3
Person [id=1, age=18, name=小明]
Person [id=2, age=15, name=小李]
Person [id=3, age=17, name=小明]
我想问的是主程序中是如果调用了person类中的方法啊,定义了person对象,但并没有调用方法,整个程序给我感觉一脸懵的感觉。
*/
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;
@SuppressWarnings("all")
public class Demo  {
    public static void main(String[] args) {
        Set set=new TreeSet();
        Person p1=new Person(1,18,"小明");
        Person p2=new Person(3,17,"小明");
        Person p3=new Person(2,15,"小李");
        set.add(p1);
        set.add(p2);
        set.add(p3);
        System.out.println(set.size());
        Iterator it=set.iterator();
        while(it.hasNext()){
            System.out.println(it.next());
        }
        
        //person类就是一个普通的类,只不过有个比较属性的方法,返回了属性的差值
        int result = (p2);
        out.println(result);
    }
}
@SuppressWarnings("all")
class Person implements Comparable{   
    int id;
    int age;
    String name;
    public Person(int id, int age, String name) {
        super();
        this.id = id;
        this.age = age;
        this.name = name;
    }
    public String toString() {
        return "Person [id=" + id + ", age=" + age + ", name=" + name + "]";
    }
    public int compareTo(Object o) {
        Person p;
        if(o instanceof Person) {
            p=(Person)o;
        }else {
            return -1;   
        }
        int diff=this.id-p.id;
        if(diff!=0) {
            diff=diff/Math.abs(diff);
        }
        return diff;
    }   
}
#4
静水且流深2018-06-14 23:22
程序用的是Comparable接口
排序是按照重写的比较规则实现的排序
即compareTo()方法
指定比较器的类型
程序代码:

public class Person implements Comparable<Person>{   
    int id;
    int age;
    String name;
    public Person(int id, int age, String name) {
        super();
        this.id = id;
        this.age = age;
        this.name = name;
    }
    public String toString() {
        return "Person [id=" + id + ", age=" + age + ", name=" + name + "]";
    }
    public int compareTo(Person o) {
        if(o == null) {
            return -1;
        }
        return id - o.id;
    }   
}
#5
壹只小小鸟2018-06-16 18:39
好难受,都看不明白你想问什么?
1