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 stub Map<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; } }
原文地址:http://wwywinnie.blog.51cto.com/10421959/1775471