java.util.Map.entrySet()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(9.0k)|赞(0)|评价(0)|浏览(183)

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

Map.entrySet介绍

[英]Returns a Set view of the mappings contained in this map. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. If the map is modified while an iteration over the set is in progress (except through the iterator's own remove operation, or through the setValue operation on a map entry returned by the iterator) the results of the iteration are undefined. The set supports element removal, which removes the corresponding mapping from the map, via the Iterator.remove, Set.remove, removeAll, retainAll and clear operations. It does not support the add or addAll operations.
[中]返回此映射中包含的映射的集合视图。集合由映射支持,因此对映射的更改将反映在集合中,反之亦然。如果在对集合进行迭代时修改映射(除了通过迭代器自己的删除操作,或者通过迭代器返回的映射项上的setValue操作),则迭代的结果是未定义的。该集合支持元素移除,即通过迭代器从映射中移除相应的映射。移除,设置。移除、移除所有、保留和清除操作。它不支持添加或添加所有操作。

代码示例

代码示例来源:origin: stackoverflow.com

public static void printMap(Map mp) {
  Iterator it = mp.entrySet().iterator();
  while (it.hasNext()) {
    Map.Entry pair = (Map.Entry)it.next();
    System.out.println(pair.getKey() + " = " + pair.getValue());
    it.remove(); // avoids a ConcurrentModificationException
  }
}

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

/** An implementation of {@link Map#putAll}. */
static <K, V> void putAllImpl(Map<K, V> self, Map<? extends K, ? extends V> map) {
 for (Entry<? extends K, ? extends V> entry : map.entrySet()) {
  self.put(entry.getKey(), entry.getValue());
 }
}

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

/**
 * Stores the contents of a map in an output stream, as part of serialization. It does not support
 * concurrent maps whose content may change while the method is running.
 *
 * <p>The serialized output consists of the number of entries, first key, first value, second key,
 * second value, and so on.
 */
static <K, V> void writeMap(Map<K, V> map, ObjectOutputStream stream) throws IOException {
 stream.writeInt(map.size());
 for (Map.Entry<K, V> entry : map.entrySet()) {
  stream.writeObject(entry.getKey());
  stream.writeObject(entry.getValue());
 }
}

代码示例来源:origin: apache/kafka

private void innerUpdateEndOffsets(final Map<TopicPartition, Long> newOffsets,
                  final boolean replace) {
  for (final Map.Entry<TopicPartition, Long> entry : newOffsets.entrySet()) {
    List<Long> offsets = endOffsets.get(entry.getKey());
    if (replace || offsets == null) {
      offsets = new ArrayList<>();
    }
    offsets.add(entry.getValue());
    endOffsets.put(entry.getKey(), offsets);
  }
}

代码示例来源:origin: prestodb/presto

public Map<String, Double> getOperatorHashCollisionsAverages()
{
  return operatorHashCollisionsStats.entrySet().stream()
      .collect(toMap(
          Map.Entry::getKey,
          entry -> entry.getValue().getWeightedHashCollisions() / operatorInputStats.get(entry.getKey()).getInputPositions()));
}

代码示例来源:origin: bumptech/glide

private Map<String, List<LazyHeaderFactory>> copyHeaders() {
 Map<String, List<LazyHeaderFactory>> result = new HashMap<>(headers.size());
 for (Map.Entry<String, List<LazyHeaderFactory>> entry : headers.entrySet()) {
  @SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
  List<LazyHeaderFactory> valueCopy = new ArrayList<>(entry.getValue());
  result.put(entry.getKey(), valueCopy);
 }
 return result;
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Match the provided column names and values with the list of columns used.
 * @param inParameters the parameter names and values
 */
public List<Object> matchInParameterValuesWithInsertColumns(Map<String, ?> inParameters) {
  List<Object> values = new ArrayList<>(inParameters.size());
  for (String column : this.tableColumns) {
    Object value = inParameters.get(column);
    if (value == null) {
      value = inParameters.get(column.toLowerCase());
      if (value == null) {
        for (Map.Entry<String, ?> entry : inParameters.entrySet()) {
          if (column.equalsIgnoreCase(entry.getKey())) {
            value = entry.getValue();
            break;
          }
        }
      }
    }
    values.add(value);
  }
  return values;
}

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

@CanIgnoreReturnValue
private Map<R, V> removeColumn(Object column) {
 Map<R, V> output = new LinkedHashMap<>();
 Iterator<Entry<R, Map<C, V>>> iterator = backingMap.entrySet().iterator();
 while (iterator.hasNext()) {
  Entry<R, Map<C, V>> entry = iterator.next();
  V value = entry.getValue().remove(column);
  if (value != null) {
   output.put(entry.getKey(), value);
   if (entry.getValue().isEmpty()) {
    iterator.remove();
   }
  }
 }
 return output;
}

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

public void testEntrySetSetValueSameValue() {
 // TODO: Investigate the extent to which, in practice, maps that support
 // put() also support Entry.setValue().
 if (!supportsPut) {
  return;
 }
 final Map<K, V> map;
 try {
  map = makePopulatedMap();
 } catch (UnsupportedOperationException e) {
  return;
 }
 Set<Entry<K, V>> entrySet = map.entrySet();
 Entry<K, V> entry = entrySet.iterator().next();
 final V oldValue = entry.getValue();
 final V returnedValue = entry.setValue(oldValue);
 assertEquals(oldValue, returnedValue);
 assertTrue(entrySet.contains(mapEntry(entry.getKey(), oldValue)));
 assertEquals(oldValue, map.get(entry.getKey()));
 assertInvariants(map);
}

代码示例来源:origin: apache/flink

@Test
public void testGetEnvironmentVariables() {
  Configuration testConf = new Configuration();
  testConf.setString("yarn.application-master.env.LD_LIBRARY_PATH", "/usr/lib/native");
  Map<String, String> res = Utils.getEnvironmentVariables("yarn.application-master.env.", testConf);
  Assert.assertEquals(1, res.size());
  Map.Entry<String, String> entry = res.entrySet().iterator().next();
  Assert.assertEquals("LD_LIBRARY_PATH", entry.getKey());
  Assert.assertEquals("/usr/lib/native", entry.getValue());
}

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

@Override
public void putAll(Map<? extends K, ? extends V> m) {
 for (Entry<? extends K, ? extends V> e : m.entrySet()) {
  put(e.getKey(), e.getValue());
 }
}

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

static <T extends StandardTable<Object, Object, Object>> T populate(
  SerializationStreamReader reader, T table) throws SerializationException {
 Map<?, ?> hashMap = (Map<?, ?>) reader.readObject();
 for (Entry<?, ?> row : hashMap.entrySet()) {
  table.row(row.getKey()).putAll((Map<?, ?>) row.getValue());
 }
 return table;
}

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

/**
 * put all.
 *
 * @param map map.
 */
public void putAll(Map<String, Object> map) {
  for (Map.Entry<String, Object> entry : map.entrySet()) {
    mMap.put(entry.getKey(), entry.getValue());
  }
}

代码示例来源:origin: prestodb/presto

@Override
public BenchmarkResultHook addResults(Map<String, Long> results)
{
  requireNonNull(results, "results is null");
  for (Entry<String, Long> entry : results.entrySet()) {
    Long currentSum = resultsSum.get(entry.getKey());
    if (currentSum == null) {
      currentSum = 0L;
    }
    resultsSum.put(entry.getKey(), currentSum + entry.getValue());
  }
  resultsCount++;
  return this;
}

代码示例来源:origin: prestodb/presto

public Map<String, Double> getOperatorExpectedCollisionsAverages()
{
  return operatorHashCollisionsStats.entrySet().stream()
      .collect(toMap(
          Map.Entry::getKey,
          entry -> entry.getValue().getWeightedExpectedHashCollisions() / operatorInputStats.get(entry.getKey()).getInputPositions()));
}

代码示例来源:origin: stackoverflow.com

Map map = new HashMap();
Iterator entries = map.entrySet().iterator();
while (entries.hasNext()) {
  Map.Entry entry = (Map.Entry) entries.next();
  Integer key = (Integer)entry.getKey();
  Integer value = (Integer)entry.getValue();
  System.out.println("Key = " + key + ", Value = " + value);
}

代码示例来源:origin: spring-projects/spring-framework

private Map<String, Class<?>> toClassMap(Map<String, ?> map) throws ClassNotFoundException {
  Map<String, Class<?>> result = new LinkedHashMap<>(map.size());
  for (Map.Entry<String, ?> entry : map.entrySet()) {
    String key = entry.getKey();
    Object value = entry.getValue();
    Class<?> type;
    if (value instanceof Class) {
      type = (Class<?>) value;
    }
    else if (value instanceof String) {
      String className = (String) value;
      type = ClassUtils.forName(className, this.beanClassLoader);
    }
    else {
      throw new IllegalArgumentException("Unknown value [" + value + "] - expected String or Class");
    }
    result.put(key, type);
  }
  return result;
}

代码示例来源:origin: jenkinsci/jenkins

private PackedMap(Map<? extends K,? extends V> src) {
  kvpairs = new Object[src.size()*2];
  int i=0;
  for (Entry<? extends K, ? extends V> e : src.entrySet()) {
    kvpairs[i++] = e.getKey();
    kvpairs[i++] = e.getValue();
  }
}

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

public static boolean mapEquals(Map<?, ?> map1, Map<?, ?> map2) {
  if (map1 == null && map2 == null) {
    return true;
  }
  if (map1 == null || map2 == null) {
    return false;
  }
  if (map1.size() != map2.size()) {
    return false;
  }
  for (Map.Entry<?, ?> entry : map1.entrySet()) {
    Object key = entry.getKey();
    Object value1 = entry.getValue();
    Object value2 = map2.get(key);
    if (!objectEquals(value1, value2)) {
      return false;
    }
  }
  return true;
}

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

public void testFilteredValuesIllegalSetValue() {
 Map<String, Integer> unfiltered = createUnfiltered();
 Map<String, Integer> filtered = Maps.filterValues(unfiltered, EVEN);
 filtered.put("a", 2);
 filtered.put("b", 4);
 assertEquals(ImmutableMap.of("a", 2, "b", 4), filtered);
 Entry<String, Integer> entry = filtered.entrySet().iterator().next();
 try {
  entry.setValue(5);
  fail();
 } catch (IllegalArgumentException expected) {
 }
 assertEquals(ImmutableMap.of("a", 2, "b", 4), filtered);
}

相关文章