Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to interpret the usage of map list set in the three collections of Java

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

How to interpret the use of map list set in the three collections of Java, many novices are not very clear about this. In order to help you solve this problem, the following editor will explain it in detail. People with this need can come and learn. I hope you can get something.

The Map interface and the Collection interface are the parent interfaces of all collection frameworks:

The subinterfaces of Collection interface include Set interface and List interface.

The main implementation classes of Map interface are: HashMap, TreeMap, Hashtable, ConcurrentHashMap and Properties, etc.

The main implementation classes of Set interface are: HashSet, TreeSet, LinkedHashSet, etc.

The main implementation classes of List interface are: ArrayList, LinkedList, Stack and Vector, etc.

What is the difference among List,Set,Map? Do List, Set, and Map inherit from the Collection interface? What are the characteristics of List, Map and Set when accessing elements?

Collection collection mainly has two interfaces: List and Set.

(1) List: is an order in which elements can be repeated. The common implementation classes are ArrayList, LinkedList, and Vector.

(2) Set: it is unordered and cannot store repeating elements. Only one null element can be stored. The common implementation classes of Set API are HashSet, LinkedHashSet and TreeSet.

Map

Map is a collection of key-value pairs that stores the mapping between keys, values, and. Key is unordered and unique; value does not require ordering and repetition is allowed. Map does not inherit from the Collection interface, and when retrieving elements from the Map collection, the corresponding value object is returned as long as the key object is given.

Common implementation classes of Map: HashMap, TreeMap, HashTable, LinkedHashMap, ConcurrentHashMap

(1) Map store elements and fetch elements and delete elements (put, get, remove)

Import java.util.HashMap;/** * Map higher Mathematics of storing and fetching elements and deleting (put, get, remove) * @ author Xiaozhi * / public class Map1 {public static void main (String [] args) {/ / create HashMap object Sites HashMap sites = new HashMap (); / / add key-value pairs sites.put ("1", "Google") Sites.put ("2", "Runoob"); sites.put ("3", "Taobao"); sites.put ("4", "Zhihu"); / / output all elements in Map System.out.println (sites); / / take out the value System.out.println (sites.get ("1") of value of 1 of key in Map) System.out.println (sites.get ("3")); sites.remove ("3"); System.out.println (sites.get ("3")); System.out.println (sites);}}

(2) cyclic Map

Package basics.map; import java.util.HashMap;import java.util.Map.Entry;public class Map2 {public static void main (String [] args) {/ / create HashMap object Sites HashMap sites = new HashMap (); / / add key value pair sites.put (4, "Zhihu"); sites.put (1, "Google"); sites.put (2, "Runoob") Sites.put (3, "Taobao"); sites.put (4, "Zhihu2"); / / Loop only key for (Integer I: sites.keySet ()) {System.out.println ("key:" + I + "value:" + sites.get (I)) } / / only loop value value for (String value: sites.values ()) {/ / output each value System.out.println (value + ",");} / / Loop key and value for (Entry entry: sites.entrySet ()) {System.out.println ("output only key" + entry.getKey ()) System.out.println ("output only value" + entry.getValue ());} for (int I = 0; I

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report