學習內容:java
Set----保證集合中對象的惟一性,存取無序,能夠存儲null算法
HashSet: 線程不安全,保證惟一性經過hashCode(),equals()安全
TreeSet: 能夠對集合中元素排序,默認升序,能夠經過迭代器調用descendingIeterator()降序, 底層使用二叉樹結構存儲,排序方法:ide
A,自定義Comparator,並構造TreeMap學習
B,javabean實現Comparable接口,重寫compareTo()方法 this
Map----存儲的是鍵值對,其中key必須惟一,能夠爲nullspa
HashTable: 線程安全,速度慢線程
HashMap: 線程不安全,速度快,容許key==nullcode
TreeMap: 按key排序,排序原理同TreeSet對象
-----------------------------------------------------------------------
做業:
1.定義罪犯Criminal類,height(身高)/weight(體重)/blood(血型)/home(籍貫)屬性。
重寫hashcode和equals,使用四個屬性的組合進行實現。
建立HashSet集合,裏面存放20個Criminal對象,其中O型血2人,A型血3人,B型血4人,AB型血 1人,其他血型不詳。
注意:hashcode()方法實現時,要求身高、體重、和血型三個屬性合成一個數字,實現兩兩比較 的高效算法。
package cto51.day10; public class Criminal { private int height; private int weight; /** * 1-AB型; 2-O型;3-A型;4-B型; */ private Integer blood; private int home; public int getHeight() { return height; } public void setHeight(int height) { this.height = height; } public int getWeight() { return weight; } public void setWeight(int weight) { this.weight = weight; } public Integer getBlood() { return blood; } public void setBlood(Integer blood) { this.blood = blood; } public int getHome() { return home; } public void setHome(int home) { this.home = home; } public Criminal(int height, int weight, Integer blood, int home) { super(); this.height = height; this.weight = weight; this.blood = blood; this.home = home; } public Criminal() { super(); } @Override public int hashCode() { int nh = this.getHeight() << 16; int wh = this.getWeight() << 8; return nh + wh + (this.getBlood() == null ? 0 : this.getBlood()); } @Override public boolean equals(Object obj) { if (obj == null) { return false; } if (this == obj) { return true; } if (obj.getClass() == Criminal.class) { Criminal c = (Criminal) obj; if (this.hashCode() == c.hashCode() && this.getHome() == c.getHome()) { return true; } } return false; } @Override public String toString() { return "height:" + this.getHeight() + ",weight:" + this.getWeight() + ",blood:" + this.getBlood() + ",home:" + this.getHome(); } }
package cto51.day10; import java.util.HashSet; import java.util.Iterator; import java.util.TreeSet; public class CollectionDemo2 { public static void main(String[] args) { HashSet<Criminal> cs = new HashSet<>(); for (int i = 1; i < 21; i++) { if (i <= 4) { // int tmp=i; for (int j = 1; j <= i; j++) { // cs.add(new Criminal(i + 100, i * 10, i, ++tmp)); cs.add(new Criminal(i + 100, i * 10, i, i)); } } else { cs.add(new Criminal(i + 100, i * 10, null, i)); } } System.out.println(cs.size()); Iterator<Criminal> it = cs.iterator(); while (it.hasNext()) { Criminal criminal = (Criminal) it.next(); System.out.println(criminal); } Dog d = new Dog("erha2", 1, "white"); Dog d2 = new Dog("藏獒1", 1, "white"); Dog d4 = new Dog("藏獒", 1, "yellow"); Dog d3 = new Dog("erha", 2, "white"); TreeSet<Dog> ds = new TreeSet<>(); ds.add(d); ds.add(d2); ds.add(d3); ds.add(d4); Iterator<Dog> iterator = ds.descendingIterator(); while (iterator.hasNext()) { Dog dog = iterator.next(); System.out.println(dog); } } } class Dog implements Comparable { private String kind; private int age; private String color; public String getKind() { return kind; } public void setKind(String kind) { this.kind = kind; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } public Dog(String kind, int age, String color) { super(); this.kind = kind; this.age = age; this.color = color; } @Override public int compareTo(Object o) { if (o == null) { return -1; } if (this == o) { return 0; } if (o.getClass() == Dog.class) { Dog c = (Dog) o; if (this.getKind().equalsIgnoreCase(c.getKind())) { if (this.getColor().equals(c.getColor())) { return this.getAge() - c.getAge(); } else { return this.getColor().compareTo(c.getColor()); } } else { return this.getKind().compareTo(c.getKind()); } } return -1; } @Override public String toString() { return this.getKind() + " : " + this.getColor() + " : " + this.getAge(); } }
2,map迭代方式
public static void main(String[] args) { Map<Person, Dog> map = new HashMap<Person, Dog>(); for (int i = 0; i < 100; i++) { map.put(new Person(), new Dog("j8", 11, "yellow")); } for (Entry<Person, Dog> en : map.entrySet()) { Person key = en.getKey(); Dog value = en.getValue(); System.out.println(key + "::" + value); } Set<Person> keySet = map.keySet(); for (Person person : keySet) { System.out.println(person + "::" + map.get(person)); } }