Guava包学习---Maps

Maps包方法列表:

还是泛型创建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等,但是不经常用,就不贴了。

Map中的不同,还有其他几种方式,这里就只贴个最常用的吧。

 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和其他内容转Map,这个方法在获得某些对象转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);
    }
  }

某些对象转不可变Map,和上面一样,你只要声明一个Guava的Function覆盖以下它的方法然后传入进来即可:

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

接下来这个有点牛逼的,然后从来不知道该用在哪里的方法,反正我是没碰到这种场景:

  /**
   * 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都会有几个方法去处理,不一一列举。

接下里又是传入Prediction过滤Map:

  @CheckReturnValue
  public 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);
  }
  @CheckReturnValue
  public 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));
  }
  @CheckReturnValue
  public 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);
  }

应该我碰到的场景太少了,List、set、map中都有大量代码去处理Navigate类型和immutable类型的集合,感觉还是用的太少了。需要再深入研究一下。

posted on 2016-01-10 22:03  见习架构师  阅读(1135)  评论(0编辑  收藏  举报

导航