com.google.common.collect.Maps.transformEntries()方法的使用及代码示例

x33g5p2x  于2022-01-24 转载在 其他  
字(8.4k)|赞(0)|评价(0)|浏览(993)

本文整理了Java中com.google.common.collect.Maps.transformEntries()方法的一些代码示例,展示了Maps.transformEntries()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Maps.transformEntries()方法的具体详情如下:
包路径:com.google.common.collect.Maps
类名称:Maps
方法名:transformEntries

Maps.transformEntries介绍

[英]Returns a view of a map whose values are derived from the original map's entries. In contrast to #transformValues, this method's entry-transformation logic may depend on the key as well as the value.

All other properties of the transformed map, such as iteration order, are left intact. For example, the code:

Map options =}; 
Map transformed = 
Maps.transformEntries(options, flagPrefixer); 
System.out.println(transformed);}

... prints {verbose=verbose, sort=nosort}}.

Changes in the underlying map are reflected in this view. Conversely, this view supports removal operations, and these are reflected in the underlying map.

It's acceptable for the underlying map to contain null keys and null values provided that the transformer is capable of accepting null inputs. The transformed map might contain null values if the transformer sometimes gives a null result.

The returned map is not thread-safe or serializable, even if the underlying map is.

The transformer is applied lazily, invoked when needed. This is necessary for the returned map to be a view, but it means that the transformer will be applied many times for bulk operations like Map#containsValue and Object#toString. For this to perform well, transformer should be fast. To avoid lazy evaluation when the returned map doesn't need to be a view, copy the returned map into a new map of your choosing.

Warning: This method assumes that for any instance k of EntryTransformer key type K, k.equals(k2) implies that k2 is also of type K. Using an EntryTransformer key type for which this may not hold, such as ArrayList, may risk a ClassCastException when calling methods on the transformed map.
[中]返回其值从原始贴图的条目派生的贴图视图。与#transformValue不同,此方法的条目转换逻辑可能取决于键和值。
变换映射的所有其他属性(如迭代顺序)保持不变。例如,代码:

Map options =}; 
Map transformed = 
Maps.transformEntries(options, flagPrefixer); 
System.out.println(transformed);}

... 打印{verbose=verbose,sort=nosort}。
基础地图中的更改将反映在此视图中。相反,此视图支持删除操作,这些操作将反映在基础映射中。
如果转换器能够接受空输入,那么底层映射可以包含空键和空值。如果转换器有时给出空结果,则转换后的映射可能包含空值。
返回的映射不是线程安全的或可序列化的,即使底层映射是。
转换器是惰性应用的,在需要时调用。这对于返回的映射成为视图是必要的,但这意味着转换器将被多次应用于诸如map#containsValue和Object#toString之类的批量操作。为了使其性能良好,变压器应快速运行。当返回的映射不需要是视图时,为了避免延迟计算,请将返回的映射复制到您选择的新映射中。
警告:此方法假定,对于EntryTransformer密钥类型k的任何实例k,k.equals(k2)表示k2也是k类型。使用可能不适用的EntryTransformer密钥类型,例如ArrayList,在转换映射上调用方法时可能会发生ClassCastException。

代码示例

代码示例来源:origin: google/guava

@Override
Map<K, Collection<V2>> createAsMap() {
 return Maps.transformEntries(
   fromMultimap.asMap(),
   new EntryTransformer<K, Collection<V1>, Collection<V2>>() {
    @Override
    public Collection<V2> transformEntry(K key, Collection<V1> value) {
     return transform(key, value);
    }
   });
}

代码示例来源:origin: apache/incubator-druid

private static Map<String, Object> getSerializableClientProperties(final Map<String, Object> clientProperties)
{
 return Maps.transformEntries(
   clientProperties,
   new Maps.EntryTransformer<String, Object, Object>()
   {
    @Override
    public Object transformEntry(String key, Object value)
    {
     if (value instanceof LongString) {
      return value.toString();
     }
     return value;
    }
   }
 );
}

代码示例来源:origin: google/guava

@Override
 public SortedMap<K, V2> tailMap(K fromKey) {
  return transformEntries(fromMap().tailMap(fromKey), transformer);
 }
}

代码示例来源:origin: google/guava

@Override
public SortedMap<K, V2> headMap(K toKey) {
 return transformEntries(fromMap().headMap(toKey), transformer);
}

代码示例来源:origin: google/guava

@Override
public SortedMap<K, V2> subMap(K fromKey, K toKey) {
 return transformEntries(fromMap().subMap(fromKey, toKey), transformer);
}

代码示例来源:origin: google/j2objc

@Override
Map<K, Collection<V2>> createAsMap() {
 return Maps.transformEntries(
   fromMultimap.asMap(),
   new EntryTransformer<K, Collection<V1>, Collection<V2>>() {
    @Override
    public Collection<V2> transformEntry(K key, Collection<V1> value) {
     return transform(key, value);
    }
   });
}

代码示例来源:origin: google/guava

