package September;
public class Person implements Comparable<Person>{
private int id;
private String name;
private int age;
private double height;
public Person() {
super();
// TODO Auto-generated constructor stub
}
public Person(int id, String name, int age, double height) {
super();
this.id = id;
this.name = name;
this.age = age;
this.height = height;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
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;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
@Override
public String toString() {
return "Person [id=" + id + ", name=" + name + ", age=" + age + ", height=" + height + "]";
}
@Override
public int compareTo(Person o) {
return this.getId()-o.getId();
}
}
package September;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class Fifth {
public static void main(String[] args) {
List<Person> list = addPerson();
System.out.println(list);
System.out.println("年龄最大:"+getMaxAge(list));
list = sortPerson(list);
System.out.println(list);
}
public static List<Person> addPerson(){
List<Person> list = new ArrayList();
Set<Person> set = new HashSet<Person>();
while(true) {
set.add(new Person(11,"张三1",20,170));
set.add(new Person(13,"张三2",25,180));
set.add(new Person(12,"张三3",30,190));
set.add(new Person(15,"张三4",50,160));
if(set.size()>=3) {
break;
}
}
for(Person ps:set) {
list.add(ps);
}
return list;
}
//输出年龄最大
public static Person getMaxAge(List<Person> list) {
Person max = list.get(0);
for(Person p:list) {
if(p.getAge()>max.getAge()) {
max = p;
}
}
return max;
}
//按身高排序
public static List<Person> sortPerson(List<Person> list){
Collections.sort(list,new Comparator<Person>() {
@Override
public int compare(Person o1, Person o2) {
return (int)(o2.getHeight()-o1.getHeight());
}
});
return list;
}
}