A simple operation method of subclass HashMap of map Collection
Import Java.util.hashmap;import Java.util.iterator;public class Hashmapdemos {public static void%r0main (string[] args) {//Hashmap<studenti,string> HM = new hashmap<studenti,string> (); Hm.put (New Studenti ("KK", 22), "Shanxi"); Hm.put (New Studenti ("JJ", 20), "Shandong"), Hm.put (New Studenti ("HH", 25), "Shanghai");iterator<studenti> it = Hm.keyset (). Iterator (); while (It.hasnext ()) {Studenti key = It.next (); String value = Hm.get (key); System.out.println (key.tostring () + " --" +value);}}}
Class studenti{private String name;private int age; Studenti (String name, int age) {this.name=name;this.age = age;} public int getage () {return age;} Public String GetName () {return name;} Public String toString () {return ' The Student Name: ' +name + ' and ' age ' : ' + age;} public int hashcode () {return name.hashcode () +age*10;} public boolean equals (object obj) {if (this = = obj) return true;//The same object is placed two times, directly returning True if (! ( obj instanceof Studenti)) throw new ClassCastException ("type error"); Studenti p = (studenti) obj; return this. Name.equals (p.name) && this.age = = P.age;}}
To run the program:
The simple operation method of the subclass HashMap of the Black Horse Programmer--java--map Collection