成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

有關于Java Map,應該掌握的8個問題

開發 后端
最近幾天看了幾篇有關于Java Map的外國博文,寫得非常不錯,所以整理了Java map 應該掌握的8個問題,都是日常開發司空見慣的問題,希望對大家有幫助;如果有不正確的地方,歡迎提出,萬分感謝哈~

 前言

[[313878]]

最近幾天看了幾篇有關于Java Map的外國博文,寫得非常不錯,所以整理了Java map 應該掌握的8個問題,都是日常開發司空見慣的問題,希望對大家有幫助;如果有不正確的地方,歡迎提出,萬分感謝哈~

本章節所有代碼demo已上傳github

1、如何把一個Map轉化為List

日常開發中,我們經常遇到這種場景,把一個Map轉化為List。map轉List有以下三種轉化方式:

  • 把map的鍵key轉化為list
  • 把map的值value轉化為list
  • 把map的鍵值key-value轉化為list

偽代碼如下:

  1.   // key list 
  2.   List keyList = new ArrayList(map.keySet()); 
  3.   // value list 
  4.   List valueList = new ArrayList(map.values()); 
  5.   // key-value list 
  6.   List entryList = new ArrayList(map.entrySet()); 

示例代碼:

  1.   public class Test { 
  2.       public static void main(String[] args) { 
  3.           Map<Integer, String> map = new HashMap<>(); 
  4.           map.put(2, "jay"); 
  5.           map.put(1, "whx"); 
  6.           map.put(3, "huaxiao"); 
  7.           //把一個map的鍵轉化為list 
  8.           List<Integer> keyList = new ArrayList<>(map.keySet()); 
  9.           System.out.println(keyList); 
  10.          //把map的值轉化為list 
  11.          List<String> valueList = new ArrayList<>(map.values()); 
  12.          System.out.println(valueList); 
  13.          把map的鍵值轉化為list 
  14.          List entryList = new ArrayList(map.entrySet()); 
  15.          System.out.println(entryList); 
  16.   
  17.      } 
  18.  } 

運行結果:

  1.   [1, 2, 3] 
  2.   [whx, jay, huaxiao] 
  3.   [1=whx, 2=jay, 3=huaxiao] 

2、如何遍歷一個Map

我們經常需要遍歷一個map,可以有以下兩種方式實現:

通過entrySet+for實現遍歷

  1.   for(Entry entry: map.entrySet()) { 
  2.     // get key 
  3.     K key = entry.getKey(); 
  4.     // get value 
  5.     V value = entry.getValue(); 
  6.   } 

實例代碼:

  1.   public class EntryMapTest { 
  2.       public static void main(String[] args) { 
  3.           Map<Integer, String> map = new HashMap<>(); 
  4.           map.put(2, "jay"); 
  5.           map.put(1, "whx"); 
  6.           map.put(3, "huaxiao"); 
  7.    
  8.           for(Map.Entry entry: map.entrySet()) { 
  9.               // get key 
  10.              Integer key = (Integer) entry.getKey(); 
  11.              // get value 
  12.              String value = (String) entry.getValue(); 
  13.   
  14.              System.out.println("key:"+key+",value:"+value); 
  15.          } 
  16.      } 
  17.  } 

通過Iterator+while實現遍歷

  1.   Iterator itr = map.entrySet().iterator(); 
  2.   while(itr.hasNext()) { 
  3.     Entry entry = itr.next(); 
  4.     // get key 
  5.     K key = entry.getKey(); 
  6.     // get value 
  7.     V value = entry.getValue(); 
  8.   } 

