java-測試開始map基本操作

來源:互聯網
上載者:User

標籤:new t   ash   排序   異常   hash   set   for   實作類別   rgs   

package java_test;import java.util.ArrayList;import java.util.Collections;import java.util.Comparator;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.Set;import java.util.TreeMap;public class mapDemo {public void mapAction(){//map基本操作Map<String,String> map =new HashMap<String,String>();map.put("a", "this is a");//往map裡添加一堆索引值對int len=map.size();//一共有多少對索引值對System.out.println(len);//輸出1String a=map.get("a");//擷取key為a的value值System.out.println(a);//輸出this is aSystem.out.println(map.get("b"));//擷取一個不存在的key的value值時,傳回值為null,並不會拋出異常map.put("a", "this is the second a");//添加一個已經存在key,後面添加的value值會覆蓋前面已經存在的valueSystem.out.println(map.get("a"));//輸出this is the second aboolean c=map.containsKey("a");//判斷map中是否存在key  aSystem.out.println(c);//輸出trueboolean v=map.containsValue("this is the second a");//判斷map中是否存在給定的valueSystem.out.println(v);//輸出trueMap<String,String> m1=new HashMap<String,String>();m1.put("a", "this is the third a");m1.put("d", "this is d");System.out.println(m1);map.putAll(m1);//取map與m1的並集,如果m1中已經在map中存在的key,則m1中的value會覆蓋map中的存在key的value,並集的值會賦值給mapSystem.out.println(map);}public void loopMap(){Map<String,String> map=new HashMap<String,String>();map.put("a", "this is a");map.put("b", "this is b");/** * set可以理解為一直特殊的List,只是裡面的元素對象是不允許重複的,keySet是將key都放到一個集合裡面去 * HashMap是無序排列的keySet後,set對象也是無序的 */Set<String> set=map.keySet();for(String s:set){System.out.println(map.get(s));//迴圈輸出map的value}}public void loopRemoveMap(){Map<Integer,String> map=new HashMap<Integer,String>();map.put(1, "this is a");map.put(2, "this is b");/** * set雖然可以看做是一個特殊的list,但是set沒有像list一樣有一個通過get取值的方式 * 要想取值,可以將set轉為list,或者轉為iterator */Set<Integer> set =map.keySet();List<Integer> list=new ArrayList<Integer>(set);//set轉為listfor(int i=0;i<list.size();i++){map.remove(list.get(i));}System.out.println(map);//輸出{}}public void clearMap(){//清空mapMap<Integer,String> map=new HashMap<Integer,String>();map.put(1, "this is a");map.put(2, "this is b");boolean e =map.isEmpty();//判斷map裡是否存在索引值對System.out.println(e);//輸出truemap.clear();//清除map裡所有的索引值對e=map.isEmpty();System.out.println(e);//輸出true}public void sortMapByKey(){//簡單排序Map<String,String> map=new HashMap<String,String>();map.put("a", "this is a");map.put("c", "this is c");map.put("b", "this is b");System.out.println(map);/** * TreeMap是一個按key進行聖墟排列的一個Map實作類別 * TreeMap會自動的把裡面的索引值對進行排序 * 利用這一點,將HashMap轉換為TreeMap,即可實現Map按key進行排序 */Map<String,String> tm=new TreeMap<String,String>();tm.putAll(map);map=tm;System.out.println(map);}public void sortFuZaMapByKey(){Map<String,String> map=new HashMap<String,String>();map.put("a", "this is a");map.put("c", "this is c");map.put("b", "this is b");/** * LonkedHashMap是會記錄你put進去的順序,輸出時,會按照你put進去的順序進行輸出 * 利用這一點,將HashMap的key按要求排列號,然後再put進一個LinkedHashMap即可實現map的複雜排序 */Map<String,String> lm= new HashMap<String,String>();List<String> list =new ArrayList<String>(map.keySet());Collections.sort(list,new Comparator<String>)(){}}public static void main(String[] args) {mapDemo m=new mapDemo();m.mapAction();m.loopMap();m.loopRemoveMap();m.sortMapByKey();}}

  

java-測試開始map基本操作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.