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

What is a Map collection? Characteristics of Map collections

PHP中文网
Release: 2017-06-20 10:15:04
Original
8076 people have browsed it

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.调用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         //使用增强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         //使用增强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     }
Copy after login

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.调用Map集合中的方法entrySet,把Map集合中的每一个映射关系(结婚证)放入到Set集合中10          * 成员内部类的访问方式:外部类.内部类(Map.Entry)11          */12         Set<Map.Entry<String, String>> set = map.entrySet();13         //3.遍历Set集合,获取每一个映射关系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         //使用增强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         //使用增强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     }
Copy after login

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         //遍历可变参数-->遍历数组 6         for (int i : arr) { 7             sum +=i; 8         } 9         10         return sum;11     }
Copy after login

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.准备牌  4  *     2.洗牌  5  *     3.发牌  6  *     4.排序  7  *     5.看牌  8  */  9 public class DouDiZhu { 10     public static void main(String[] args) { 11         //1.准备牌 12         //创建存储序号和拍面值的Map集合 13         HashMap<Integer,String> poker = new HashMap<Integer, String>(); 14         //创建存储序号的List集合 15         ArrayList<Integer> pokerNumber = new ArrayList<Integer>(); 16         //创建序号的数组 17         String[] numbers = {"2","A","K","Q","J","10","9","8","7","6","5","4","3"}; 18         //创建花色数组 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         //使用循环嵌套遍历两个数组,组成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.发牌:发的也是牌的序号 48          *     a.定义4个集合存储3个玩家和1个底牌 49          *     b.遍历存储序号的List集合 50          *     c.使用list集合的索引%进行判断进行发牌 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集合的索引%进行判断进行发牌 64             if(i>=51){ 65                 //存储底牌 66                 diPai.add(number); 67             }else if(i%3==0){ 68                 //给玩家1发牌 69                 player01.add(number); 70             }else if(i%3==1){ 71                 //给玩家2发牌 72                 player02.add(number); 73             }else if(i%3==2){ 74                 //给玩家3发牌 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         //调用看牌方法 99         lookPoker("刘德华",player01, poker);100         lookPoker("周润发",player02, poker);101         lookPoker("周星驰",player03, poker);102         lookPoker("底牌",diPai, poker);103     }104     105     /*106      * 定义一个看牌的方法107      * 返回值类型:void108      * 方法名:lookPoker109      * 参数列表:玩家和底牌的集合,存储排的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 }
Copy after login

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!

Related labels:
source:php.cn
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template