day11
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了day11相关的知识,希望对你有一定的参考价值。
学习内容:
Set----保证集合中对象的唯一性,存取无序,可以存储null
HashSet: 线程不安全,保证唯一性通过hashCode(),equals()
TreeSet: 可以对集合中元素排序,默认升序,可以通过迭代器调用descendingIeterator()降序, 底层使用二叉树结构存储,排序方法:
A,自定义Comparator,并构造TreeMap
B,javabean实现Comparable接口,重写compareTo()方法
Map----存储的是键值对,其中key必须唯一,可以为null
HashTable: 线程安全,速度慢
HashMap: 线程不安全,速度快,允许key==null
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)); } }
以上是关于day11的主要内容,如果未能解决你的问题,请参考以下文章
19 01 11 javascript ?????????????????????(???????????????) ??????????????????????????????(代码片段