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 will explain in detail the example analysis of the Java Map collection. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
I. Preface
The map collection is a collection that we often use. It is necessary to understand and use the map collection.
II. Introduction to Map
Basic form: public interface Map
Map is an interface, we can not create objects directly, we can create objects in the form of polymorphism, there are two in Map
Parameter, one is K for key, the other is V for value, and a key has and corresponds to a value, Map cannot contain duplicate
Key, if a duplicate key is added, the last key will prevail, and other keys will be overwritten. The collection is all there.
Java.util package, so you need a guide package.
There are generally two kinds of concrete implementation, one is HashMap, the other is TreeMap
Import java.util.HashMap;import java.util.Map; public class MapTest {public static void main (String [] args) {Map map=new HashMap (); map.put ("2001", "Zhang San"); map.put ("2002", "Zhang San"); map.put ("2003", "Li Si"); map.put ("2003", "Wang Wu") / / if the key is repeated, it will overwrite the previous one, leaving the latest System.out.println (map); / / {2003 = Wang Wu, 2002 = Zhang San, 2001 = Zhang San}}
From the above, we can see that the key in map cannot be repeated, and the value can be repeated and can be obtained directly from the output object.
Collection, indicating that the toString method is overridden within this collection.
III. Basic functions of Map
These functions are common and can be mastered.
Map function demonstration: import java.util.HashMap; import java.util.Map; public class MapTest {public static void main (String [] args) {/ / create Map collection object Map map=new HashMap (); / / add elements map.put ("2001", "Zhang San"); map.put ("2002", "Li Si"); map.put ("2003", "Wang Wu") System.out.println (map); / / 2003 = Wang Wu, 2002 = Li Si, 2001 = Zhang San} / / Delete the element according to the key / / map.remove ("2001"); / / System.out.println (map); / / {2003 = Wang Wu, 2002 = Li Si} / / determine whether the set contains the specified key return boolean type System.out.println (map.containsKey ("2001")) / / true System.out.println (map.containsKey ("2004")); / / false / / determine whether the set contains the specified value and return boolean type System.out.println (map.containsValue ("Zhang San")); / / true System.out.println (map.containsValue ("Zhao Liu")); / / false / / determine whether the set is empty and return boolean type System.out.println (map.isEmpty ()) / / false / / get the length of the set System.out.println (map.size ()); / / 3 / / clear all key-value pairs map.clear (); System.out.println (map.isEmpty ()); / / true, empty}} IV. Map collection acquisition function
This method is mostly used when traversing collections, and the first three are more commonly used and easier to remember.
Map traversal demonstration: import java.util.HashMap;import java.util.Map;import java.util.Set; public class Maptest2 {public static void main (String [] args) {Map map = new HashMap (); map.put ("2001", "Zhang San"); map.put ("2002", "Li Si"); map.put ("2003", "Wang Wu") / / traverse the set / / Mode 1: / create the set of keys Set keySet=map.keySet () by finding the value of the key; / / traverse the collection of keys to get each key for (String key:keySet) {/ / String value=map.get (key) / / output key and value System.out.print (key+ "" + value+ ",");} System.out.println ("\ n -"); / / method 2: / / get the set of all key-value pairs Set entrySet = map.entrySet () / / traversing the key-value pair set for (Map.Entry me:entrySet) {/ / to get the key and value String key=me.getKey (); String value=me.getValue (); System.out.print (key+ "" + value+ ",");}
As shown in the figure:
This is the end of this article on "sample Analysis of Java Map Collection". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
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.