實例代碼:

  1.   public class IteratorMapTest { 
  2.       public static void main(String[] args) { 
  3.           Map<Integer, String> map = new HashMap<>(); 
  4.           map.put(2, "jay"); 
  5.           map.put(1, "whx"); 
  6.           map.put(3, "huaxiao"); 
  7.    
  8.           Iterator itr = map.entrySet().iterator(); 
  9.           while(itr.hasNext()) { 
  10.              Map.Entry entry = (Map.Entry) itr.next(); 
  11.              // get key 
  12.              Integer key = (Integer) entry.getKey(); 
  13.              // get value 
  14.              String value = (String) entry.getValue(); 
  15.   
  16.              System.out.println("key:"+key+",value:"+value); 
  17.          } 
  18.      } 
  19.  } 

運行結果:

  1.   key:1,value:whx 
  2.   key:2,value:jay 
  3.   key:3,value:huaxiao 

3、如何根據Map的keys進行排序

對Map的keys進行排序,在日常開發很常見,主要有以下兩種方式實現。

把Map.Entry放進list,再用Comparator對list進行排序

  1.   List list = new ArrayList(map.entrySet()); 
  2.   Collections.sort(list, (Entry e1, Entry e2)-> { 
  3.       return e1.getKey().compareTo(e2.getKey()); 
  4.   }); 

實例代碼:

  1.   public class SortKeysMapTest { 
  2.       public static void main(String[] args) { 
  3.           Map<String, String> map = new HashMap<>(); 
  4.           map.put("2010""jay"); 
  5.           map.put("1999""whx"); 
  6.           map.put("3010""huaxiao"); 
  7.    
  8.           List<Map.Entry<String,String>> list = new ArrayList<>(map.entrySet()); 
  9.           Collections.sort(list, (Map.Entry e1, Map.Entry e2)-> { 
  10.                  return e1.getKey().toString().compareTo(e2.getKey().toString()); 
  11.          }); 
  12.   
  13.          for (Map.Entry entry : list) { 
  14.              System.out.println("key:" + entry.getKey() + ",value:" + entry.getValue()); 
  15.          } 
  16.   
  17.      } 
  18.  } 

使用SortedMap+TreeMap+Comparator實現

  1. 1.  SortedMap sortedMap = new TreeMap(new Comparator() { 
  2. 2.    @Override 
  3. 3.    public int compare(K k1, K k2) { 
  4. 4.      return k1.compareTo(k2); 
  5. 5.    } 
  6. 6.  }); 
  7. 7.  sortedMap.putAll(map); 

實例代碼:

  1.   public class SortKeys2MapTest { 
  2.       public static void main(String[] args) { 
  3.           Map<String, String> map = new HashMap<>(); 
  4.           map.put("2010""jay"); 
  5.           map.put("1999""whx"); 
  6.           map.put("3010""huaxiao"); 
  7.    
  8.           SortedMap sortedMap = new TreeMap(new Comparator<String>() { 
  9.               @Override 
  10.              public int compare(String k1, String k2) { 
  11.                  return k1.compareTo(k2); 
  12.              } 
  13.          }); 
  14.          sortedMap.putAll(map); 
  15.   
  16.          Iterator itr = sortedMap.entrySet().iterator(); 
  17.          while(itr.hasNext()) { 
  18.              Map.Entry entry = (Map.Entry) itr.next(); 
  19.              // get key 
  20.              String key = (String) entry.getKey(); 
  21.              // get value 
  22.              String value = (String) entry.getValue(); 
  23.   
  24.              System.out.println("key:"+key+",value:"+value); 
  25.          } 
  26.      } 
  27.  } 

運行結果:

  1.   key:1999,value:whx 
  2.   key:2010,value:jay 
  3.   key:3010,value:huaxiao 

4、如何對Map的values進行排序

  1.   List list = new ArrayList(map.entrySet()); 
  2.   Collections.sort(list, (Entry e1, Entry e2) ->
  3.       return e1.getValue().compareTo(e2.getValue()); 
  4.     }); 

