本文整理了Java中com.google.common.collect.Maps.asMap()
方法的一些代码示例,展示了Maps.asMap()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Maps.asMap()
方法的具体详情如下:
包路径:com.google.common.collect.Maps
类名称:Maps
方法名:asMap
[英]Returns a view of the navigable set as a map, mapping keys from the set according to the specified function.
Specifically, for each k in the backing set, the returned map has an entry mapping k to function.apply(k). The keySet, values, and entrySet views of the returned map iterate in the same order as the backing set.
Modifications to the backing set are read through to the returned map. The returned map supports removal operations if the backing set does. Removal operations write through to the backing set. The returned map does not support put operations.
Warning: If the function rejects null, caution is required to make sure the set does not contain null, because the view cannot stop null from being added to the set.
Warning: This method assumes that for any instance k of key type K, k.equals(k2) implies that k2 is also of type K. Using a key type for which this may not hold, such as ArrayList, may risk a ClassCastException when calling methods on the resulting map view.
[中]以映射的形式返回可导航集的视图,根据指定的函数映射集中的键。
具体地说,对于支持集中的每个k,返回的映射都有一个条目映射k to函数。应用(k)。返回映射的键集、值和入口集视图以与支持集相同的顺序迭代。
对备份集的修改将通读到返回的映射。如果备份集支持删除操作,则返回的映射支持删除操作。删除操作写入备份集。返回的映射不支持put操作。
警告:如果函数拒绝null,则需要注意确保集合不包含null,因为视图无法阻止将null添加到集合中。
警告:此方法假设对于键类型为k的任何实例k,k.equals(k2)意味着k2也是k类型。在结果映射视图上调用方法时,使用可能不适用的键类型(如ArrayList)可能会导致ClassCastException。
代码示例来源:origin: google/guava
@Override
public NavigableMap<K, V> descendingMap() {
return asMap(set.descendingSet(), function);
}
}
代码示例来源:origin: google/guava
private static <N, E> Map<E, EndpointPair<N>> edgeIncidentNodesMap(final Network<N, E> network) {
Function<E, EndpointPair<N>> edgeToIncidentNodesFn =
new Function<E, EndpointPair<N>>() {
@Override
public EndpointPair<N> apply(E edge) {
return network.incidentNodes(edge);
}
};
return Maps.asMap(network.edges(), edgeToIncidentNodesFn);
}
}
代码示例来源:origin: google/guava
private static <N, V> Map<EndpointPair<N>, V> edgeValueMap(final ValueGraph<N, V> graph) {
Function<EndpointPair<N>, V> edgeToValueFn =
new Function<EndpointPair<N>, V>() {
@Override
public V apply(EndpointPair<N> edge) {
return graph.edgeValueOrDefault(edge.nodeU(), edge.nodeV(), null);
}
};
return Maps.asMap(graph.edges(), edgeToValueFn);
}
}
代码示例来源:origin: google/guava
@Override
public NavigableMap<K, V> subMap(
K fromKey, boolean fromInclusive, K toKey, boolean toInclusive) {
return asMap(set.subSet(fromKey, fromInclusive, toKey, toInclusive), function);
}
代码示例来源:origin: google/guava
@Override
public NavigableMap<K, V> headMap(K toKey, boolean inclusive) {
return asMap(set.headSet(toKey, inclusive), function);
}
代码示例来源:origin: google/guava
@Override
public NavigableMap<K, V> tailMap(K fromKey, boolean inclusive) {
return asMap(set.tailSet(fromKey, inclusive), function);
}
代码示例来源:origin: google/guava
@Override
public SortedMap<K, V> headMap(K toKey) {
return asMap(backingSet().headSet(toKey), function);
}
代码示例来源:origin: google/guava
@Override
public SortedMap<K, V> tailMap(K fromKey) {
return asMap(backingSet().tailSet(fromKey), function);
}
代码示例来源:origin: google/guava
@Override
public SortedMap<K, V> subMap(K fromKey, K toKey) {
return asMap(backingSet().subSet(fromKey, toKey), function);
}
代码示例来源:origin: google/guava
private static <N> GraphConnections<N, Presence> connectionsOf(Graph<N> graph, N node) {
Function<Object, Presence> edgeValueFn = Functions.constant(Presence.EDGE_EXISTS);
return graph.isDirected()
? DirectedGraphConnections.ofImmutable(
graph.predecessors(node), Maps.asMap(graph.successors(node), edgeValueFn))
: UndirectedGraphConnections.ofImmutable(
Maps.asMap(graph.adjacentNodes(node), edgeValueFn));
}
代码示例来源:origin: google/guava
private static <N, V> GraphConnections<N, V> connectionsOf(
final ValueGraph<N, V> graph, final N node) {
Function<N, V> successorNodeToValueFn =
new Function<N, V>() {
@Override
public V apply(N successorNode) {
return graph.edgeValueOrDefault(node, successorNode, null);
}
};
return graph.isDirected()
? DirectedGraphConnections.ofImmutable(
graph.predecessors(node), Maps.asMap(graph.successors(node), successorNodeToValueFn))
: UndirectedGraphConnections.ofImmutable(
Maps.asMap(graph.adjacentNodes(node), successorNodeToValueFn));
}
}
代码示例来源:origin: google/guava
public void testAsMapReadsThrough() {
Set<String> strings = Sets.newLinkedHashSet();
Collections.addAll(strings, "one", "two", "three");
Map<String, Integer> map = Maps.asMap(strings, LENGTH_FUNCTION);
assertEquals(ImmutableMap.of("one", 3, "two", 3, "three", 5), map);
assertNull(map.get("four"));
strings.add("four");
assertEquals(ImmutableMap.of("one", 3, "two", 3, "three", 5, "four", 4), map);
assertEquals(Integer.valueOf(4), map.get("four"));
}
代码示例来源:origin: google/guava
public void testAsMapEmpty() {
Set<String> strings = ImmutableSet.of();
Map<String, Integer> map = Maps.asMap(strings, LENGTH_FUNCTION);
assertThat(map.entrySet()).isEmpty();
assertTrue(map.isEmpty());
assertNull(map.get("five"));
}
代码示例来源:origin: google/guava
public void testAsMapWritesThrough() {
Set<String> strings = Sets.newLinkedHashSet();
Collections.addAll(strings, "one", "two", "three");
Map<String, Integer> map = Maps.asMap(strings, LENGTH_FUNCTION);
assertEquals(ImmutableMap.of("one", 3, "two", 3, "three", 5), map);
assertEquals(Integer.valueOf(3), map.remove("two"));
assertThat(strings).containsExactly("one", "three").inOrder();
}
代码示例来源:origin: google/guava
public void testAsMapSortedEmpty() {
SortedSet<String> strings = new NonNavigableSortedSet();
SortedMap<String, Integer> map = Maps.asMap(strings, LENGTH_FUNCTION);
assertThat(map.entrySet()).isEmpty();
assertTrue(map.isEmpty());
assertNull(map.get("five"));
}
代码示例来源:origin: google/guava
public void testAsMapSortedWritesThrough() {
SortedSet<String> strings = new NonNavigableSortedSet();
Collections.addAll(strings, "one", "two", "three");
SortedMap<String, Integer> map = Maps.asMap(strings, LENGTH_FUNCTION);
assertEquals(ImmutableMap.of("one", 3, "two", 3, "three", 5), map);
assertEquals(Integer.valueOf(3), map.remove("two"));
assertThat(strings).containsExactly("one", "three").inOrder();
}
代码示例来源:origin: google/guava
public void testIteratesOnce() {
Map<AnEnum, AnEnum> map =
Maps.asMap(
ImmutableSet.of(AnEnum.A),
new Function<AnEnum, AnEnum>() {
boolean used = false;
@Override
public AnEnum apply(AnEnum ae) {
checkState(!used, "should not be applied more than once");
used = true;
return ae;
}
});
ImmutableMap<AnEnum, AnEnum> copy = Maps.immutableEnumMap(map);
assertThat(copy.entrySet()).containsExactly(Helpers.mapEntry(AnEnum.A, AnEnum.A));
}
代码示例来源:origin: google/guava
@GwtIncompatible // NavigableMap
public void testAsMapNavigableEmpty() {
NavigableSet<String> strings = ImmutableSortedSet.of();
NavigableMap<String, Integer> map = Maps.asMap(strings, LENGTH_FUNCTION);
assertThat(map.entrySet()).isEmpty();
assertTrue(map.isEmpty());
assertNull(map.get("five"));
}
代码示例来源:origin: google/guava
public void testAsMap() {
Set<String> strings = ImmutableSet.of("one", "two", "three");
Map<String, Integer> map = Maps.asMap(strings, LENGTH_FUNCTION);
assertEquals(ImmutableMap.of("one", 3, "two", 3, "three", 5), map);
assertEquals(Integer.valueOf(5), map.get("three"));
assertNull(map.get("five"));
assertThat(map.entrySet())
.containsExactly(mapEntry("one", 3), mapEntry("two", 3), mapEntry("three", 5))
.inOrder();
}
代码示例来源:origin: google/guava
@GwtIncompatible // NavigableMap
public void testAsMapNavigableWritesThrough() {
NavigableSet<String> strings = Sets.newTreeSet();
Collections.addAll(strings, "one", "two", "three");
NavigableMap<String, Integer> map = Maps.asMap(strings, LENGTH_FUNCTION);
assertEquals(ImmutableMap.of("one", 3, "two", 3, "three", 5), map);
assertEquals(Integer.valueOf(3), map.remove("two"));
assertThat(strings).containsExactly("one", "three").inOrder();
assertEquals(mapEntry("three", 5), map.subMap("one", false, "zzz", true).pollLastEntry());
assertThat(strings).contains("one");
}
内容来源于网络,如有侵权,请联系作者删除!