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

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

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

Maps.filterEntries介绍

[英]Returns a bimap containing the mappings in unfiltered that satisfy a predicate. The returned bimap is a live view of unfiltered; changes to one affect the other.

The resulting bimap's keySet(), entrySet(), and values() views have iterators that don't support remove(), but all other methods are supported by the bimap and its views. When given a key/value pair that doesn't satisfy the predicate, the bimap's put(), forcePut() and putAll() methods throw an IllegalArgumentException. Similarly, the map's entries have an Entry#setValuemethod that throws an IllegalArgumentException when the existing key and the provided value don't satisfy the predicate.

When methods such as removeAll() and clear() are called on the filtered bimap or its views, only mappings that satisfy the filter will be removed from the underlying bimap.

The returned bimap isn't threadsafe or serializable, even if unfiltered is.

Many of the filtered bimap's methods, such as size(), iterate across every key/value mapping in the underlying bimap and determine which satisfy the filter. When a live view is not needed, it may be faster to copy the filtered bimap and use the copy.

Warning: entryPredicate must be consistent with equals , as documented at Predicate#apply.
[中]返回一个bimap,其中包含满足谓词的未筛选映射。返回的bimap是未过滤的实时视图;对其中一个的更改会影响另一个。
生成的bimap的keySet()、entrySet()和values()视图具有不支持remove()的迭代器,但bimap及其视图支持所有其他方法。当给定不满足谓词的键/值对时,bimap的put()、forcePut()和putAll()方法会抛出IllegalArgumentException。类似地,映射的条目有一个条目#setValuemethod,当现有键和提供的值不满足谓词时,该方法会抛出IllegalArgumentException。
当对过滤后的bimap或其视图调用removeAll()和clear()等方法时,只会从基础bimap中删除满足过滤器的映射。
返回的bimap不是线程安全的或可序列化的,即使是未筛选的。
许多过滤后的bimap方法,例如size(),都会在基础bimap中的每个键/值映射上迭代,并确定哪些方法满足过滤器的要求。当不需要实时视图时,复制过滤后的bimap并使用副本可能会更快。
警告:entryPredicate必须与equals一致,如Predicate#apply中所述。

代码示例

代码示例来源:origin: thinkaurelius/titan

private static Map<ConfigElement.PathIdentifier, Object> getManagedSubset(Map<ConfigElement.PathIdentifier, Object> m) {
  return Maps.filterEntries(m, new Predicate<Map.Entry<ConfigElement.PathIdentifier, Object>>() {
    @Override
    public boolean apply(@Nullable Map.Entry<ConfigElement.PathIdentifier, Object> entry) {
      assert entry.getKey().element.isOption();
      return ((ConfigOption)entry.getKey().element).isManaged();
    }
  });
}

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

@Override
public NavigableMap<K, V> subMap(
  K fromKey, boolean fromInclusive, K toKey, boolean toInclusive) {
 return filterEntries(
   unfiltered.subMap(fromKey, fromInclusive, toKey, toInclusive), entryPredicate);
}

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

@Override
 public NavigableMap<K, V> tailMap(K fromKey, boolean inclusive) {
  return filterEntries(unfiltered.tailMap(fromKey, inclusive), entryPredicate);
 }
}

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

@Override
public NavigableMap<K, V> headMap(K toKey, boolean inclusive) {
 return filterEntries(unfiltered.headMap(toKey, inclusive), entryPredicate);
}

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

@Override
public NavigableMap<K, V> descendingMap() {
 return filterEntries(unfiltered.descendingMap(), entryPredicate);
}

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

/**
 * Returns a map containing the mappings in {@code unfiltered} whose values satisfy a predicate.
 * The returned map is a live view of {@code unfiltered}; changes to one affect the other.
 *
 * <p>The resulting map's {@code keySet()}, {@code entrySet()}, and {@code values()} views have
 * iterators that don't support {@code remove()}, but all other methods are supported by the map
 * and its views. When given a value that doesn't satisfy the predicate, the map's {@code put()},
 * {@code putAll()}, and {@link Entry#setValue} methods throw an {@link IllegalArgumentException}.
 *
 * <p>When methods such as {@code removeAll()} and {@code clear()} are called on the filtered map
 * or its views, only mappings whose values satisfy the filter will be removed from the underlying
 * map.
 *
 * <p>The returned map isn't threadsafe or serializable, even if {@code unfiltered} is.
 *
 * <p>Many of the filtered map's methods, such as {@code size()}, iterate across every key/value
 * mapping in the underlying map and determine which satisfy the filter. When a live view is
 * <i>not</i> needed, it may be faster to copy the filtered map and use the copy.
 *
 * <p><b>Warning:</b> {@code valuePredicate} must be <i>consistent with equals</i>, as documented
 * at {@link Predicate#apply}. Do not provide a predicate such as {@code
 * Predicates.instanceOf(ArrayList.class)}, which is inconsistent with equals.
 */
