1.定义罪犯Criminal类,height(身高)/weight(体重)/blood(血型)/home(籍贯)属性。
重写hashcode和equals,使用四个属性的组合进行实现。
创建HashSet集合,里面存放20个Criminal对象,其中O型血2人,A型血3人,B型血4人,AB型血1人,其余血型不详。
注意:hashcode()方法实现时,要求身高、体重、和血型三个属性合成一个数字,实现两两比较的高效算法。
package com.it18zhang.hash.demo;
import java.util.HashSet;
import java.util.Set;
public class Criminal {
private int height;
private int weight;
private int blood;
private String home;
public Criminal(int height, int weight, int blood) {
if (height <= 0) {
System.out.println("身高不能为负数 ");
System.exit(-1);
} else if (weight <= 0) {
System.out.println("体重不能为负数 ");
System.exit(-1);
} else if (blood > 4 || blood < 1) {
System.out.println("血型,必须为1-4之间的值 ");
System.exit(-1);
} else {
this.height = height;
this.weight = weight;
this.blood = blood;
}
}
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;
}
/**
* 重写hashcode方法
*/
public int hashCode() {
int i0 = 0 << 24;
int i1 = (ArrayTools.int2Bytes(height)[0]) & 0xff << 16;
int i2 = (ArrayTools.int2Bytes(weight)[0]) & 0xff << 8;
int i3 = (ArrayTools.int2Bytes(blood)[0]) & 0xff << 0;
int newResult = i0 | i1 | i2 | i3;
return home == null?newResult:newResult +home.hashCode();
}
public boolean equals(Object obj) {
//判断是否为空 ,是否有 同样的地址
if (obj == null || obj == this) {
return false;
}
//判断是否同一 类型
if (obj.getClass() == this.getClass()){
//1.是同一类型,判断身高,体重,血型
Criminal c = (Criminal)obj;
if(c.height == this.height&&c.weight == this.weight&&c.blood == this.blood){
return true;
}
}
return false;
}
}
public class HashTest {
public static void main(String[] args) {
Set<Criminal> cSet = new HashSet<Criminal>();
Criminal c1 = new Criminal(180, 170, 1);
Criminal c2 = new Criminal(170, 180, 1);
Criminal c3 = new Criminal(160, 180, 3);
Criminal c4 = new Criminal(150, 180, 4);
Criminal c5 = new Criminal(150, 180, 4);
cSet.add(c1);
cSet.add(c2);
cSet.add(c3);
cSet.add(c4);
System.out.println(c1.equals(c2));
System.out.println(c5.equals(c4));
for (Criminal c : cSet) {
out(c);
}
}
public static void out(Criminal c) {
String blood = "";
switch (c.getBlood()) {
case 1:
blood = "A";
break;
case 2:
blood = "B";
break;
case 3:
blood = "AB";
break;
case 4:
blood = "O";
break;
}
System.out.println("身高:" + c.getHeight() + "\t 体重: " + c.getWeight() + "\t" + "血型:" + blood);
}
}
public class ArrayTools {
/**
*
*整数转换为字节数组 ;向右移位,然后强转截断。
*/
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;
}
/**
*
* 字节数组 转 为整数 ,向左移位,由于向左移位,先转换为整数 ,缺的用符号位补齐,故用&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;
}
}
2.Map:
-----------
key(键) - value(值) :kv对.
创建HashMap,Person为key,Dog为value。
存放100元素,遍历map集合,两种方式。EntrySet + KeySet.
删除操作。remove();
package com.it18zhang.hash.demo;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
public class HashApp {
public static void main(String[] args) {
Map<Person,Dog> map = new HashMap<Person,Dog>();
for (int i = 1; i <100; i++) {
map.put(new Person("Tom" + i,i), new Dog("hei" + i,i));
}
System.out.println("-----------keySet-------------");
for (Person p:map.keySet()) {
System.out.println(map.get(p).getName());
}
System.out.println("-----------EntrySet-------------");
for (Entry<Person, Dog> entry : map.entrySet()){
System.out.println(entry.getKey().getName() +""+ entry.getValue().getName());
}
}
}
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 boolean equals(Object obj){
if (obj == null) {
return false;
}
if (this == obj){return true;}
//精准判断
boolean nameEqu;
if(obj.getClass() == this.getClass()){
Person p = (Person)obj;
//name
if (this.name == null)
{
if(p.name == null){
nameEqu = true;
}else {
nameEqu = false;
}
}else{
nameEqu = this.name.equals(p.name);
}
//age
boolean ageEqu = (this.age == p.age);
//sex
return nameEqu&&ageEqu;
}
return false;
}
}
public class Dog {
private String name;
private int age;
public Dog()
{
}
public Dog(String name,int age)
{
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;
}
}