• 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏吧

JDK8——Map操作

java 来源:午夜阳光psb 1次浏览

       本文涵盖的JDK8中引进的Map方法有:getOrDefault(Object,V),putIfAbsent(K,V),remove(Object,Object),replace(K,V),和 replace(K,V,V)。 

import java.util.HashMap;                                                                                              
import java.util.Map;                                                                                                  
                                                                                                                       
public class Jdk8Map {                                                                                                 
                                                                                                                       
    private final static Map<String, String> statesAndCapitals;                                                        
                                                                                                                       
    static {                                                                                                           
                                                                                                                       
        statesAndCapitals = new HashMap<>();                                                                           
                                                                                                                       
        statesAndCapitals.put("Alaska", "Anchorage");                                                                  
                                                                                                                       
        statesAndCapitals.put("California", "Sacramento");                                                             
                                                                                                                       
        statesAndCapitals.put("Colorado", "Denver");                                                                   
                                                                                                                       
        statesAndCapitals.put("Florida", "Tallahassee");                                                               
                                                                                                                       
        statesAndCapitals.put("Nevada", "Las Vegas");                                                                  
                                                                                                                       
        statesAndCapitals.put("New Mexico", "Sante Fe");                                                               
                                                                                                                       
        statesAndCapitals.put("Utah", "Salt Lake City");                                                               
                                                                                                                       
        statesAndCapitals.put("Wyoming", "Cheyenne");                                                                  
                                                                                                                       
    }                                                                                                                  
                                                                                                                       
    public static void main(String[] args) {                                                                           
                                                                                                                       
        /**                                                                                                            
         * getOrDefault(Object,V)                                                                                      
         * 允许调用者在代码语句中规定获得在map中符合提供的键的值,否则在没有找到提供的键的匹配项的时候返回一个“默认值”。                                                   
         * 它不仅仅在“键”没找到的时候返回提供的默认值,而且还会将键和默认值增加到调用的map中。                                                                
         */                                                                                                            
        String capitalNevada = statesAndCapitals.getOrDefault(                                                         
                "Nevada", "Unknown");                                                                                  
        System.out.println(capitalNevada);                                                                             
        String capitalWisconsin = statesAndCapitals.getOrDefault(                                                      
                "Wisconsin", "Unknown");                                                                               
        System.out.println(capitalWisconsin);                                                                          
                                                                                                                       
        /**                                                                                                            
         * putIfAbsent(v,v)                                                                                            
         * 当map中不存在指定的键时,把<key,value>添加到map中                                                                           
         */                                                                                                            
        String capitalWyoming =                                                                                        
                statesAndCapitals.putIfAbsent("Wyoming", "Albany");                                                    
        String capitalNewYork =                                                                                        
                statesAndCapitals.putIfAbsent("New York", "Albany");                                                   
        System.out.println(statesAndCapitals);                                                                         
                                                                                                                       
        /**                                                                                                            
         * remove(Object,Object)超越了长期有效的Map.remove(Object)                                                             
         * 方法,只有在提供的键和值都匹配的时候才会删除该map项                                                                                 
         */                                                                                                            
        boolean removedCalifornia =                                                                                    
                statesAndCapitals.remove("California", "Sacramento");                                                  
        boolean removedUtah = statesAndCapitals.remove("Utah", "Sacramento");                                          
        System.out.println(removedCalifornia);                                                                         
        System.out.println(removedUtah);                                                                               
                                                                                                                       
        /**                                                                                                            
         * replace(K,V)                                                                                                
         * 有在指定的键已经存在并且有与之相关的映射值时才会将指定的键映射到指定的值(新值)                                                                    
         */                                                                                                            
        String replacedAlaska = statesAndCapitals.replace("Alaska",                                                    
                                                          "Anchorage");                                                
        System.out.println(replacedAlaska);                                                                            
                                                                                                                       
        /**                                                                                                            
         * replace(K,V,V)                                                                                              
         * 当那个方法(上一个replace方法)只是涵盖指定的键在映射中有任意一个有效的值的替换处理,而这个“replace”方法                                                
         * 接受一个额外的(第三个)参数,只有在指定的键和值都匹配的情况下才会替换。                                                                        
         */                                                                                                            
        Boolean replacedNevada = statesAndCapitals.replace("Nevada", "Juneau",                                         
                                                           "USA");                                                     
        System.out.println(replacedNevada);                                                                            
        replacedNevada = statesAndCapitals.replace("Nevada", "Las Vegas",                                              
                                                   "USA");                                                             
        System.out.println(replacedNevada);                                                                            
                                                                                                                       
    }                                                                                                                  
}                                                                                                                      
                                                                                                                       

运行结果:

Las Vegas
Unknown
{New York=Albany, New Mexico=Sante Fe, Wyoming=Cheyenne, Colorado=Denver, Alaska=Anchorage, California=Sacramento, Florida=Tallahassee, Utah=Salt Lake City, Nevada=Las Vegas}
true
false
Anchorage
false
true

 


版权声明:本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系管理员进行删除。
喜欢 (0)