国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Home Java javaTutorial What is a Map collection? Characteristics of Map collections

What is a Map collection? Characteristics of Map collections

Jun 20, 2017 am 10:15 AM
collections parameter variable gather

Characteristics of Map collection:
1. It is a double-column collection. When assigning values, key and value must be assigned at the same time.
2. It is an unordered collection (storing and removing elements) The order may be inconsistent)
3. The key value cannot be repeated, but the value can be repeated
4. One key can only correspond to one vlaue
5. When defining a collection, the data type key and value can use the same data type. You can also use different data types

Characteristics of Map collection
java.util.MapInterface: Collection, which is a two-column collection

The first way to traverse a Map collection
The first way to traverse a Map collection: to find a value through a key
There is a method in the Map collection: keySet
Set keySet() Returns a Set view of the keys contained in this map. Store the keys in the Map collection into a Set collection
Steps to traverse the Map collection:
1. Define a Map collection and add elements to the collection
2. Call the Map collection The method keySet in the Map collection stores the keys in the Map collection into a Set collection
3. Traverse the Set collection and obtain all the keys in the Map collection
4. Use the get method of the Map collection to search through the obtained keys Value

?1?public?static?void?main(String[]?args)?{?2?????????//1.定義一個Map集合,往集合中添加元素?3?????????Map<String,String>?map?=?new?HashMap<String,String>();?4?????????map.put("a",?"1");?5?????????map.put("b",?"2");?6?????????map.put("c",?"3");?7?????????map.put("d",?"4");?8?????????//2.調(diào)用Map集合中的方法keySet,把Map集合中的健存儲到一個Set集合中?9?????????Set<String>?set?=?map.keySet();10?????????//System.out.println(set.getClass());11?????????//3.遍歷Set集合,獲取Map集合所有的健12?????????//使用迭代器遍歷13?????????Iterator<String>?it?=?set.iterator();14?????????while(it.hasNext()){15?????????????String?key?=?it.next();16?????????????//4.通過獲取到的健,使用Map集合的方法get查找值17?????????????String?value?=?map.get(key);18?????????????System.out.println(key+"..."+value);19?????????}20?????????System.out.println("----------------");21?????????//使用增強(qiáng)for遍歷22?????????for(String?key?:?set){23?????????????//4.通過獲取到的健,使用Map集合的方法get查找值24?????????????String?value?=?map.get(key);25?????????????System.out.println(key+"..."+value);26?????????}27?????????System.out.println("----------------");28?????????//使用增強(qiáng)for遍歷29?????????for(String?key?:?map.keySet()){30?????????????//4.通過獲取到的健,使用Map集合的方法get查找值31?????????????String?value?=?map.get(key);32?????????????System.out.println(key+"..."+value);33?????????}34?????}

The second way of Map collection traversal
The second way of Map collection traversal: the way of traversing key-value pairs
Map There is a method in the collection: entrySet
Set> entrySet() returns a Set view of the mapping relationships contained in this map.
Traversal steps:
1. Define a Map collection and add elements to the collection
2. Call the entrySet method in the Map collection to add each mapping relationship in the Map collection ( Marriage certificate) into the Set collection
3. Traverse the Set collection and obtain each mapping relationship Entry
4. Use the methods getKey and getValue in Entry to obtain the key sum Value

?1?public?static?void?main(String[]?args)?{?2?????????//1.定義一個Map集合,往集合中添加元素?3?????????Map<String,String>?map?=?new?HashMap<String,String>();?4?????????map.put("a",?"1");?5?????????map.put("b",?"2");?6?????????map.put("c",?"3");?7?????????map.put("d",?"4");?8?????????/*?9??????????*?2.調(diào)用Map集合中的方法entrySet,把Map集合中的每一個映射關(guān)系(結(jié)婚證)放入到Set集合中10??????????*?成員內(nèi)部類的訪問方式:外部類.內(nèi)部類(Map.Entry)11??????????*/12?????????Set<Map.Entry<String, String>>?set?=?map.entrySet();13?????????//3.遍歷Set集合,獲取每一個映射關(guān)系Entry<K,V>14?????????//使用迭代器遍歷Set集合15?????????Iterator<Map.Entry<String, String>>?it?=?set.iterator();16?????????while(it.hasNext()){17?????????????Map.Entry<String, String>?entry?=?it.next();18?????????????//4.使用Entry<K,V>中的方法getKey和getValue獲取健和值19?????????????String?key?=?entry.getKey();20?????????????String?value?=?entry.getValue();21?????????????System.out.println(key+"..."+value);22?????????}23?????????System.out.println("---------------------");24?????????//使用增強(qiáng)for遍歷Set集合25?????????for(Map.Entry<String, String>?entry:set){26?????????????//4.使用Entry<K,V>中的方法getKey和getValue獲取健和值27?????????????String?key?=?entry.getKey();28?????????????String?value?=?entry.getValue();29?????????????System.out.println(key+"..."+value);30?????????}31?????????System.out.println("---------------------");32?????????//使用增強(qiáng)for遍歷Set集合33?????????for(Map.Entry<String, String>?entry:map.entrySet()){34?????????????//4.使用Entry<K,V>中的方法getKey和getValue獲取健和值35?????????????String?key?=?entry.getKey();36?????????????String?value?=?entry.getValue();37?????????????System.out.println(key+"..."+value);38?????????}39?????}

HashMap stores custom type key value
HashMap stores custom type key value
Custom type as the value of Map collection
Custom type as Map Keys of collections

Remember: when custom types override hashCode and equals
1. Use HashSet to store custom types
2. Use HashMap collections, and use custom types for keys

Hashtable
Map implementation class Hashtable
The underlying data structure is a hash table, the characteristics are the same as hashMap
Hashtable is a thread-safe collection and runs slowly
HashMap is a thread-unsafe collection and runs fast

The fate of Hashtable is the same as Vector. Starting from JDK1.2, it was replaced by the more advanced HashMap

HashMap allows the storage of null values. null key
hashtable is not allowed to store null values, null key

Hashtable His children, subclass Properties are still active in the development stage

LinkedHashMap collection features
java.util.LinkedHashMap extends HashMap implements Map
LinkedHashMap collection features:
1. Hash table + linked list: doubly linked list, which can guarantee the iteration order
2.Key cannot be repeated


Collections
java.util.Collections: Tool class for operating Collection collections
The methods in the tool class are all static methods and can be used directly through the class name

public static void sort(List list) // Sorting of collection elements
public static void shuffle(List list) // The storage location of collection elements is shuffled

Variable Parameter
New features that appeared after JDK1.5
Prerequisite for use: The data type of the method parameter is determined, but the number of parameters is uncertain

Using format:
Modifier return value type method name (data type...variable name){
}
...represents that the method can receive multiple parameters of the same data type
The bottom layer of variable parameters can be regarded as It is an array

Notes on variable parameters:
1. A method parameter can only use one variable parameter
2. If the method has multiple parameters , variable parameters must be written at the end of the parameter list

?1?public?static?int?add(int...arr){?2?????????System.out.println(arr);//[I@104c575?3?????????System.out.println(arr.length);?4?????????int?sum?=?0;?5?????????//遍歷可變參數(shù)-->遍歷數(shù)組?6?????????for?(int?i?:?arr)?{?7?????????????sum?+=i;?8?????????}?9?????????10?????????return?sum;11?????}

static import
JDK1.5 new feature, static import
Reduce development time Code amount
Standard writing method, can only be used when importing a package
import static java.lang.System.out; At the end, it must be a static member

package cn.itcast.demo05;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;

??1?/*??2??*?帶排序功能的斗地主案例:??3??*?????1.準(zhǔn)備牌??4??*?????2.洗牌??5??*?????3.發(fā)牌??6??*?????4.排序??7??*?????5.看牌??8??*/??9?public?class?DouDiZhu?{?10?????public?static?void?main(String[]?args)?{?11?????????//1.準(zhǔn)備牌?12?????????//創(chuàng)建存儲序號和拍面值的Map集合?13?????????HashMap<Integer,String>?poker?=?new?HashMap<Integer, String>();?14?????????//創(chuàng)建存儲序號的List集合?15?????????ArrayList<Integer>?pokerNumber?=?new?ArrayList<Integer>();?16?????????//創(chuàng)建序號的數(shù)組?17?????????String[]?numbers?=?{"2","A","K","Q","J","10","9","8","7","6","5","4","3"};?18?????????//創(chuàng)建花色數(shù)組?19?????????String[]?colors?=?{"?","?","?","?"};?20?????????//先把大王和小王存儲到集合中?21?????????int?index?=?0;?22?????????poker.put(index,?"大王");?23?????????pokerNumber.add(index);?24?????????index++;?25?????????poker.put(index,?"小王");?26?????????pokerNumber.add(index);?27?????????index++;?28??????????29?????????//使用循環(huán)嵌套遍歷兩個數(shù)組,組成52張牌?30?????????for?(String?number?:?numbers)?{?31?????????????for?(String?color?:?colors)?{?32?????????????????//把組合包的牌添加到集合中?33?????????????????poker.put(index,?color+number);?34?????????????????pokerNumber.add(index);?35?????????????????index++;?36?????????????}?37?????????}?38?????????//System.out.println(poker);?39?????????//System.out.println(pokerNumber);?40??????????41?????????//2.洗牌:洗的是牌的序號?42?????????//使用Collections中的方法shuffle?43?????????Collections.shuffle(pokerNumber);?44?????????//System.out.println(pokerNumber);?45??????????46?????????/*?47??????????*?3.發(fā)牌:發(fā)的也是牌的序號?48??????????*?????a.定義4個集合存儲3個玩家和1個底牌?49??????????*?????b.遍歷存儲序號的List集合?50??????????*?????c.使用list集合的索引%進(jìn)行判斷進(jìn)行發(fā)牌?51??????????*?????注意:先判斷底牌?52??????????*/?53?????????//a.定義4個集合存儲3個玩家和1個底牌?54?????????ArrayList<Integer>?player01?=?new?ArrayList<Integer>();?55?????????ArrayList<Integer>?player02?=?new?ArrayList<Integer>();?56?????????ArrayList<Integer>?player03?=?new?ArrayList<Integer>();?57?????????ArrayList<Integer>?diPai?=?new?ArrayList<Integer>();?58??????????59?????????//b.遍歷存儲序號的List集合?60?????????for(int?i=0;?i<pokerNumber.size(); i++){ 61             //定義變量,接收排的序號 62             int number = pokerNumber.get(i); 63             //c.使用list集合的索引%進(jìn)行判斷進(jìn)行發(fā)牌 64             if(i>=51){?65?????????????????//存儲底牌?66?????????????????diPai.add(number);?67?????????????}else?if(i%3==0){?68?????????????????//給玩家1發(fā)牌?69?????????????????player01.add(number);?70?????????????}else?if(i%3==1){?71?????????????????//給玩家2發(fā)牌?72?????????????????player02.add(number);?73?????????????}else?if(i%3==2){?74?????????????????//給玩家3發(fā)牌?75?????????????????player03.add(number);?76?????????????}?77?????????}?78?????????/*System.out.println(player01);?79?????????System.out.println(player02);?80?????????System.out.println(player03);?81?????????System.out.println(diPai);*/?82??????????83?????????//4.排序?84?????????//使用Collections中的方法sort?85?????????Collections.sort(player01);?86?????????Collections.sort(player02);?87?????????Collections.sort(player03);?88?????????Collections.sort(diPai);?89??????????90?????????/*System.out.println(player01);?91?????????System.out.println(player02);?92?????????System.out.println(player03);?93?????????System.out.println(diPai);*/?94??????????95?????????/*?96??????????*?5.看牌?97??????????*/?98?????????//調(diào)用看牌方法?99?????????lookPoker("劉德華",player01,?poker);100?????????lookPoker("周潤發(fā)",player02,?poker);101?????????lookPoker("周星馳",player03,?poker);102?????????lookPoker("底牌",diPai,?poker);103?????}104?????105?????/*106??????*?定義一個看牌的方法107??????*?返回值類型:void108??????*?方法名:lookPoker109??????*?參數(shù)列表:玩家和底牌的集合,存儲排的Map集合110??????*?使用查表法看牌:111??????*?????遍歷List集合,獲取Map集合key,使用key去Map集合中查找value112??????*/113?????public?static?void?lookPoker(String?name,ArrayList<Integer>?list,HashMap<Integer,String>?map){114?????????System.out.print(name+":");115?????????//遍歷List集合,獲取Map集合key116?????????for?(Integer?key?:?list)?{117?????????????//使用key去Map集合中查找value118?????????????String?value?=?map.get(key);119?????????????System.out.print(value+"?");120?????????}121?????????System.out.println();//換行122?????}123?}

The above is the detailed content of What is a Map collection? Characteristics of Map collections. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

i9-12900H parameter evaluation list i9-12900H parameter evaluation list Feb 23, 2024 am 09:25 AM

i9-12900H is a 14-core processor. The architecture and technology used are all new, and the threads are also very high. The overall work is excellent, and some parameters have been improved. It is particularly comprehensive and can bring users Excellent experience. i9-12900H parameter evaluation review: 1. i9-12900H is a 14-core processor, which adopts the q1 architecture and 24576kb process technology, and has been upgraded to 20 threads. 2. The maximum CPU frequency is 1.80! 5.00ghz, which mainly depends on the workload. 3. Compared with the price, it is very suitable. The price-performance ratio is very good, and it is very suitable for some partners who need normal use. i9-12900H parameter evaluation and performance running scores

vivox200ultra parameters and price details vivox200ultra parameters and price details Jun 28, 2024 pm 01:23 PM

The latest official news of vivox200ultra has exposed the parameters and price details of vivox200ultra. It is reported that vivox200ultra will be equipped with a 10x periscope super telephoto lens, and the price starts at about 6999 yuan. It can be seen that it occupies an absolute advantage in photography performance. The following are the parameters and prices of vivox200ultra Come and see the details. 1. Parameter configuration details of vivox200ultra 1. Vivox200ultra rendering From the vivo X200 Ultra rendering, the front of the phone adopts a borderless full-screen design, and the visual effect of the entire front of the phone can be said to be very invincible. 2. vivox200ultra has Blackhawk frame

Why is it difficult to implement collection-like functions in Go language? Why is it difficult to implement collection-like functions in Go language? Mar 24, 2024 am 11:57 AM

It is difficult to implement collection-like functions in the Go language, which is a problem that troubles many developers. Compared with other programming languages ??such as Python or Java, the Go language does not have built-in collection types, such as set, map, etc., which brings some challenges to developers when implementing collection functions. First, let's take a look at why it is difficult to implement collection-like functionality directly in the Go language. In the Go language, the most commonly used data structures are slice and map. They can complete collection-like functions, but

C++ function parameter type safety check C++ function parameter type safety check Apr 19, 2024 pm 12:00 PM

C++ parameter type safety checking ensures that functions only accept values ??of expected types through compile-time checks, run-time checks, and static assertions, preventing unexpected behavior and program crashes: Compile-time type checking: The compiler checks type compatibility. Runtime type checking: Use dynamic_cast to check type compatibility, and throw an exception if there is no match. Static assertion: Assert type conditions at compile time.

C++ program to find the value of the inverse hyperbolic sine function taking a given value as argument C++ program to find the value of the inverse hyperbolic sine function taking a given value as argument Sep 17, 2023 am 10:49 AM

Hyperbolic functions are defined using hyperbolas instead of circles and are equivalent to ordinary trigonometric functions. It returns the ratio parameter in the hyperbolic sine function from the supplied angle in radians. But do the opposite, or in other words. If we want to calculate an angle from a hyperbolic sine, we need an inverse hyperbolic trigonometric operation like the hyperbolic inverse sine operation. This course will demonstrate how to use the hyperbolic inverse sine (asinh) function in C++ to calculate angles using the hyperbolic sine value in radians. The hyperbolic arcsine operation follows the following formula -$$\mathrm{sinh^{-1}x\:=\:In(x\:+\:\sqrt{x^2\:+\:1})}, Where\:In\:is\:natural logarithm\:(log_e\:k)

The open source model wins GPT-4 for the first time! Arena's latest battle report has sparked heated debate, Karpathy: This is the only list I trust The open source model wins GPT-4 for the first time! Arena's latest battle report has sparked heated debate, Karpathy: This is the only list I trust Apr 10, 2024 pm 03:16 PM

An open source model that can beat GPT-4 has appeared! The latest battle report of the large model arena: the 104 billion parameter open source model CommandR+ climbed to 6th place, tying with GPT-4-0314 and surpassing GPT-4-0613. Image This is also the first open-weight model to beat GPT-4 in the large model arena. The large model arena is one of the only test benchmarks that the master Karpathy trusts. Image CommandR+ from AI unicorn Cohere. The co-founder and CEO of this large model startup is none other than Aidan Gomez, the youngest author of Transformer (referred to as the wheat harvester). As soon as this battle report came out, another wave of big model clubs started

Common concurrent collections and thread safety issues in C# Common concurrent collections and thread safety issues in C# Oct 09, 2023 pm 10:49 PM

Common concurrent collections and thread safety issues in C# In C# programming, handling concurrent operations is a very common requirement. Thread safety issues arise when multiple threads access and modify the same data at the same time. In order to solve this problem, C# provides some concurrent collection and thread safety mechanisms. This article will introduce common concurrent collections in C# and how to deal with thread safety issues, and give specific code examples. Concurrent collection 1.1ConcurrentDictionaryConcurrentDictio

How to adjust the rounded corners of win10 search box How to adjust the rounded corners of win10 search box Jan 15, 2024 pm 03:12 PM

There has been news about the rounded corners of the win10 search box for a long time, but it has never been implemented. We can generally use the registry to experience the rounded corners of the win10 search box. So let's take a look at the tutorial on the rounded corners of the win10 search box. Bar. Win10 search box variable rounded corners: 1. Open the search box, enter regedit, and enter the registry. 2. Find this path in Computer\HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Search. 3. In the blank space, select New - DWORD (32-bit) value - Name the new key ImmersiveSearch - Number

See all articles