實例代碼:

  1.   public class SortValuesMapTest { 
  2.       public static void main(String[] args) { 
  3.           Map<String, String> map = new HashMap<>(); 
  4.           map.put("2010""jay"); 
  5.           map.put("1999""whx"); 
  6.           map.put("3010""huaxiao"); 
  7.    
  8.           List <Map.Entry<String,String>>list = new ArrayList<>(map.entrySet()); 
  9.           Collections.sort(list, (Map.Entry e1, Map.Entry e2)-> { 
  10.                  return e1.getValue().toString().compareTo(e2.getValue().toString()); 
  11.              } 
  12.          ); 
  13.   
  14.          for (Map.Entry entry : list) { 
  15.              System.out.println("key:" + entry.getKey() + ",value:" + entry.getValue()); 
  16.          } 
  17.      } 
  18.  } 

運行結果:

  1.   key:3010,value:huaxiao 
  2.   key:2010,value:jay 
  3.   key:1999,value:whx 

5、如何初始化一個靜態/不可變的Map

初始化一個靜態不可變的map,單單static final+static代碼塊還是不行的,如下:

  1.   public class Test1 { 
  2.       private static final Map <Integer,String>map; 
  3.       static { 
  4.           map = new HashMap<Integer, String>(); 
  5.           map.put(1, "one"); 
  6.           map.put(2, "two"); 
  7.       } 
  8.       public static void main(String[] args) { 
  9.           map.put(3, "three"); 
  10.          Iterator itr = map.entrySet().iterator(); 
  11.          while(itr.hasNext()) { 
  12.              Map.Entry entry = (Map.Entry) itr.next(); 
  13.              // get key 
  14.              Integer key = (Integer) entry.getKey(); 
  15.              // get value 
  16.              String value = (String) entry.getValue(); 
  17.   
  18.              System.out.println("key:"+key+",value:"+value); 
  19.          } 
  20.      } 
  21.  } 

這里面,map繼續添加元素(3,"three"),發現是OK的,運行結果如下:

  1.   key:1,value:one 
  2.   key:2,value:two 
  3.   key:3,value:three    

真正實現一個靜態不可變的map,需要Collections.unmodifiableMap,代碼如下:

  1.   public class Test2 { 
  2.       private static final Map<Integer, String> map; 
  3.       static { 
  4.           Map<Integer,String> aMap = new HashMap<>(); 
  5.           aMap.put(1, "one"); 
  6.           aMap.put(2, "two"); 
  7.           map = Collections.unmodifiableMap(aMap); 
  8.       } 
  9.    
  10.      public static void main(String[] args) { 
  11.          map.put(3, "3"); 
  12.          Iterator itr = map.entrySet().iterator(); 
  13.          while(itr.hasNext()) { 
  14.              Map.Entry entry = (Map.Entry) itr.next(); 
  15.              // get key 
  16.              Integer key = (Integer) entry.getKey(); 
  17.              // get value 
  18.              String value = (String) entry.getValue(); 
  19.   
  20.             System.out.println("key:"+key+",value:"+value); 
  21.          } 
  22.      } 
  23.   
  24.  } 

運行結果如下:

可以發現,繼續往map添加元素是會報錯的,實現真正不可變的map。

6、HashMap, TreeMap, and Hashtable,ConcurrentHashMap的區別

7、如何創建一個空map

如果map是不可變的,可以這樣創建:

  1.   Map map=Collections.emptyMap(); 
  2.   or 
  3.   Map<String,String> map=Collections.<String, String>emptyMap(); 
  4.   //map1.put("1""1"); 運行出錯 

如果你希望你的空map可以添加元素的,可以這樣創建

  1. Map map = new HashMap(); 

8、有關于map的復制

有關于hashmap的復制,在日常開發中,使用也比較多。主要有 =,clone,putAll,但是他們都是淺復制,使用的時候注意啦,可以看一下以下例子:

