In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article is about how to use the two-column collection of the Map collection in Java. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Overview of Map Collection Map
Map is a container for storing elements based on key, which is much like a subscript, which is an integer in List. In Map, the key can make any type of object. There cannot be duplicate keys (Key) in Map, and each key (key) has a corresponding value (value). A key (key) and its corresponding value form an element in the map collection.
Characteristics of Map
Key-value correspondence
A key corresponds to a value.
Keys cannot be repeated, values can be repeated
Element access disorder
Functions of the Map collection
Basic function
Public class MapDemo_01 {public static void main (String [] args) {/ / create collection object Map map = new HashMap (); / / V put (K key,V value): add elements map.put ("Grey Big Wolf", "Red Big Wolf"); map.put ("Pleasant Goat", "Beautiful Sheep"); map.put ("flat mouth Lun", "warm Sheep") / / V remove (Object key): delete the key value pair element / / System.out.println (map.remove ("Pleasant Goat") according to the key; / / if there is no corresponding key, return null// System.out.println (map.remove ("boiling Sheep")); / / void clear (): remove all key value pair elements / / map.clear () / / boolean containsKey (Object key): determines whether the collection contains the specified key / / System.out.println (map.containsKey ("Pleasant Goat"); / / contains the return true, otherwise false// System.out.println (map.containsKey ("boiling Sheep")); / / boolean isEmpty (): determines whether the collection is empty / / System.out.println (map.isEmpty ()). / / int size (): the length of the set, that is, the number of key-value pairs in the set System.out.println (map.size ()); System.out.println (map);}}
Get function
Public class MapDemo_02 {public static void main (String [] args) {/ / create collection object Map map = new HashMap (); / / add elements map.put ("Grey Wolf", "Red Wolf"); map.put ("Pleasant Goat", "Beautiful Sheep"); map.put ("flat mouth Lun", "warm Sheep") / / V get (Object key): get the value / / System.out.println (map.get ("Grey Wolf") by key; / / Set keySet (): get the collection of all keys / / Set keySet = map.keySet (); / / for (String key: keySet) {/ / System.out.println (key) / /} / / Collection values (): get the collection of all values Collection values = map.values (); for (String value: values) {System.out.println (value);}} traversal of the Map collection
Method 1:
1) get a collection of all keys. Using keySet () method to realize
2) iterate through the collection of keys to get each key. Implement with enhanced for
3) find the value according to the key. Using get (Object key) method to realize
Public class MapDemo_03 {public static void main (String [] args) {/ / create collection object Map map = new HashMap (); / / add elements map.put ("Grey Wolf", "Red Wolf"); map.put ("Pleasant Goat", "Beautiful Sheep"); map.put ("flat mouth Lun", "warm Sheep") / / gets a collection of all keys. Use the keySet () method to implement Set keySet = map.keySet (); / / iterate through the collection of keys to get each key. Use enhanced for to implement for (String key: keySet) {/ / find the value according to the key. Use get (Object key) method to realize String value = map.get (key); System.out.println (key + "," + value);}
Method 2:
1) get the collection of all key-value pairs: Set entrySet (): get the collection of all key-value pairs of objects
2) iterate through the collection of key-value pair objects to get each key-value pair object: implement it with enhanced for to get each Map.Entry
3) get the key and value according to the key value of the object: get the key with getKey () and get the value with getValue ()
Public class MapDemo02 {public static void main (String [] args) {/ / create collection object Map map = new HashMap (); / / add elements map.put ("Grey Wolf", "Red Wolf"); map.put ("Pleasant Goat", "Beautiful Sheep"); map.put ("flat mouth Lun", "warm Sheep") / / get the set of all key-value pair objects Set entrySet = map.entrySet (); / / iterate through the collection of key-value pair objects to get each key-value pair object for (Map.Entry me: entrySet) {/ / get the key and value String key = me.getKey () according to the key-value pair object; String value = me.getValue () Each subclass of the System.out.println (key + "," + value);} Map collection
1) Hashtable:
The underlying is the hash table data structure, the thread is synchronous, can not be stored in the null key, null value.
It is inefficient and is replaced by HashMap.
2) HashMap: (data out of order, unique)
The underlying is the hash table data structure, threads are not synchronized, can be stored in null keys, null values.
To ensure the uniqueness of the key, you need to override the hashCode method and the equals method.
3) LinkedHashMap: (data is orderly and unique)
This subclass is based on the hash table and incorporates the linked list. You can add or delete Map collections to improve efficiency.
4) TreeMap: (data is orderly and unique)
The bottom layer is the binary tree data structure. You can sort the keys in the map collection. You need to use Comparable or Comparator for comparative sorting. Return 0, to determine the uniqueness of the key.
Set frame diagram
When checking and accepting the checkpoint today, the teacher gave some supplementary knowledge and related questions for this part of the collection. I briefly summarized it.
[1] the difference among ArrayList, LinkedList and Vector:
Vector: array, thread safe
ArrayList:List subclass, data access orderly, repeatable; the underlying is the implementation of array structure, indexing, query fast, slow to add and delete; thread is not safe
LinkedList:List subclass, data access orderly, repeatable; underlying is the implementation of linked list structure, pointer, slow query, fast additions and deletions; thread is not safe
[2] the difference between HashMap and HashTable:
1) the underlying data structure is different: HashTable only has array + linked list (proposed by JDK1); HashMap has array + linked list before JDK1.8, and array + linked list / red-black tree after 1.8,
2) the initialization capacity is different: the initial capacity of HashMap is 16, the initial capacity of Hashtable is 11, and the load factor of both is 0.75 by default.
3) the expansion mechanism is different: when used capacity > total capacity * load factor, the HashMap expansion rule doubles the current capacity, and the Hashtable expansion rule doubles the current capacity + 1.
4) the key or value of Hashtable is not allowed to be null, while the key value of HashMap can be null
[3] the difference between HashSet and TreeSet:
HashSet:
1) the order of elements cannot be guaranteed, and the order may change.
2) the collection element can be a null, but only one null can be put in it.
3) the underlying layer of HashSet is implemented with HashMap.
4) the underlying layer of HashSet is implemented by hash table.
TreeSet:
1) the data in Treeset is sorted and null values are not allowed.
2) TreeSet is implemented through TreeMap, but Set only uses Map's key
3) the underlying implementation of TreeSet is the data structure of binary tree (red-black tree).
[4] the difference between Collection and Collections:
Collection:
A collection interface that provides a general interface method for performing basic operations on collection objects. The main classes that implement the interface are List and Set. The design goal of the interface is to provide a maximum unified mode of operation for a variety of specific sets.
Collections:
For a package class of the collection class, it provides a series of static methods to search, sort, and thread-safe various collections, most of which are used to deal with linear tables. The Collections class cannot be instantiated as a utility class that serves the Collection framework. If the corresponding Collection object null is used when using the methods of the Collections class, those methods will throw a NullPointerException
Thank you for reading! This is the end of the article on "how to use the double-column set of Map sets in Java". 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, you can 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.