@Override
public NavigableMap<K, V2> subMap(
  K fromKey, boolean fromInclusive, K toKey, boolean toInclusive) {
 return transformEntries(
   fromMap().subMap(fromKey, fromInclusive, toKey, toInclusive), transformer);
}

代码示例来源:origin: google/guava

return transformEntries(fromMap, asEntryTransformer(function));

代码示例来源:origin: google/guava

@Override
public NavigableMap<K, V2> headMap(K toKey, boolean inclusive) {
 return transformEntries(fromMap().headMap(toKey, inclusive), transformer);
}

代码示例来源:origin: google/guava

@Override
public NavigableMap<K, V2> descendingMap() {
 return transformEntries(fromMap().descendingMap(), transformer);
}

代码示例来源:origin: google/guava

@Override
public NavigableMap<K, V2> tailMap(K fromKey, boolean inclusive) {
 return transformEntries(fromMap().tailMap(fromKey, inclusive), transformer);
}

代码示例来源:origin: google/guava

return transformEntries(fromMap, asEntryTransformer(function));

代码示例来源:origin: google/guava

@Override
 protected Map<String, String> create(Entry<String, String>[] entries) {
  Map<String, String> map = Maps.newLinkedHashMap();
  for (Entry<String, String> entry : entries) {
   map.put(entry.getKey(), encode(entry.getValue()));
  }
  return Maps.transformEntries(map, DECODE_ENTRY_TRANSFORMER);
 }
})

代码示例来源:origin: google/guava

@Override
 protected NavigableMap<String, String> create(Entry<String, String>[] entries) {
  NavigableMap<String, String> map = new SafeTreeMap<>();
  for (Entry<String, String> entry : entries) {
   map.put(entry.getKey(), encode(entry.getValue()));
  }
  return Maps.transformEntries(map, DECODE_ENTRY_TRANSFORMER);
 }
})

代码示例来源:origin: google/guava

public void testTransformEntriesExample() {
 Map<String, Boolean> options = ImmutableMap.of("verbose", true, "sort", false);
 EntryTransformer<String, Boolean, String> flagPrefixer =
   new EntryTransformer<String, Boolean, String>() {
    @Override
    public String transformEntry(String key, Boolean value) {
     return value ? key : "no" + key;
    }
   };
 Map<String, String> transformed = transformEntries(options, flagPrefixer);
 assertEquals("{verbose=verbose, sort=nosort}", transformed.toString());
}

代码示例来源:origin: google/guava

@Override
 protected SortedMap<String, String> create(Entry<String, String>[] entries) {
  SortedMap<String, String> map = new NonNavigableSortedMap();
  for (Entry<String, String> entry : entries) {
   map.put(entry.getKey(), encode(entry.getValue()));
  }
  return Maps.transformEntries(map, DECODE_ENTRY_TRANSFORMER);
 }
})

代码示例来源:origin: google/guava

public void testTransformEntries() {
 Map<String, String> map = ImmutableMap.of("a", "4", "b", "9");
 EntryTransformer<String, String, String> concat =
   new EntryTransformer<String, String, String>() {
    @Override
    public String transformEntry(String key, String value) {
     return key + value;
    }
   };
 Map<String, String> transformed = transformEntries(map, concat);
 assertEquals(ImmutableMap.of("a", "a4", "b", "b9"), transformed);
}

代码示例来源:origin: apache/incubator-druid

@Override
public Object compute(Map<String, Object> values)
{
 // Maps.transformEntries is lazy, will only finalize values we actually read.
 final Map<String, Object> finalizedValues = Maps.transformEntries(
   values,
   (String k, Object v) -> {
    final Function<Object, Object> finalizer = finalizers.get(k);
    return finalizer != null ? finalizer.apply(v) : v;
   }
 );
 return parsed.eval(Parser.withMap(finalizedValues)).value();
}

代码示例来源:origin: google/guava

@GwtIncompatible // NavigableMap
public void testNavigableMapTransformEntries() {
 NavigableMap<String, String> map = ImmutableSortedMap.of("a", "4", "b", "9");
 EntryTransformer<String, String, String> concat =
   new EntryTransformer<String, String, String>() {
    @Override
    public String transformEntry(String key, String value) {
     return key + value;
    }
   };
 NavigableMap<String, String> transformed = transformEntries(map, concat);
 assertEquals(ImmutableSortedMap.of("a", "a4", "b", "b9"), transformed);
}

代码示例来源:origin: google/guava

public void testSortedMapTransformEntries() {
 SortedMap<String, String> map = sortedNotNavigable(ImmutableSortedMap.of("a", "4", "b", "9"));
 EntryTransformer<String, String, String> concat =
   new EntryTransformer<String, String, String>() {
    @Override
    public String transformEntry(String key, String value) {
     return key + value;
    }
   };
 SortedMap<String, String> transformed = transformEntries(map, concat);
 /*
  * We'd like to sanity check that we didn't get a NavigableMap out, but we
  * can't easily do so while maintaining GWT compatibility.
  */
 assertEquals(ImmutableSortedMap.of("a", "a4", "b", "b9"), transformed);
}

相关文章