public static <K, V> Map<K, V> filterValues(
  Map<K, V> unfiltered, final Predicate<? super V> valuePredicate) {
 return filterEntries(unfiltered, Maps.<V>valuePredicateOnEntries(valuePredicate));
}

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

/**
 * Returns a bimap containing the mappings in {@code unfiltered} whose keys satisfy a predicate.
 * The returned bimap is a live view of {@code unfiltered}; changes to one affect the other.
 *
 * <p>The resulting bimap's {@code keySet()}, {@code entrySet()}, and {@code values()} views have
 * iterators that don't support {@code remove()}, but all other methods are supported by the bimap
 * and its views. When given a key that doesn't satisfy the predicate, the bimap's {@code put()},
 * {@code forcePut()} and {@code putAll()} methods throw an {@link IllegalArgumentException}.
 *
 * <p>When methods such as {@code removeAll()} and {@code clear()} are called on the filtered
 * bimap or its views, only mappings that satisfy the filter will be removed from the underlying
 * bimap.
 *
 * <p>The returned bimap isn't threadsafe or serializable, even if {@code unfiltered} is.
 *
 * <p>Many of the filtered bimap's methods, such as {@code size()}, iterate across every key in
 * the underlying bimap and determine which satisfy the filter. When a live view is <i>not</i>
 * needed, it may be faster to copy the filtered bimap and use the copy.
 *
 * <p><b>Warning:</b> {@code entryPredicate} must be <i>consistent with equals </i>, as documented
 * at {@link Predicate#apply}.
 *
 * @since 14.0
 */
public static <K, V> BiMap<K, V> filterKeys(
  BiMap<K, V> unfiltered, final Predicate<? super K> keyPredicate) {
 checkNotNull(keyPredicate);
 return filterEntries(unfiltered, Maps.<K>keyPredicateOnEntries(keyPredicate));
}

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

/**
 * Returns a sorted map containing the mappings in {@code unfiltered} whose values satisfy a
 * predicate. The returned map is a live view of {@code unfiltered}; changes to one affect the
 * other.
 *
 * <p>The resulting map's {@code keySet()}, {@code entrySet()}, and {@code values()} views have
 * iterators that don't support {@code remove()}, but all other methods are supported by the map
 * and its views. When given a value that doesn't satisfy the predicate, the map's {@code put()},
 * {@code putAll()}, and {@link Entry#setValue} methods throw an {@link IllegalArgumentException}.
 *
 * <p>When methods such as {@code removeAll()} and {@code clear()} are called on the filtered map
 * or its views, only mappings whose values satisfy the filter will be removed from the underlying
 * map.
 *
 * <p>The returned map isn't threadsafe or serializable, even if {@code unfiltered} is.
 *
 * <p>Many of the filtered map's methods, such as {@code size()}, iterate across every key/value
 * mapping in the underlying map and determine which satisfy the filter. When a live view is
 * <i>not</i> needed, it may be faster to copy the filtered map and use the copy.
 *
 * <p><b>Warning:</b> {@code valuePredicate} must be <i>consistent with equals</i>, as documented
 * at {@link Predicate#apply}. Do not provide a predicate such as {@code
 * Predicates.instanceOf(ArrayList.class)}, which is inconsistent with equals.
 *
 * @since 11.0
 */
public static <K, V> SortedMap<K, V> filterValues(
  SortedMap<K, V> unfiltered, final Predicate<? super V> valuePredicate) {
 return filterEntries(unfiltered, Maps.<V>valuePredicateOnEntries(valuePredicate));
}

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

@Override
 protected Map<String, String> create(Entry<String, String>[] entries) {
  Map<String, String> map = Maps.newHashMap();
  putEntries(map, entries);
  map.putAll(ENTRIES_TO_FILTER);
  map = Maps.filterEntries(map, FILTER_ENTRIES_1);
  return Maps.filterEntries(map, FILTER_ENTRIES_2);
 }
})

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

@GET
@Path("/properties")
@ResourceFilters(ConfigResourceFilter.class)
@Produces(MediaType.APPLICATION_JSON)
public Map<String, String> getProperties()
{
 Map<String, String> allProperties = Maps.fromProperties(properties);
 Set<String> hidderProperties = druidServerConfig.getHiddenProperties();
 return Maps.filterEntries(allProperties, (entry) -> !hidderProperties.contains(entry.getKey()));
}

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

@Override
 protected Map<String, String> create(Entry<String, String>[] entries) {
  Map<String, String> map = Maps.newHashMap();
  putEntries(map, entries);
  map.putAll(ENTRIES_TO_FILTER);
  return Maps.filterEntries(map, FILTER_ENTRIES);
 }
})

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

@Override
 protected NavigableMap<String, String> create(Entry<String, String>[] entries) {
  NavigableMap<String, String> map = new SafeTreeMap<>();
  putEntries(map, entries);
  map.put("banana", "toast");
  map.put("eggplant", "spam");
  return Maps.filterEntries(map, FILTER_ENTRIES);
 }
})

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

