標籤:hashmap keyset entryset
1.定義罪犯Criminal類,height(身高)/weight(體重)/blood(血型)/home(籍貫)屬性。
重寫hashcode和equals,使用四個屬性的組合進行實現。
建立HashSet集合,裡面存放20個Criminal對象,其中O型血2人,A型血3人,B型血4人,AB型血1人,其餘血型不詳。
注意:hashcode()方法實現時,要求身高、體重、和血型三個屬性合成一個數字,實現兩兩比較的高效演算法。
2.Map:
-----------
key(鍵) - value(值) :kv對.
建立HashMap,Person為key,Dog為value。
存放100元素,遍曆map集合,兩種方式。EntrySet + KeySet.
刪除操作。remove();
1.
package it18zhang;public class ArrayTools {/* * 整數轉換為位元組數組;向右移位;截斷後或運算 * *///public static void main(String[] args) {// TODO Auto-generated method stub//}public static byte[] int2Bytes(int i){/* * 整數轉換為位元組數組 */byte[] bytes=new byte[4];bytes[0]=(byte) i;bytes[1]=(byte)(i>>8);bytes[2]=(byte)(i>>16);bytes[3]=(byte)(i>>24);return bytes;}/** *位元組轉換為整數,向左移位 */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 it18zhang;public class ArrayTools {/* * 整數轉換為位元組數組;向右移位;截斷後或運算 * *///public static void main(String[] args) {// TODO Auto-generated method stub//}public static byte[] int2Bytes(int i){/* * 整數轉換為位元組數組 */byte[] bytes=new byte[4];bytes[0]=(byte) i;bytes[1]=(byte)(i>>8);bytes[2]=(byte)(i>>16);bytes[3]=(byte)(i>>24);return bytes;}/** *位元組轉換為整數,向左移位 */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 it18zhang;public class Criminal {private int height;private int weight;private String blood;private String home;public Criminal(int height,int weight,String blood){if(height <= 0){System.out.println("身高不能為負數");System.exit(-1);}else if(weight<=0){System.out.println("體重不能為負數");System.exit(-1);;}else if(blood == "A" && blood == "B" && blood =="AB" && blood =="O"){System.out.println("血型不正確");System.exit(-1);}else if(home==null){System.out.println("無家可歸");System.exit(-1);}else{this.height=height;this.weight=weight;this.blood=blood;this.home=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 String getBlood() {return blood;}public void setBlood(String blood) {this.blood = blood;}public String getHome() {return home;}public void setHome(String home) {this.home = home;}/* * 重寫hasecode方法 */public int hashCode(){int i0=0<<24;int i1=(ArrayTools.int2By)}public boolean equals(Object obj){if(obj==null)return false;if(obj==this)return true;if(obj.getClass()==Criminal.class){Criminal c=(Criminal) obj;if(c.blood==this.blood && c.height==this.height && c.weight==this.weight && c.home==this.home){return true;}}return false;}}2.package it18zhang;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.Map.Entry;public class HashMapTest {public static void main(String[] args) {// TODO Auto-generated method stubMap<Person,Dog> map=new HashMap<Person,Dog>();List<Person> keys=new ArrayList<Person>();for(int i=0;i<100;i++){Person p=new Person("person"+i,i);map.put(p,new Dog("dog"+i,i));keys.add(p);}System.out.println("-----keySet-------");for(Person p : map.keySet()){System.out.println(p.getName()+" "+map.get(p).getName());}System.out.println("----------EntrySet----------");for(Entry<Person,Dog> entry : map.entrySet()){System.out.println(entry.getKey().getName());}}}package it18zhang;public class Person {private String name;private int age;public Person(String name,int age){super();this.age=age;this.name=name;}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;}}package it18zhang;public class Dog {private String name;private int age;public Dog(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;}}
大資料第11天