例子一,使用=復制一個map:

  1.   public class CopyMapAssignTest { 
  2.       public static void main(String[] args) { 
  3.    
  4.           Map<IntegerUser> userMap = new HashMap<>(); 
  5.    
  6.           userMap.put(1, new User("jay", 26)); 
  7.           userMap.put(2, new User("fany", 25)); 
  8.    
  9.           //Shallow clone 
  10.          Map<IntegerUser> clonedMap = userMap; 
  11.   
  12.          //Same as userMap 
  13.          System.out.println(clonedMap); 
  14.   
  15.          System.out.println("\nChanges reflect in both maps \n"); 
  16.   
  17.          //Change a value is clonedMap 
  18.          clonedMap.get(1).setName("test"); 
  19.   
  20.          //Verify content of both maps 
  21.          System.out.println(userMap); 
  22.          System.out.println(clonedMap); 
  23.      } 
  24.  } 

運行結果:

  1.   {1=User{name='jay', age=26}, 2=User{name='fany', age=25}} 
  2.    
  3.   Changes reflect in both maps 
  4.    
  5.   {1=User{name='test', age=26}, 2=User{name='fany', age=25}} 
  6.   {1=User{name='test', age=26}, 2=User{name='fany', age=25}} 

從運行結果看出,對cloneMap修改,兩個map都改變了,所以=是淺復制。

例子二,使用hashmap的clone復制:

  1.   { 
  2.       public static void main(String[] args) { 
  3.           HashMap<IntegerUser> userMap = new HashMap<>(); 
  4.    
  5.           userMap.put(1, new User("jay", 26)); 
  6.           userMap.put(2, new User("fany", 25)); 
  7.    
  8.           //Shallow clone 
  9.           HashMap<IntegerUser> clonedMap = (HashMap<IntegerUser>) userMap.clone(); 
  10.   
  11.          //Same as userMap 
  12.          System.out.println(clonedMap); 
  13.   
  14.          System.out.println("\nChanges reflect in both maps \n"); 
  15.   
  16.          //Change a value is clonedMap 
  17.          clonedMap.get(1).setName("test"); 
  18.   
  19.          //Verify content of both maps 
  20.          System.out.println(userMap); 
  21.          System.out.println(clonedMap); 
  22.      } 
  23.  } 

運行結果:

  1.   {1=User{name='jay', age=26}, 2=User{name='fany', age=25}} 
  2.    
  3.   Changes reflect in both maps 
  4.    
  5.   {1=User{name='test', age=26}, 2=User{name='fany', age=25}} 
  6.   {1=User{name='test', age=26}, 2=User{name='fany', age=25}} 

從運行結果看出,對cloneMap修改,兩個map都改變了,所以hashmap的clone也是淺復制。

例子三,通過putAll操作

  1.   public class CopyPutAllMapTest { 
  2.       public static void main(String[] args) { 
  3.           HashMap<IntegerUser> userMap = new HashMap<>(); 
  4.    
  5.           userMap.put(1, new User("jay", 26)); 
  6.           userMap.put(2, new User("fany", 25)); 
  7.    
  8.           //Shallow clone 
  9.           HashMap<IntegerUser> clonedMap = new HashMap<>(); 
  10.          clonedMap.putAll(userMap); 
  11.   
  12.          //Same as userMap 
  13.          System.out.println(clonedMap); 
  14.   
  15.          System.out.println("\nChanges reflect in both maps \n"); 
  16.   
  17.          //Change a value is clonedMap 
  18.          clonedMap.get(1).setName("test"); 
  19.   
  20.          //Verify content of both maps 
  21.          System.out.println(userMap); 
  22.          System.out.println(clonedMap); 
  23.      } 
  24.  } 

運行結果:

  1.   {1=User{name='jay', age=26}, 2=User{name='fany', age=25}} 
  2.    
  3.   Changes reflect in both maps 
  4.    
  5.   {1=User{name='test', age=26}, 2=User{name='fany', age=25}} 
  6.   {1=User{name='test', age=26}, 2=User{name='fany', age=25}} 

從運行結果看出,對cloneMap修改,兩個map都改變了,所以putAll還是淺復制。

