Guava(四):集合基礎總結之Map

其實Guavad的集合操作適合我們平時使用的原生的集合是一樣的,只是他將我們平時操作的集合更加的流暢優雅加單。其實Map就和List一樣也是在創建的時候和其他的一些很讚的方法,但是呢好像這些方法我們平時的工作中用到的很少,但是呢我們還是來看看把。

首先說一下這幾個方法:

1:創建Map方法:

	Map<String,String> guavaMap = Maps.newHashMap();

2:集合diff方法:兩個Map中都有的映射項,包括匹配的鍵與值

	MapDifference<String,String> diffMap = Maps.difference(map,guavaMap);

3:也是集合方法:鍵只存在於左邊Map的映射項,也就是說左邊參數map裏面有的而右邊沒有的就展示:

	entriesOnlyOnLeft()

4:也是集合方法:鍵只存在於右邊Map的映射項

	entriesOnlyOnRight()

好了上代碼:

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.MapDifference;
import com.google.common.collect.Maps;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by luyangli on 15-9-19.
 */
public class MapsTest {
    public static void main(String[] args) {
        Map<String,String> map = new HashMap<String, String>();
        map.put("3","c");
        map.put("1","a");
        map.put("2","b");
        map.put("4","t");
        System.out.println("========原生Map=======");
        for (Map.Entry<String, String> entry : map.entrySet()) {
            System.out.println(entry.getKey() + ":" + entry.getValue());
        }

        Map<String,String> guavaMap = Maps.newHashMap();
        guavaMap.put("3","c");
        guavaMap.put("1","a");
        guavaMap.put("2","b");
        guavaMap.put("5","t");
        System.out.println("========Guava Map=======");
        for (Map.Entry<String, String> entry : guavaMap.entrySet()) {
            System.out.println(entry.getKey() + ":" + entry.getValue());
        }

        MapDifference<String,String> diffMap = Maps.difference(map,guavaMap);
        System.out.println("========Guava diff Map=======");
        for (Map.Entry<String, String> entry : diffMap.entriesInCommon().entrySet()) {
            System.out.println(entry.getKey() + ":" + entry.getValue());
        }
        System.out.println("========Guava diff Left Map=======");
        for (Map.Entry<String, String> entry : diffMap.entriesOnlyOnLeft().entrySet()) {
            System.out.println(entry.getKey() + ":" + entry.getValue());
        }
        System.out.println("========Guava diff Right Map=======");
        for (Map.Entry<String, String> entry : diffMap.entriesOnlyOnRight().entrySet()) {
            System.out.println(entry.getKey() + ":" + entry.getValue());
        }

//        Map<String, Integer> left = ImmutableMap.of("a", 1, "b", 2, "c", 3);
//        Map<String, Integer> right = ImmutableMap.of("a", 1, "b", 2, "c", 3);
//        MapDifference<String, Integer> diff = Maps.difference(left, right);
//        Map<String,Integer> map2 = diff.entriesInCommon();
//        System.out.println("========Guava diff Map=======");
//        for (Map.Entry<String, Integer> entry : map2.entrySet()) {
//            System.out.println(entry.getKey() + ":" + entry.getValue());
//        }

     }
}

我們來看一下效果:

========原生Map=======
3:c
2:b
1:a
4:t
========Guava Map=======
3:c
2:b
1:a
5:t
========Guava diff Map=======
3:c
2:b
1:a
========Guava diff Left Map=======
4:t
========Guava diff Right Map=======
5:t
========Guava diff Map=======
b:2
c:3
a:1

好了先寫在這,我要去健身房了,回來補上Map的一下排序。。。

好高心昨天在健身房要到了心儀的女生的微信號,好開心好開心,昨天我也是購拼的,昨天爲了要微信號在健身房帶了4個小時,累死了。

好了今天我們學習以下Map的幾種遍歷方法:我在今天整理了四種Map的遍歷方式,現在原樣奉上:

System.out.println("=====第一種MapKey遍歷,遍歷KeyValue=====");
//第一種MapKey遍歷,遍歷KeyValue
for (String key : map.keySet()) {
    System.out.println("Key : " + key + " and value : " + map.get(key));
}

//第二種使用entries進行遍歷
System.out.println("=====第二種使用entries進行遍歷=====");
for (Map.Entry<String, String> entry : map.entrySet()) {
    System.out.println("Key : " + entry.getKey() + " and value : " + entry.getValue());
}

//第三種通過Map.entrySet使用iterator遍歷keyvalue
System.out.println("=====第三種通過Map.entrySet使用iterator遍歷keyvalue=====");
Iterator<Map.Entry<String, String>> it = map.entrySet().iterator();
while (it.hasNext()){
    Map.Entry<String,String> entry = it.next();
    System.out.println("Key : " + entry.getKey() + " and value : " + entry.getValue());
}

//第四種獲取MapKey或者Value
System.out.println("=====第四種獲取MapKey=====");
for (String key : guavaMap.keySet()) {
    System.out.println("Key : " + key);
}

System.out.println("=====第四種獲取MapValue=====");
for (String value : guavaMap.values()) {
    System.out.println("value : " + value);
}

看一下結果:

=====第一種Map的Key遍歷,遍歷Key和Value=====
Key : 3 and value : c
Key : 2 and value : b
Key : 1 and value : a
Key : 4 and value : t
=====第二種使用entries進行遍歷=====
Key : 3 and value : c
Key : 2 and value : b
Key : 1 and value : a
Key : 4 and value : t
=====第三種通過Map.entrySet使用iterator遍歷key和value=====
Key : 3 and value : c
Key : 2 and value : b
Key : 1 and value : a
Key : 4 and value : t
=====第四種獲取Map的Key=====
Key : 3
Key : 2
Key : 1
Key : 5
=====第四種獲取MapValue=====
value : c
value : b
value : a
value : t

其實我們的幾種方法都是OK的,就是有效率之分啦,人家提供幾種不同的方法就是有幾種區別啦:1.就是方法升級,也就是更方便啦 2.就是有更高效的手段啦 3.....

所以我們現在來看一下我們的幾種方法:第四種不是很難常用,就不在比較之內。

第一種雖然很簡便,耶很好看清楚,但是呢這個效率是最低的,應爲從Map中取出Key值在通過Key值來取出Value,這個是很費效率的操作,這就是簡便的代價--犧牲效率。

第三種方法是之前map 老版本的唯一指定遍歷方式,第二種是他的升級,也就是說他們的效率相差不多。但是呢現在大家都習慣於用第二種方式啦。

至於具體的問題,具體分析,具體決策啦。。




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章