集合的一些练习题2(Collection+Map)

题目四:2.定义一个Person类型(属性私有、空参有参构造、set、get方法、toString方法、equals方法、hashCode方法),
  包含两个属性:姓名(String name)、年龄(int age)
  List集合中存储多个Person对象,获取年龄最大的Person对象并打印信息

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
public class List_PersonCMP_high_to_print {

	public static void main(String[] args) {
		List<PersonMen> list=new ArrayList<PersonMen>();
		list.add(new PersonMen("AAA", 100));
		list.add(new PersonMen("BBB",90));
		list.add(new PersonMen("CCC", 120));
		list.add(new PersonMen("AAA", 100));
		list.add(new PersonMen("BBB",90));
		list.add(new PersonMen("CCC", 120));
		HQ(list);

	}

	//简单方法
	private static void HQ(List<PersonMen> list) {
		int max=list.get(0).age;
		int index=0;
		for (int i = 1; i < list.size(); i++) {
			if (max<list.get(i).age) {
				max=list.get(i).age;
				index=i;
			}
		}
		System.out.println(list.get(index));
	}

}

class PersonMen{
	
	String name;
	int age;
	
	public PersonMen() {
		super();
		// TODO Auto-generated constructor stub
	}

	public PersonMen(String name, int age) {
		super();
		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 "PersonMen [name=" + name + ", age=" + age + "]";
	}

	@Override
	public int hashCode() {
		return name.hashCode()+age*1000;
	}

	@Override
	public boolean equals(Object obj) {
		if (!(obj instanceof PersonMen)) {
			throw new ClassCastException("类型错误");
		}
		PersonMen personMen=(PersonMen)obj;
		return this.name.equals(personMen.name)&&this.age==personMen.age;
	}
	
	
}

题目五:

1、声明一个Book类型,有编号、书名、作者、价格,销量
2、添加到一个集合中,要求不可重复,规定编号相同就认为是一本书
    提示:HashSet
3、添加到一个集合中,要求不可重复,但是要求按照销量从高到低排序
    提示:TreeSet
4、添加到一个集合中,要求不可重复,但是要求这次按照价格从低到高排序    
    提示:TreeSet
5、添加到一个集合中,要求不可重复,但是要求按照添加顺序显示
    提示:LinkedHashSet


import java.util.Comparator;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.TreeSet;


public class ZongHeYY {

	public static void main(String[] args) {
		Set<Object> set1=new HashSet<Object>();
		//试试去重
		System.out.println("去重:");
		set1.add(new Book(1001,"边城","沈以诚", 29, 10000));
		set1.add(new Book(1002,"朝花夕拾","鲁迅", 29, 10001));
		set1.add(new Book(1001,"边城","沈以诚", 29, 10003));
		for (Object object : set1) {
			System.out.println(object);
		}
		System.out.println("********************************");
		System.out.println("按照销量从高到低排序:");
		CompareWithSale compareWithSale=new CompareWithSale();
		Set<Object> set2=new TreeSet<Object>(compareWithSale);
		set2.add(new Book(1003,"边城1","沈以诚", 29, 10001));
		set2.add(new Book(1004,"边城2","沈以诚", 30, 10002));
		set2.add(new Book(1005,"边城3","沈以诚", 31, 10003));
		for (Object object : set2) {
			System.out.println(object);
		}
		System.out.println("********************************");
		System.out.println("按照价格从低到高排序:");
		CompareWithPrice compareWithprice=new CompareWithPrice();
		Set<Object> set3=new TreeSet<Object>(compareWithprice);
		set3.add(new Book(1003,"边城1","沈以诚", 100, 10001));
		set3.add(new Book(1004,"边城2","沈以诚", 350, 10002));
		set3.add(new Book(1005,"边城3","沈以诚", 310, 10003));
		for (Object object : set3) {
			System.out.println(object); 
		}
		System.out.println("********************************");
		LinkedHashSet<Object> set4=new LinkedHashSet<Object>();
		System.out.println("不可重复,按照添加顺序显示:");
		set4.add(new Book(1003,"边城1","沈以诚", 100, 10001));
		set4.add(new Book(1004,"边城2","沈以诚", 350, 10002));
		set4.add(new Book(1005,"边城3","沈以诚", 310, 10003));
		set4.add(new Book(1003,"边城1","沈以诚", 100, 10001));
		set4.add(new Book(1004,"边城2","沈以诚", 350, 10002));
		set4.add(new Book(1005,"边城3","沈以诚", 310, 10003));
		for (Object object : set4) {
			System.out.println(object); 
		}

	}

}
//创建比较器,按照销量从高到低排序 后面的大于前面的
class CompareWithSale implements Comparator<Object>{

	@Override
	public int compare(Object o1, Object o2) {
		Book book1=(Book)o1;
		Book book2=(Book)o2;
		int hTOl =book2.sale-book1.sale;//后面的大于前面的
		return hTOl;
	}
	
}
//创建比较器 要求这次按照价格从低到高排序
class CompareWithPrice implements Comparator<Object>{

	@Override
	public int compare(Object o1, Object o2) {
		Book book3=(Book)o1;
		Book book4=(Book)o2;
		int lTOh=book3.price-book4.price;
		return lTOh;
	}
	
}
class Book implements Comparable<Object>{
	int id;
	String name;
	String author;
	int price;
	int sale;
	public Book() {
		super();
		// TODO Auto-generated constructor stub
	}
	public Book(int id, String name, String author, int price, int sale) {
		super();
		this.id = id;
		this.name = name;
		this.author = author;
		this.price = price;
		this.sale = sale;
	}
	@Override
	public int hashCode() {
		
		return name.hashCode()+id*1000;
	}
	@Override
	public boolean equals(Object obj) {
		Book book=(Book)obj;
		return this.name.equals(book.name)&&this.id==book.id&&this.author.equals(book.author);
	}
	@Override
	public int compareTo(Object o) {
		Book book=(Book)o;
		//先比较书名
		int num = book.name.compareTo(this.name);
		if (num==0) {
			return this.id-book.id;
		}
		else{
			return num;
		}
	}
	@Override
	public String toString() {
		return "Book [id=" + id + ", name=" + name + ", author=" + author + ", price=" + price + ", sale=" + sale + "]";
	}
	
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值