那么,如何實現深度復制呢?

可以使用序列化實現,如下為谷歌Gson序列化HashMap,實現深度復制的例子:

  1.   public class CopyDeepMapTest { 
  2.    
  3.       public static void main(String[] args) { 
  4.           HashMap<IntegerUser> userMap = new HashMap<>(); 
  5.    
  6.           userMap.put(1, new User("jay", 26)); 
  7.           userMap.put(2, new User("fany", 25)); 
  8.    
  9.           //Shallow clone 
  10.          Gson gson = new Gson(); 
  11.          String jsonString = gson.toJson(userMap); 
  12.   
  13.          Type type = new TypeToken<HashMap<IntegerUser>>(){}.getType(); 
  14.          HashMap<IntegerUser> clonedMap = gson.fromJson(jsonString, type); 
  15.   
  16.          //Same as userMap 
  17.          System.out.println(clonedMap); 
  18.   
  19.          System.out.println("\nChanges reflect in only one map \n"); 
  20.   
  21.          //Change a value is clonedMap 
  22.          clonedMap.get(1).setName("test"); 
  23.   
  24.          //Verify content of both maps 
  25.          System.out.println(userMap); 
  26.          System.out.println(clonedMap); 
  27.      } 
  28.  } 

運行結果:

  1.   {1=User{name='jay', age=26}, 2=User{name='fany', age=25}} 
  2.    
  3.   Changes reflect in only one map 
  4.    
  5.   {1=User{name='jay', age=26}, 2=User{name='fany', age=25}} 
  6.   {1=User{name='test', age=26}, 2=User{name='fany', age=25}} 

從運行結果看出,對cloneMap修改,userMap沒有被改變,所以是深度復制。

 

責任編輯:華軒 來源: 撿田螺的小男孩
相關推薦

2010-05-05 17:53:56

web負載均衡

2017-07-06 09:20:51

2020-04-10 16:35:58

GitHub數據網站

2023-03-03 13:42:45

2015-04-22 12:08:16

JAVAMap問題

2017-05-17 17:23:00

2009-06-15 13:17:37

Java初學者Java概念

2010-06-09 16:57:14

路由選擇協議

2023-10-14 18:18:59

Spring

2023-10-12 13:07:29

2016-01-18 11:03:58

程序員搜索技巧

2023-11-22 11:30:03

首席信息官IT管理

2023-07-03 10:46:20

CIOIT領導者

2009-12-01 14:30:08

VS2003使用

2021-04-27 15:28:32

程序員技能開發者

2014-12-03 10:17:09

Java 8

2023-09-15 11:47:13

2009-07-02 17:39:46

Java未來

2011-07-08 14:09:51

iPhone UI

2022-05-06 13:19:13

JS前端
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 日韩爱爱网 | 久久伊 | 视频在线一区二区 | 99久久免费观看 | 成人精品视频在线观看 | 国产综合久久久 | 久热久热 | 久久国产精品久久国产精品 | 91九色视频 | 日韩精品网站 | 国产精品一二三区在线观看 | 在线国产小视频 | 欧美精品第一页 | 欧美一级三级 | 国产一区二区在线播放视频 | 日本三级电影在线看 | 国产一级片av | 免费观看黄a一级视频 | 成人免费淫片aa视频免费 | h视频网站在线观看 | 91玖玖 | 亚洲理论在线观看电影 | 亚洲精品在线看 | 6080yy精品一区二区三区 | 天天干狠狠操 | 欧美性一区二区三区 | 国产激情第一页 | 日韩免费一二三区 | 午夜电影网址 | 午夜电影在线播放 | 一区二区三区 在线 | 成人亚洲精品 | 久久久久久久久中文字幕 | 午夜久久久 | 最新国产视频 | 天堂一区二区三区 | 黄网址在线观看 | 农夫在线精品视频免费观看 | 国产精品区一区二区三 | 国产精品视频网站 | www久久99 |