2017.08.13-集合练习

package com.shuhuadream.work;

import java.util.HashSet;
import java.util.Iterator;

/*
 * 练习二:创建GirlFriend对象,name,age,sex,birthday存入HashSet集合中
	1)如果对象所有的属性值都一致,就认为是同一对象,不重复存储,否则就存储,
	      然后两种方式遍历set集合。
	      重写hashCode(),equals(),toString()

	2)如果name,birthday属性相同就视为同一个对象,不重复存储,否则存储	
	      重写hashCode(),equals(),toString()
 */
public class Work01 {
	public static void main(String[] args) {
		HashSet<GirlFriend> hs = new HashSet<GirlFriend>();
		hs.add(new GirlFriend("小昭",23,"女","1994-09-01"));
		hs.add(new GirlFriend("周芷若",23,"女","1994-09-04"));
		hs.add(new GirlFriend("赵敏",23,"女","1994-10-01"));
		hs.add(new GirlFriend("小昭",23,"女","1994-09-01"));
		System.out.println(hs);
		
		//迭代器遍历
		System.out.println("------------------------");
		Iterator<GirlFriend> it = hs.iterator();
		while(it.hasNext()){
			Object next = it.next();
			System.out.println(next);		
		}
		
		//增强for循环遍历
		System.out.println("------------------------");
		for(GirlFriend gf:hs){
			System.out.println(gf);
		}
		
		//把hs转换为数组形式遍历
		System.out.println("------------------------");
		Object[] gf = hs.toArray();
		for(int i=0;i<gf.length;i++){
			System.out.println(gf[i]);
		}
	}
}


class GirlFriend{
	private String name;
	private int age;
	private String sex;
	private String birthday;
	
	
	public GirlFriend(String name, int age, String sex, String birthday) {
		super();
		this.name = name;
		this.age = age;
		this.sex = sex;
		this.birthday = birthday;
	}
	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 String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public String getBirthday() {
		return birthday;
	}
	public void setBirthday(String birthday) {
		this.birthday = birthday;
	}
	
	
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + age;
		result = prime * result + ((birthday == null) ? 0 : birthday.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result + ((sex == null) ? 0 : sex.hashCode());
		return result;
	}
	
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		GirlFriend other = (GirlFriend) obj;
		if (birthday == null) {
			if (other.birthday != null)
				return false;
		} else if (!birthday.equals(other.birthday))
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}
	
	public String toString() {
		return "[姓名:" + name + ", 年龄:" + age + ", 性别:" + sex + ", 生日:" + birthday + "]";
	}
	
	
}
package com.shuhuadream.work;

import java.text.CollationKey;
import java.text.Collator;
import java.util.Comparator;
import java.util.TreeSet;

/*
 * TreeSet的练习:
	课堂练习:成绩单:
	学生:姓名,年龄,成绩
	要求:1.成绩从高到低。
	2.如果成绩相同,年龄从小到大,
	3.如果年龄一致,字典顺序姓名。
	4如果姓名也一致,就同一对象。
 */
public class Work02 {
	public static void main(String[] args) {
		//创建一个比较器对象
		MyComparator comparator = new MyComparator();
		TreeSet<Student> transcript = new TreeSet<Student>(comparator);//创建TreeSet时传入比较器
		transcript.add(new Student("刘德华",21,100));
		transcript.add(new Student("吴京",18,100));
		transcript.add(new Student("王宝强",23,100));
		transcript.add(new Student("成龙",23,90));
		transcript.add(new Student("余罪",16,100));
		transcript.add(new Student("狗娃",23,65));
		transcript.add(new Student("铁蛋",23,100));
		transcript.add(new Student("李小龙",30,100));
		transcript.add(new Student("安迪",23,60));
		transcript.add(new Student("安迪",23,60));
		
		//System.out.println(transcript);
		for(Student stu :transcript){
			System.out.println(stu);
		}
		
	}
}

//自定义比较器
class MyComparator implements Comparator<Student>{

	@Override
	public int compare(Student s1, Student s2) {
		if(s1.getScore()==s2.getScore()){
			if(s1.getAge()==s2.getAge()){
				 if(s1.getName()==s2.getName()){
					 return 0;
				 }
				 CollationKey key = Collator.getInstance().getCollationKey(s1.getName());  
	             CollationKey key2 = Collator.getInstance().getCollationKey(s2.getName());  
	             if(key.compareTo(key2)>0){  
	                 return 1;  
	             }else if(key2.compareTo(key)>0){  
	                 return -1;  
	             }  
			}
			return s1.getAge()-s2.getAge();
		}
		if(s1.getScore()>s2.getScore()){
			return -1;
		}
		return 1;
	}
	
}

