TreeSet引發的元素重複思考
阿新 • • 發佈:2018-11-04
----測試類 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; } }