919-5

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;
}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值