Guava包学习---Maps
生活随笔
收集整理的這篇文章主要介紹了
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)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: NSArray基础-数组排序
- 下一篇: python笔记-列表和元组