日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Guava包学习---Maps

發(fā)布時(shí)間:2025/7/14 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Guava包学习---Maps 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Maps包方法列表:

還是泛型創(chuàng)建Map:

public static <K, V> HashMap<K, V> newHashMap() {return new HashMap<K, V>();}public static <K, V> HashMap<K, V> newHashMapWithExpectedSize(int expectedSize) {return new HashMap<K, V>(capacity(expectedSize));}public static <K, V> HashMap<K, V> newHashMap(Map<? extends K, ? extends V> map) { return new HashMap<K, V>(map); }public static <K, V> LinkedHashMap<K, V> newLinkedHashMap() { return new LinkedHashMap<K, V>(); }public static <K, V> LinkedHashMap<K, V> newLinkedHashMapWithExpectedSize(int expectedSize) { return new LinkedHashMap<K, V>(capacity(expectedSize)); }public static <K, V> LinkedHashMap<K, V> newLinkedHashMap(Map<? extends K, ? extends V> map) {return new LinkedHashMap<K, V>(map);}public static <K, V> ConcurrentMap<K, V> newConcurrentMap() {return new MapMaker().<K, V>makeMap();}public static <K extends Comparable, V> TreeMap<K, V> newTreeMap() {return new TreeMap<K, V>();}

還有一些EnumMap、IdentitiyMap等,但是不經(jīng)常用,就不貼了。

Map中的不同,還有其他幾種方式,這里就只貼個(gè)最常用的吧。

public static <K, V> MapDifference<K, V> difference(Map<? extends K, ? extends V> left, Map<? extends K, ? extends V> right) {if (left instanceof SortedMap) {SortedMap<K, ? extends V> sortedLeft = (SortedMap<K, ? extends V>) left;SortedMapDifference<K, V> result = difference(sortedLeft, right);return result;}return difference(left, right, Equivalence.equals());}

Set和其他內(nèi)容轉(zhuǎn)Map,這個(gè)方法在獲得某些對象轉(zhuǎn)Map操作比較好用。

public static <K, V> Map<K, V> asMap(Set<K> set, Function<? super K, V> function) {if (set instanceof SortedSet) {return asMap((SortedSet<K>) set, function);} else {return new AsMapView<K, V>(set, function);}}

某些對象轉(zhuǎn)不可變Map,和上面一樣,你只要聲明一個(gè)Guava的Function覆蓋以下它的方法然后傳入進(jìn)來即可:

public static <K, V> ImmutableMap<K, V> toMap(Iterable<K> keys, Function<? super K, V> valueFunction) {return toMap(keys.iterator(), valueFunction);}

接下來這個(gè)有點(diǎn)牛逼的,然后從來不知道該用在哪里的方法,反正我是沒碰到這種場景:

/*** Returns a view of a map where each value is transformed by a function. All* other properties of the map, such as iteration order, are left intact. For* example, the code: <pre> {@code** Map<String, Integer> map = ImmutableMap.of("a", 4, "b", 9);* Function<Integer, Double> sqrt =* new Function<Integer, Double>() {* public Double apply(Integer in) {* return Math.sqrt((int) in);* }* };* Map<String, Double> transformed = Maps.transformValues(map, sqrt);* System.out.println(transformed);}</pre>** ... prints {@code {a=2.0, b=3.0}}.*public static <K, V1, V2> Map<K, V2> transformValues(Map<K, V1> fromMap, Function<? super V1, V2> function) {return transformEntries(fromMap, asEntryTransformer(function));}

基本上每種類型的Map都會(huì)有幾個(gè)方法去處理,不一一列舉。

接下里又是傳入Prediction過濾Map:

@CheckReturnValuepublic static <K, V> Map<K, V> filterKeys(Map<K, V> unfiltered, final Predicate<? super K> keyPredicate) {if (unfiltered instanceof SortedMap) {return filterKeys((SortedMap<K, V>) unfiltered, keyPredicate);} else if (unfiltered instanceof BiMap) {return filterKeys((BiMap<K, V>) unfiltered, keyPredicate);}checkNotNull(keyPredicate);Predicate<Entry<K, ?>> entryPredicate = keyPredicateOnEntries(keyPredicate);return (unfiltered instanceof AbstractFilteredMap)? filterFiltered((AbstractFilteredMap<K, V>) unfiltered, entryPredicate): new FilteredKeyMap<K, V>(checkNotNull(unfiltered), keyPredicate, entryPredicate);} @CheckReturnValuepublic static <K, V> Map<K, V> filterValues(Map<K, V> unfiltered, final Predicate<? super V> valuePredicate) {if (unfiltered instanceof SortedMap) {return filterValues((SortedMap<K, V>) unfiltered, valuePredicate);} else if (unfiltered instanceof BiMap) {return filterValues((BiMap<K, V>) unfiltered, valuePredicate);}return filterEntries(unfiltered, Maps.<V>valuePredicateOnEntries(valuePredicate));} @CheckReturnValuepublic static <K, V> Map<K, V> filterEntries(Map<K, V> unfiltered, Predicate<? super Entry<K, V>> entryPredicate) {if (unfiltered instanceof SortedMap) {return filterEntries((SortedMap<K, V>) unfiltered, entryPredicate);} else if (unfiltered instanceof BiMap) {return filterEntries((BiMap<K, V>) unfiltered, entryPredicate);}checkNotNull(entryPredicate);return (unfiltered instanceof AbstractFilteredMap)? filterFiltered((AbstractFilteredMap<K, V>) unfiltered, entryPredicate): new FilteredEntryMap<K, V>(checkNotNull(unfiltered), entryPredicate);}

應(yīng)該我碰到的場景太少了,List、set、map中都有大量代碼去處理Navigate類型和immutable類型的集合,感覺還是用的太少了。需要再深入研究一下。

轉(zhuǎn)載于:https://www.cnblogs.com/congsg2016/p/5119676.html

總結(jié)

以上是生活随笔為你收集整理的Guava包学习---Maps的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。