TreeSet引发的元素重复思考

----测试类

package TreeSetTest;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeSet;


public class TreeSetTest {
	
	
	public static void main(String[] args) {
		/*map循环输出*/
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("1", "a");
		map.put("2", "a");
		map.put("3", "a");
		map.put("4", "a");
		Set<Entry<String, Object>> set = map.entrySet();//map中可以看做是多个map.entry的接口对象
		System.out.println(set);//[3=a, 2=a, 1=a, 4=a]
		Iterator<Entry<String, Object>>  it = set.iterator();//Collection实现了Iterable<T> 接口,故可以该接口提供的迭代方法
		while (it.hasNext()) {
			Entry<String, Object> entry = it.next();
			System.out.println(entry.getKey() + "==" + entry.getValue());//再利用Map.Entry提供的方法,遍历,map主要用来查找,集合用来输出hash都是无序的
		}
		/*TreeSet 实现对象比较,必须实现Comparable接口,自定义  排序和过滤重复的信息*/
		Set<Student> setStu = new TreeSet<Student>();
		setStu.add(new Student("胡明明","24"));
		setStu.add(new Student("胡明明","24"));
		setStu.add(new Student("胡明明1","25"));
		setStu.add(new Student("胡明明2","25"));
		setStu.add(new Student("肖体秀","23"));
		setStu.add(new Student("王二小","22"));
		System.out.println(setStu);
		/**
		 *  虽然TreeSet能实现重复元素的判断,但它只适用于排序类操作的环境下,而其它子类需要消除重复元素该怎么做??比如hashSet,hashmap
		 *  依靠Object类提供的两个方法hashcode和equals  假设equals和hashcode返回的结果都一致则重复
		 */
		Set<Book> hashSet = new HashSet<Book>();
		hashSet.add( new Book("java", 78));
		hashSet.add( new Book("java", 78));
		hashSet.add(new Book("php", 78));
		System.out.println(hashSet);//[Book [titie=java, price=78], Book [titie=php, price=78]]
		//为了确保map中key的唯一性,也可以重写equals和hashcode方法
		Map<Book,Integer> mmp = new HashMap<Book, Integer>();
		mmp.put( new Book("js", 78),1);
		mmp.put( new Book("js", 78),1);
		mmp.put(new Book("html", 78),1);
		System.out.println(mmp);//{Book [titie=js, price=78]=1, Book [titie=html, price=78]=1}
	}
	 
}

----学生类

package TreeSetTest;

public class Student implements Comparable<Student>{
		private String name;
		private String age;
		
		public Student(String name, String age) {
			super();
			this.name = name;
			this.age = age;
		}
		public String getName() {
			return name;
		}
		public void setName(String name) {
			this.name = name;
		}
		public String getAge() {
			return age;
		}
		public void setAge(String age) {
			this.age = age;
		}
		@Override
		//备注:比较对象时TreeSet<Objetc>,假设类中存了5个属性,但是我们只比较了3个,并且这3个属性还相同,则TreeSet会认为这个对象也相同,从而会新的替换旧的(有序),造成数据遗失
		public int compareTo(Student o) {
			//如何比较自己自定义吧,我这里就暂时认定为假设名字相同和年龄相同则认为元素重复,在比较年龄,按大小排序
			if(this.age.equals(o.age) && this.name.equals(o.name)){
				return 0;
			}else if(Integer.parseInt(this.age )>=Integer.parseInt(o.age)){
				return 1;
			}else{
				return -1;
			}
		}
		@Override
		public String toString() {
			return name +":" + age;
		}
		
	
}

----Book类


package TreeSetTest;

public class Book {
	private String titie;
	private int price;

	public Book(String titie, int price) {
		super();
		this.titie = titie;
		this.price = price;
	}
	public String getTitie() {
		return titie;
	}
	public void setTitie(String titie) {
		this.titie = titie;
	}
	public int getPrice() {
		return price;
	}
	public void setPrice(int price) {
		this.price = price;
	}
	@Override
	public String toString() {
		return "Book [titie=" + titie + ", price=" + price + "]";
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + price;
		result = prime * result + ((titie == null) ? 0 : titie.hashCode());
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Book other = (Book) obj;
		if (price != other.price)
			return false;
		if (titie == null) {
			if (other.titie != null)
				return false;
		} else if (!titie.equals(other.titie))
			return false;
		return true;
	}
	
}

猜你喜欢

转载自blog.csdn.net/qq_40826106/article/details/82999064