//学生类
class Student{
	private String name;
	private int age;
	private double score;
	static int num=1;
	public Student(String name, int age, double score) {
		super();
		this.name = name;
		this.age = age;
		this.score = score;
	}
	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 getScore() {
		return score;
	}
	public void setScore(double score) {
		this.score = score;
	}
	@Override
	public String toString() {
		return num+++" 姓名:" + name + ", 年龄:" + age + ", 分数:" + score ;
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + age;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		long temp;
		temp = Double.doubleToLongBits(score);
		result = prime * result + (int) (temp ^ (temp >>> 32));
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Student other = (Student) obj;
		if (age != other.age)
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		if (Double.doubleToLongBits(score) != Double.doubleToLongBits(other.score))
			return false;
		return true;
	}
	
	
}
package com.shuhuadream.work;

import java.text.CollationKey;
import java.text.Collator;
import java.util.Comparator;
import java.util.TreeSet;

/*课堂练习:人
姓名,年龄,书:书名,价格、

要求:
1.按照年龄,从小到大排序,
2.如果年龄相等,按照姓名字典顺序,
3.如果姓名也一致,按照书的价格共高到低。
4.价格一致,按照书名,
5.书名也一致,认为是同一对象,不存储
用自定义的比较器实现
*/
public class Work03 {
	public static void main(String[] args) {
		//创建一个比较器对象
		MyComparator1 comparator1 = new MyComparator1();
		TreeSet<Person> tree = new TreeSet<Person>(comparator1);
		tree.add(new Person("狗娃",23,new Book("庄子",23)));
		tree.add(new Person("狗娃",22,new Book("三国演义",23)));
		tree.add(new Person("可可",23,new Book("三国演义",23)));
		tree.add(new Person("安迪",23,new Book("三国演义",23)));
		tree.add(new Person("狗娃",23,new Book("庄子",24)));
		tree.add(new Person("狗娃",23,new Book("三国演义",23)));
		
		tree.add(new Person("狗娃",23,new Book("三国演义",23)));
		for(Person p :tree){
			System.out.println(p);
		}
		
	}
}
//自定义比较器
/*1.按照年龄,从小到大排序,
2.如果年龄相等,按照姓名字典顺序,
3.如果姓名也一致,按照书的价格共高到低。
4.价格一致,按照书名,
5.书名也一致,认为是同一对象,不存储*/
class MyComparator1 implements Comparator<Person>{

	public int compare(Person p1, Person p2) {
		if(p1.getAge()==p2.getAge()){
			if(p1.getName()==p2.getName()){
				if(p1.book.getPrice()==p2.book.getPrice()){
					if(p1.book.getBooName()==p1.book.getBooName()){
						return 0;
					}
					CollationKey key = Collator.getInstance().getCollationKey(p1.book.getBooName());  
		            CollationKey key2 = Collator.getInstance().getCollationKey(p1.book.getBooName());  
		            if(key.compareTo(key2)>0){  
		                 return 1;  
		            }else if(key2.compareTo(key)>0){  
		                 return -1;  
		            }  
				}
				if(p1.book.getPrice()>p2.book.getPrice()){
					return -1;
				}
				return 1;
			}
			 CollationKey key = Collator.getInstance().getCollationKey(p1.getName());  
             CollationKey key2 = Collator.getInstance().getCollationKey(p2.getName());  
             if(key.compareTo(key2)>0){  
                 return 1;  
             }else if(key2.compareTo(key)>0){  
                 return -1;  
             }  
		}
		return p1.getAge()-p2.getAge();
	}
	
}
class Person{
	private String name;
	private int age;
	Book book;
	public Person(String name, int age, Book book) {
		super();
		this.name = name;
		this.age = age;
		this.book = book;
	}
	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 Book getBook() {
		return book;
	}
	public void setBook(Book book) {
		this.book = book;
	}
	
	public String toString() {
		return " [姓名:" + name + ", 年龄:" + age + ", 书:" + book + "]";
	}
	
}

class Book{
	private String booName;
	private double price;
	public Book(String booName, double price) {
		super();
		this.booName = booName;
		this.price = price;
	}
	public String getBooName() {
		return booName;
	}
	public void setBooName(String booName) {
		this.booName = booName;
	}
	public double getPrice() {
		return price;
	}
	public void setPrice(double price) {
		this.price = price;
	}
	@Override
	public String toString() {
		return "[书名:" + booName + ",价格:" + price + "¥]";
	}
	
	
}
package com.shuhuadream.work;

import java.util.TreeSet;

/*1.将String类型的字符串:str = "23 12 45 78 19 25",中的数值,进行排序。建议,将字符串切割,
得到的数据存入TreeSet集合中。
*/
public class Work05 {
	public static void main(String[] args) {
		String str = "23 12 45 78 19 25";
		String[] arr = str.split(" ");
		TreeSet<Integer> tree = new TreeSet<Integer>();
		for(String s:arr){
			int num = Integer.parseInt(s);
			tree.add(num);
		}
		System.out.println(tree);
	}
}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

麦客子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值