The simple operation method of the subclass HashMap of the Black Horse Programmer--java--map Collection

Source: Internet
Author: User



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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.