笔记:
var1 == var2 ?
-------------------------
stu.getClass() == Student.class ? //精准判断 Class clazz = Student.class
stu instanceof Student ? //不是精准判断。
Class clazz = List.class ;
Class clazz2= ... ;
clazz2 == clazz ?
xxx.getClass() ;
成员变量 === 属性 === 字段 === Field
成员函数 === 方法 === Method
构造函数 === 构造器 === 构造子 === Constructor
类 === Class
TreeSet
---------------
1.使用比较方法判断对象是否重复。
2.比较方法实现有两种
a)自定义Comparator比较器,和TreeSet关联。
b)让javaBean实现Comparable接口,实现CompareTo()方法。
3.TreeSet可以容纳null元素。
4.TreeSet可以使用降序排列。
通过descendingIterator()方法得到降序迭代器实现。
5.TreeSet默认升序排列。
HashMap 1 key-value 键值对
2 Entey 每一对Key和value
3 每一个key存在keySet中通过map.keySet()获取
4 遍历有两种方式:
KeySet遍历 通过Map.get(Object key)获取value的值
EnetySet遍历 通过map.entrySet()获取Entey的Set集合后 循环遍历Entey的key或value
效率上来说HashMap 比HashSet更快 因为是使用唯一的键来获取对象
作业:
1.定义罪犯Criminal类,height(身高)/weight(体重)/blood(血型)/home(籍贯)属性。
重写hashcode和equals,使用四个属性的组合进行实现。 创建HashSet集合,里面存放20个Criminal对 象,其中O型血2人,A型血3人,B型血4人,AB型血1人,其余血型不详。
注意:hashcode()方法实现时,要求身高、体重、和血型三个属性合成一个数字, 实现两两比较的高效 算法
代码:
package com.work.eleven; import com.work.eleven.*; public class Criminal implements Comparable{ private int height; private int weight; private int blood; private String 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 int getBlood() { return blood; } public void setBlood(int blood) { this.blood = blood; } public String getHome() { return home; } public void setHome(String home) { this.home = home; } public Criminal(int height, int weight, String bloods,String home) { String blood1=bloods.toLowerCase(); if (height <= 0) { System.out.println("身高需为正数 "); System.exit(-1); } else if (weight <= 0) { System.out.println("体重需为正数 "); System.exit(-1); } else if (!blood1.equals("a")&&!blood1.equals("b")&&!blood1.equals("ab")&&!blood1.equals("o")&&!blood1.equals("")) { System.out.println("血型错误 "); System.exit(-1); } else { this.height=height; this.weight=weight; if(blood1.equals("")) this.blood=0; if(blood1.equals("a")) this.blood=1; if(blood1.equals("b")) this.blood=2; if(blood1.equals("ab")) this.blood=3; if(blood1.equals("o")) this.blood=4; this.home= home; } } @Override public int compareTo(Object obj) { if(obj==null) return 1; else{ if(obj instanceof Criminal){ Criminal criminal=(Criminal)obj; //比较身高 int heightResult = this.height-criminal.height; // 身高不一样 if(heightResult!=0) return heightResult; else { //比较体重 int weightResult = this.weight-criminal.weight; // 体重不一样 if(weightResult!=0) return weightResult; else{ //比较血型 int bloodResult = this.blood-criminal.blood; // 血型不一样 if(bloodResult!=0) { return bloodResult; } else{ return this.home.compareTo(criminal.home); } } } } return 0; } } /** * 重写hashcode方法 */ @Override public int hashCode() { int h0 = 0 << 32; //身高左16位 int h1 = (ArrayTools.int2Bytes(height)[0] & 0xff) << 16; /*System.out.println("h1= "+h1);*/ //体重左移8位 int h2 = (ArrayTools.int2Bytes(weight)[0] & 0xff) << 8; /*System.out.println("h2= "+h2);*/ //血型位置不变 int h3 = (ArrayTools.int2Bytes(blood)[0] & 0xff) << 0; /*System.out.println("h3= "+h3);*/ int identityResult = h0 | h1 | h2 | h3; /*System.out.println("identityResult= "+identityResult);*/ return home == null ? identityResult : identityResult + home.hashCode(); } } package com.work.eleven; public class ArrayTools { /** * * 整数转换为字节数组 ;向右移位,然后强转截断。 */ public static byte[] int2Bytes(int i) { byte[] bytes = new byte[4]; bytes[0] = (byte) i;// 低位 /*System.out.println("bytes[0]= "+bytes[0]);*/ bytes[1] = (byte) (i >> 8);// 次低位 /*System.out.println("bytes[1]= "+bytes[1]);*/ bytes[2] = (byte) (i >> 16);// 次高位 /*System.out.println("bytes[2]= "+bytes[2]);*/ bytes[3] = (byte) (i >> 24); // 高位 /*System.out.println("bytes[3]= "+bytes[3]);*/ return bytes; } /** * * 字节数组 转 为整数 ,向左移位,由于向左移位,先转换为整数 ,缺的用符号位补齐,故用&0xFF去除 */ /* public static int bytes2Int(byte[] bytes) { int i0 = bytes[3] << 24; int i1 = (bytes[2] & 0xFF) << 16; int i2 = (bytes[1] & 0xFF) << 8; int i3 = (bytes[0] & 0xFF) << 0; return i0 | i1 | i2 | i3; }*/ } package com.work.eleven; import java.util.HashSet; import java.util.Iterator; import java.util.Set; public class HashSetCriminal { public static void main(String[] args) { Set<Criminal> criminalset = new HashSet<Criminal>(); criminalset.add(new Criminal(175,165,"A","Beijing")); criminalset.add(new Criminal(176,165,"A","Beijing")); criminalset.add(new Criminal(177,166,"A","Beijing")); criminalset.add(new Criminal(178,166,"O","Beijing")); criminalset.add(new Criminal(179,167,"O","Beijing")); criminalset.add(new Criminal(180,167,"B","Beijing")); criminalset.add(new Criminal(181,168,"B","Beijing")); criminalset.add(new Criminal(181,168,"B","Beijing")); criminalset.add(new Criminal(181,169,"B","Beijing")); criminalset.add(new Criminal(182,169,"AB","Beijing")); criminalset.add(new Criminal(182,170,"","Beijing")); criminalset.add(new Criminal(183,170,"","Beijing")); criminalset.add(new Criminal(183,171,"","Beijing")); criminalset.add(new Criminal(184,171,"","Beijing")); criminalset.add(new Criminal(184,172,"","Beijing")); criminalset.add(new Criminal(185,172,"","Beijing")); criminalset.add(new Criminal(185,173,"","Beijing")); criminalset.add(new Criminal(186,173,"","")); criminalset.add(new Criminal(186,174,"","")); criminalset.add(new Criminal(187,174,"","")); System.out.println(criminalset.size()); for(Iterator<Criminal> it = criminalset.iterator() ; it.hasNext() ; ){ System.out.println(it.next()); } } }
运行结果:
20
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
作业2:
key(键) - value(值) :kv对.
创建HashMap,Person为key,Dog为value。
存放100元素,遍历map集合,两种方式。EntrySet + KeySet.
删除操作。remove();
代码:
package com.work.eleven; /** * */ public class Dog implements Comparable{ private String color = "" ; private int weight ; private String category = ""; public Dog(String color, int weight, String category) { if(color == null) throw new NullPointerException("color不能为空!"); if(category == null) throw new NullPointerException("category不能为空!"); this.color = color; this.weight = weight; this.category = category; } public String getColor() { return color; } public void setColor(String color) { if(color == null) throw new NullPointerException("color不能为空!"); this.color = color; } public int getWeight() { return weight; } public void setWeight(int weight) { this.weight = weight; } public String getCategory() { return category; } public void setCategory(String category) { if(category == null) throw new NullPointerException("category不能为空!"); this.category = category; } public int compareTo(Object o){ if(o == null){ return 1 ; } // if(o instanceof Dog){ Dog dog = (Dog)o ; int catResult = this.category.compareTo(dog.category); //比较品种 if(catResult != 0){ return catResult ; } //品种相同 else{ //比较 int colRes = this.color.compareTo(dog.color); //颜色 不同 if(colRes != 0){ return colRes ; } //颜色相同 else{ //比较体重 return this.weight - dog.weight ; } } } return -1 ; } /** * 重写toString方法() */ public String toString() { return "category= "+category+" color= "+color+" weight= "+weight + " Dog.hascode= "+hashCode(); } } package com.work.eleven; /** * hashcode + equlas */ public class Person { private String name; private int age; public Person(String name, int age) { super(); this.name = name; this.age = age; } 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 int hashCode() { return name == null ?age : name.hashCode() + age ; } public String toString() { return "name= "+name+" age= "+age+" Person.hascode= "+hashCode(); } public boolean equals(Object obj) { if(obj == null) return false ; if(obj == this) return true ; //精准判断 if(obj.getClass() == Person.class){ Person p = (Person)obj; //name是否相同 boolean nameEqu = false ; if(this.name == null){ if(p.name == null){ nameEqu =true ; } else{ nameEqu = false ; } } //name 不null else{ nameEqu = name.equals(p.name); } //age 是否相同 boolean ageEqu = (this.age == p.age) ; // return nameEqu && ageEqu ; } return false ; } } package com.work.eleven; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Map.Entry; import java.util.Set; public class PerDogMap { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Map<Person, Dog> pdmap=new HashMap<Person, Dog>(); for (int i=1;i<101;i++){ pdmap.put(new Person("Bruce"+i,i+10), new Dog("color"+i, i+10,"category"+i)); } System.out.println("===============================================keySet遍历====================================================="); //KeySet遍历 Set<Person> personKeySet=pdmap.keySet(); for(Person person : personKeySet){ System.out.println("Person : "+person.toString()+" and Dog : "+pdmap.get(person).toString()); } System.out.println("===============================================EntrySet遍历====================================================="); Set<Entry<Person, Dog>> personEnteySet=pdmap.entrySet(); for(Entry<Person,Dog> personEntey : personEnteySet){ System.out.println("Person : "+personEntey.getKey().toString()+" and Dog : "+personEntey.getValue().toString()); } System.out.println("===============================================Remove====================================================="); Iterator<Person> personiterator = pdmap.keySet().iterator(); Dog dog; for(int j=90;j<101;j++){ dog= pdmap.remove(new Person("Bruce"+j,j+10)); System.out.println("删除了"+dog.toString()); } } }
运行结果(部分):
Person : name= Bruce62 age= 72 Person.hascode= 1820524139 and Dog : category= category62 color= color62 weight= 72 Dog.hascode= 246507189
Person : name= Bruce61 age= 71 Person.hascode= 1820524137 and Dog : category= category61 color= color61 weight= 71 Dog.hascode= 550370460
Person : name= Bruce60 age= 70 Person.hascode= 1820524135 and Dog : category= category60 color= color60 weight= 70 Dog.hascode= 864253591
Person : name= Bruce100 age= 110 Person.hascode= 601666520 and Dog : category= category100 color= color100 weight= 110 Dog.hascode= 2078199276
Person : name= Bruce69 age= 79 Person.hascode= 1820524153 and Dog : category= category69 color= color69 weight= 79 Dog.hascode= 1621196924
Person : name= Bruce68 age= 78 Person.hascode= 1820524151 and Dog : category= category68 color= color68 weight= 78 Dog.hascode= 184188532
Person : name= Bruce67 age= 77 Person.hascode= 1820524149 and Dog : category= category67 color= color67 weight= 77 Dog.hascode= 1132721997
Person : name= Bruce66 age= 76 Person.hascode= 1820524147 and Dog : category= category66 color= color66 weight= 76 Dog.hascode= 216072924
Person : name= Bruce65 age= 75 Person.hascode= 1820524145 and Dog : category= category65 color= color65 weight= 75 Dog.hascode= 142786591
===============================================Remove======================================
删除了category= category90 color= color90 weight= 100 Dog.hascode= 1157436341
删除了category= category91 color= color91 weight= 101 Dog.hascode= 1048575489
删除了category= category92 color= color92 weight= 102 Dog.hascode= 753416466
删除了category= category93 color= color93 weight= 103 Dog.hascode= 1879089963
删除了category= category94 color= color94 weight= 104 Dog.hascode= 1057945868
删除了category= category95 color= color95 weight= 105 Dog.hascode= 776412720
删除了category= category96 color= color96 weight= 106 Dog.hascode= 454535357
删除了category= category97 color= color97 weight= 107 Dog.hascode= 721042113
删除了category= category98 color= color98 weight= 108 Dog.hascode= 1988716027
删除了category= category99 color= color99 weight= 109 Dog.hascode= 977199748
删除了category= category100 color= color100 weight= 110 Dog.hascode= 2078199276
3.HashTable:线程安全的。
代码:
package com.work.eleven; import java.util.HashSet; import java.util.Hashtable; import java.util.Map; import java.util.Map.Entry; import java.util.Set; public class HashTableDemo { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Map<Person, Dog> pdmap = new Hashtable<Person, Dog>(); for (int i=1;i<11;i++){ pdmap.put(new Person("Bruce"+i,i+10), new Dog("color"+i, i+10,"category"+i)); } System.out.println("===========keySet遍历=================="); Set<Person> personKeySet=pdmap.keySet(); for(Person person : personKeySet){ System.out.println("Person : "+person.toString()+" and Dog : "+pdmap.get(person).toString()); } System.out.println("===========EntrySet遍历=================="); Set<Entry<Person, Dog>> personEntrySet =pdmap.entrySet(); for(Entry<Person, Dog> personEntry : personEntrySet){ System.out.println("Person : "+personEntry.getKey().toString()+" and Dog : "+personEntry.getValue().toString()); } System.out.println("===========Remove=================="); Dog dog; for(int j=1;j<5;j++){ dog= pdmap.remove(new Person("Bruce"+j,j+10)); System.out.println("删除了"+dog.toString()); } System.out.println("剩余Dog数量 :"+pdmap.size()+" 只"); } } /* 运行结果: ===========keySet遍历================== Person : name= Bruce5 age= 15 Person.hascode= 1998389245 and Dog : category= category5 color= color5 weight= 15 Dog.hascode= 1701381926 Person : name= Bruce4 age= 14 Person.hascode= 1998389243 and Dog : category= category4 color= color4 weight= 14 Dog.hascode= 1381270477 Person : name= Bruce3 age= 13 Person.hascode= 1998389241 and Dog : category= category3 color= color3 weight= 13 Dog.hascode= 714682869 Person : name= Bruce2 age= 12 Person.hascode= 1998389239 and Dog : category= category2 color= color2 weight= 12 Dog.hascode= 798941612 Person : name= Bruce1 age= 11 Person.hascode= 1998389237 and Dog : category= category1 color= color1 weight= 11 Dog.hascode= 1743911840 Person : name= Bruce10 age= 20 Person.hascode= 1820523930 and Dog : category= category10 color= color10 weight= 20 Dog.hascode= 1069480624 Person : name= Bruce9 age= 19 Person.hascode= 1998389253 and Dog : category= category9 color= color9 weight= 19 Dog.hascode= 322722178 Person : name= Bruce8 age= 18 Person.hascode= 1998389251 and Dog : category= category8 color= color8 weight= 18 Dog.hascode= 1595436971 Person : name= Bruce7 age= 17 Person.hascode= 1998389249 and Dog : category= category7 color= color7 weight= 17 Dog.hascode= 1028355155 Person : name= Bruce6 age= 16 Person.hascode= 1998389247 and Dog : category= category6 color= color6 weight= 16 Dog.hascode= 616699029 ===========EntrySet遍历================== Person : name= Bruce5 age= 15 Person.hascode= 1998389245 and Dog : category= category5 color= color5 weight= 15 Dog.hascode= 1701381926 Person : name= Bruce4 age= 14 Person.hascode= 1998389243 and Dog : category= category4 color= color4 weight= 14 Dog.hascode= 1381270477 Person : name= Bruce3 age= 13 Person.hascode= 1998389241 and Dog : category= category3 color= color3 weight= 13 Dog.hascode= 714682869 Person : name= Bruce2 age= 12 Person.hascode= 1998389239 and Dog : category= category2 color= color2 weight= 12 Dog.hascode= 798941612 Person : name= Bruce1 age= 11 Person.hascode= 1998389237 and Dog : category= category1 color= color1 weight= 11 Dog.hascode= 1743911840 Person : name= Bruce10 age= 20 Person.hascode= 1820523930 and Dog : category= category10 color= color10 weight= 20 Dog.hascode= 1069480624 Person : name= Bruce9 age= 19 Person.hascode= 1998389253 and Dog : category= category9 color= color9 weight= 19 Dog.hascode= 322722178 Person : name= Bruce8 age= 18 Person.hascode= 1998389251 and Dog : category= category8 color= color8 weight= 18 Dog.hascode= 1595436971 Person : name= Bruce7 age= 17 Person.hascode= 1998389249 and Dog : category= category7 color= color7 weight= 17 Dog.hascode= 1028355155 Person : name= Bruce6 age= 16 Person.hascode= 1998389247 and Dog : category= category6 color= color6 weight= 16 Dog.hascode= 616699029 ===========Remove================== 删除了category= category1 color= color1 weight= 11 Dog.hascode= 1743911840 删除了category= category2 color= color2 weight= 12 Dog.hascode= 798941612 删除了category= category3 color= color3 weight= 13 Dog.hascode= 714682869 删除了category= category4 color= color4 weight= 14 Dog.hascode= 1381270477 剩余Dog数量 :6 只*/