In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "Java's HashMap collection stores student objects and traverses the method", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "Java HashMap collection to store student objects and traversal method" bar!
First, requirements: create a HashMap collection, the key is the student number (String), the value is the student object (Student), store three key-value pairs of elements, and traverse
Analysis:
1. Define the student class
two。 Create a HashMap collection object
3. Create a student object
4 add students to the collection
5. Ergodic set
Public class StudentDemo {public static void main (String [] args) {/ / create Map collection object Map m=new HashMap (); / / add key value pairs m.put ("01", new Student ("Zhang San"); m.put ("04", new Student ("Zhao Liu")); m.put ("02", new Student ("Li Si")) M.put ("03", new Student ("Wang Wu")); / / traversing the collection Set s = m.entrySet (); / / traversing for (Map.Entry ss:s) {/ / get the value and key String key=ss.getKey () of the object according to the key value; Student value=ss.getValue () System.out.println (key+ "," + value.getName ());} System.out.println ("-"); / / method 2, find the value Set m1=m.keySet () through the key; for (String key: M1) {Student student = m.get (key) System.out.println (key+ "," + student.getName ());}
Second, requirements: create a HashMap collection, the key is the student object (Student), the value is the address (String), store three key-value pairs of elements, and traverse the analysis:
1. Define the student class
two。 Create a HashMap collection object
3. Create a student object and add the student object to the collection as a key
4 add an address string to the collection
5. In order to ensure the uniqueness of the data, it is necessary to rewrite the hashCode and equals methods in the student class.
6. Ergodic set
Public class StudentDemo {public static void main (String [] args) {/ / create collection object Map m=new HashMap (); / / add key value pairs m.put (new Student ("Zhang San", 18), "Shanghai"); m.put (new Student ("Li Si", 19), "Beijing"); m.put ("Wang Wu", 20), "Shanghai") M.put (new Student ("Wang Wu", 20), "Hainan"); / / Mode 1 / / get the set of all key-value pairs Set s=m.entrySet (); / / Mode 1, traverse for (Map.Entry mm:s) {/ / obtain the corresponding value and key Student key=mm.getKey () through key-value pair String value=mm.getValue (); System.out.println (key.getName () + "," + key.getAge () + value);} System.out.println ("- -"); / / Mode 2 Set key=m.keySet () For (Student s1:key) {String value=m.get (S1); System.out.println (s1.getName () + "," + s1.getAge () + "," + value) Thank you for your reading. The above is the content of "the method of storing student objects and traversing in Java's HashMap collection". After the study of this article, I believe you have a deeper understanding of the method of storing and traversing student objects in Java's HashMap collection, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.