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

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

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

Maps.transformValues介绍

[英]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:

Map map = ImmutableMap.of("a", 4, "b", 9);}; 
Map transformed = Maps.transformValues(map, sqrt); 
System.out.println(transformed);}

... prints {a=2.0, b=3.0}}.

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 even null values provided that the function is capable of accepting null input. The transformed map might contain null values, if the function sometimes gives a null result.

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

The function is applied lazily, invoked when needed. This is necessary for the returned map to be a view, but it means that the function will be applied many times for bulk operations like Map#containsValue and Map.toString(). For this to perform well, function 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.
[中]返回映射的视图,其中每个值都由函数转换。映射的所有其他属性(如迭代顺序)保持不变。例如,代码:

Map map = ImmutableMap.of("a", 4, "b", 9);}; 
Map transformed = Maps.transformValues(map, sqrt); 
System.out.println(transformed);}

。。。打印{a=2.0,b=3.0}。
基础地图中的更改将反映在此视图中。相反,此视图支持删除操作,这些操作将反映在基础映射中。
如果函数能够接受空输入,则底层映射可以包含空键,甚至空值。如果函数有时给出空结果,则转换后的映射可能包含空值。
返回的映射不是线程安全的或可序列化的,即使底层映射是。
该函数被惰性地应用,在需要时调用。这对于返回的映射成为视图是必要的,但这意味着该函数将多次应用于诸如map#containsValue和map之类的批量操作。toString()。要使其性能良好,功能应该是快速的。当返回的映射不需要是视图时,为了避免延迟计算,请将返回的映射复制到您选择的新映射中。

代码示例

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

@Override
 public Map<C, V2> apply(Map<C, V1> row) {
  return Maps.transformValues(row, function);
 }
};

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

@Override
 public Map<R, V2> apply(Map<R, V1> column) {
  return Maps.transformValues(column, function);
 }
};

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

@Override
 public Map<C, Map<R, V2>> columnMap() {
  Function<Map<R, V1>, Map<R, V2>> columnFunction =
    new Function<Map<R, V1>, Map<R, V2>>() {
     @Override
     public Map<R, V2> apply(Map<R, V1> column) {
      return Maps.transformValues(column, function);
     }
    };
  return Maps.transformValues(fromTable.columnMap(), columnFunction);
 }
}

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

@Override
public Map<R, Map<C, V>> rowMap() {
 Function<Map<C, V>, Map<C, V>> wrapper = unmodifiableWrapper();
 return Collections.unmodifiableMap(Maps.transformValues(super.rowMap(), wrapper));
}

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

@Override
public Map<R, Map<C, V2>> rowMap() {
 Function<Map<C, V1>, Map<C, V2>> rowFunction =
   new Function<Map<C, V1>, Map<C, V2>>() {
    @Override
    public Map<C, V2> apply(Map<C, V1> row) {
     return Maps.transformValues(row, function);
    }
   };
 return Maps.transformValues(fromTable.rowMap(), rowFunction);
}

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

private Map<String, ImmutableMultiset<String>> parametersAsMap() {
 return Maps.transformValues(
   parameters.asMap(),
   new Function<Collection<String>, ImmutableMultiset<String>>() {
    @Override
    public ImmutableMultiset<String> apply(Collection<String> input) {
     return ImmutableMultiset.copyOf(input);
    }
   });
}

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

@Override
public Map<C, Map<R, V>> columnMap() {
 Function<Map<R, V>, Map<R, V>> wrapper = unmodifiableWrapper();
 return Collections.unmodifiableMap(Maps.transformValues(super.columnMap(), wrapper));
}

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

@Override
public SortedMap<R, Map<C, V>> rowMap() {
 Function<Map<C, V>, Map<C, V>> wrapper = unmodifiableWrapper();
 return Collections.unmodifiableSortedMap(Maps.transformValues(delegate().rowMap(), wrapper));
}

代码示例来源: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.transformValues(map, DECODE_FUNCTION);
 }
})

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

@Override
protected Map<String, String> makePopulatedMap() {
 Map<String, Integer> underlying = Maps.newHashMap();
 underlying.put("a", 1);
 underlying.put("b", 2);
 underlying.put("c", 3);
 return Maps.transformValues(underlying, Functions.toStringFunction());
}

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

public void testTransformValues() {
 Map<String, Integer> map = ImmutableMap.of("a", 4, "b", 9);
 Map<String, Double> transformed = transformValues(map, SQRT_FUNCTION);
 assertEquals(ImmutableMap.of("a", 2.0, "b", 3.0), transformed);
}

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

@Override
 protected SortedMap<String, String> makePopulatedMap() {
  SortedMap<String, Integer> underlying = Maps.newTreeMap();
  underlying.put("a", 1);
  underlying.put("b", 2);
  underlying.put("c", 3);
  return Maps.transformValues(underlying, Functions.toStringFunction());
 }
}

代码示例来源: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.transformValues(map, DECODE_FUNCTION);
 }
})

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

public void testTransformEmptyMapEquality() {
 Map<String, String> map =
   Maps.transformValues(ImmutableMap.<String, Integer>of(), Functions.toStringFunction());
 assertMapsEqual(Maps.newHashMap(), map);
}

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

public void testTransformSingletonMapEquality() {
 Map<String, String> map =
   Maps.transformValues(ImmutableMap.of("a", 1), Functions.toStringFunction());
 Map<String, String> expected = ImmutableMap.of("a", "1");
 assertMapsEqual(expected, map);
 assertEquals(expected.get("a"), map.get("a"));
}

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

public void testTransformRemoveEntry() {
 Map<String, Integer> underlying = Maps.newHashMap();
 underlying.put("a", 1);
 Map<String, String> map = Maps.transformValues(underlying, Functions.toStringFunction());
 assertEquals("1", map.remove("a"));
 assertNull(map.remove("b"));
}

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

@Override
protected Map<String, String> makeEmptyMap() {
 Map<String, Integer> underlying = Maps.newHashMap();
 return Maps.transformValues(
   new UnmodifiableIteratorMap<String, Integer>(underlying), Functions.toStringFunction());
}

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

public void testTransformIdentityFunctionEquality() {
 Map<String, Integer> underlying = ImmutableMap.of("a", 1);
 Map<String, Integer> map = Maps.transformValues(underlying, Functions.<Integer>identity());
 assertMapsEqual(underlying, map);
}

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

public void testTransformIdentityFunctionEquality() {
 Map<String, Integer> underlying = ImmutableMap.of("a", 1);
 Map<String, Integer> map = Maps.transformValues(underlying, Functions.<Integer>identity());
 assertMapsEqual(underlying, map);
}

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

public void testTransformRemoveEntry() {
 Map<String, Integer> underlying = Maps.newHashMap();
 underlying.put("a", 1);
 Map<String, String> map = Maps.transformValues(underlying, Functions.toStringFunction());
 assertEquals("1", map.remove("a"));
 assertNull(map.remove("b"));
}

相关文章