public void testFirstAndLastKeyFilteredMap() {
 SortedMap<String, Integer> unfiltered = createUnfiltered();
 unfiltered.put("apple", 2);
 unfiltered.put("banana", 6);
 unfiltered.put("cat", 3);
 unfiltered.put("dog", 5);
 SortedMap<String, Integer> filtered = Maps.filterEntries(unfiltered, CORRECT_LENGTH);
 assertEquals("banana", filtered.firstKey());
 assertEquals("cat", filtered.lastKey());
}

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

public void testFilteredEntriesWildCardEntryPredicate() {
  Map<String, Integer> unfiltered = createUnfiltered();
  unfiltered.put("cat", 3);
  unfiltered.put("dog", 2);
  unfiltered.put("horse", 5);
  Predicate<Entry<?, ?>> predicate =
    new Predicate<Entry<?, ?>>() {
     @Override
     public boolean apply(Entry<?, ?> input) {
      return "cat".equals(input.getKey()) || Integer.valueOf(2) == input.getValue();
     }
    };
  Map<String, Integer> filtered = Maps.filterEntries(unfiltered, predicate);
  assertEquals(ImmutableMap.of("cat", 3, "dog", 2), filtered);
 }
}

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

public void testFilteredEntriesObjectPredicate() {
 Map<String, Integer> unfiltered = createUnfiltered();
 unfiltered.put("cat", 3);
 unfiltered.put("dog", 2);
 unfiltered.put("horse", 5);
 Predicate<Object> predicate = Predicates.alwaysFalse();
 Map<String, Integer> filtered = Maps.filterEntries(unfiltered, predicate);
 assertTrue(filtered.isEmpty());
}

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

@Override
 protected BiMap<String, String> create(Entry<String, String>[] entries) {
  BiMap<String, String> map = HashBiMap.create();
  putEntries(map, entries);
  map.putAll(ENTRIES_TO_FILTER);
  return Maps.filterEntries(map, FILTER_ENTRIES);
 }
})

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

@Override
 protected SortedMap<String, String> create(Entry<String, String>[] entries) {
  SortedMap<String, String> map = new NonNavigableSortedMap();
  putEntries(map, entries);
  map.putAll(ENTRIES_TO_FILTER);
  return Maps.filterEntries(map, FILTER_ENTRIES);
 }
})

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

public void testHeadSubTailMap_FilteredMap() {
  SortedMap<String, Integer> unfiltered = createUnfiltered();
  unfiltered.put("apple", 2);
  unfiltered.put("banana", 6);
  unfiltered.put("cat", 4);
  unfiltered.put("dog", 3);
  SortedMap<String, Integer> filtered = Maps.filterEntries(unfiltered, CORRECT_LENGTH);
  assertEquals(ImmutableMap.of("banana", 6), filtered.headMap("dog"));
  assertEquals(ImmutableMap.of(), filtered.headMap("banana"));
  assertEquals(ImmutableMap.of("banana", 6, "dog", 3), filtered.headMap("emu"));
  assertEquals(ImmutableMap.of("banana", 6), filtered.subMap("banana", "dog"));
  assertEquals(ImmutableMap.of("dog", 3), filtered.subMap("cat", "emu"));
  assertEquals(ImmutableMap.of("dog", 3), filtered.tailMap("cat"));
  assertEquals(ImmutableMap.of("banana", 6, "dog", 3), filtered.tailMap("banana"));
 }
}

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

public void testFilteredEntriesIllegalPutAll() {
 Map<String, Integer> unfiltered = createUnfiltered();
 unfiltered.put("cat", 3);
 unfiltered.put("dog", 2);
 unfiltered.put("horse", 5);
 Map<String, Integer> filtered = Maps.filterEntries(unfiltered, CORRECT_LENGTH);
 assertEquals(ImmutableMap.of("cat", 3, "horse", 5), filtered);
 filtered.put("chicken", 7);
 assertEquals(ImmutableMap.of("cat", 3, "horse", 5, "chicken", 7), filtered);
 try {
  filtered.putAll(ImmutableMap.of("sheep", 5, "cow", 7));
  fail();
 } catch (IllegalArgumentException expected) {
 }
 assertEquals(ImmutableMap.of("cat", 3, "horse", 5, "chicken", 7), filtered);
}

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

public void testFilteredEntriesIllegalPut() {
 Map<String, Integer> unfiltered = createUnfiltered();
 unfiltered.put("cat", 3);
 unfiltered.put("dog", 2);
 unfiltered.put("horse", 5);
 Map<String, Integer> filtered = Maps.filterEntries(unfiltered, CORRECT_LENGTH);
 assertEquals(ImmutableMap.of("cat", 3, "horse", 5), filtered);
 filtered.put("chicken", 7);
 assertEquals(ImmutableMap.of("cat", 3, "horse", 5, "chicken", 7), filtered);
 try {
  filtered.put("cow", 7);
  fail();
 } catch (IllegalArgumentException expected) {
 }
 assertEquals(ImmutableMap.of("cat", 3, "horse", 5, "chicken", 7), filtered);
}

相关文章