1. Define the offender criminal class, height (height)/weight (body weight)/blood (blood type)/home (native) attribute.
Overrides Hashcode and Equals, implemented using a combination of four attributes.
Create HashSet collection, which contains 20 criminal objects, of which O-type blood 2 people, a type of blood 3 people, B-type blood 4 people, AB-type blood 1 people, the remaining blood type is unknown.
Note: When the Hashcode () method is implemented, it requires three attributes of height, weight, and blood type to synthesize a number to achieve a 22 comparison of efficient algorithms.
2.MAP:
-----------
Key (key)-Value (value): KV pair.
Create Hashmap,person to Key,dog as value.
Store 100 elements and traverse the map collection in two ways. EntrySet + KeySet.
The delete operation. Remove ();
1.
package it18zhang;public class arraytools {/* * Integer to byte array, right SHIFT, truncated or op * *///public static void main (String[] args) {// todo auto-generated method stub//}public static byte[] int2bytes ( Int i) {/* * integer converted to byte array */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;} /** * bytes converted to integers, shifted left */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 {/* * integer to byte array, right shift, truncated or operation * *///public static void main (String[] args) {// TODO Auto-generated method stub//}public static byte[] int2bytes (int i) {/* * integer conversion to byte array */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;} /** * bytes converted to integers, shifted left */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 ("Height cannot be negative"); System.exit (-1);} Else if (weight<=0) {System.out.println ("Weight cannot be negative"); System.exit (-1);;} Else if (blood == "A" && blood == "B" && blood == "AB" && blood == "O") {System.out.println ("Incorrect blood type"); System.exit (-1);} Else if (home==null) {System.out.println ("homeless"); 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;} /* * override Hasecode Method */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;}}
Big Data Day 11th