java.util.Set.iterator()方法的使用及代码示例

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

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

Set.iterator介绍

[英]Returns an iterator on the elements of this set. The elements are unordered.
[中]返回此集合元素的迭代器。元素是无序的。

代码示例

代码示例来源: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

static <K, V> Entry<K, V> mapEntry(K key, V value) {
  return Collections.singletonMap(key, value).entrySet().iterator().next();
 }
}

代码示例来源:origin: square/okhttp

@Override public void deleteContents(File directory) throws IOException {
  String prefix = directory.toString() + "/";
  for (Iterator<File> i = files.keySet().iterator(); i.hasNext(); ) {
   File file = i.next();
   if (file.toString().startsWith(prefix)) i.remove();
  }
 }
}

代码示例来源:origin: iluwatar/java-design-patterns

/**
 * Checkout object from pool
 */
public synchronized T checkOut() {
 if (available.isEmpty()) {
  available.add(create());
 }
 T instance = available.iterator().next();
 available.remove(instance);
 inUse.add(instance);
 return instance;
}

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

@MapFeature.Require(SUPPORTS_REMOVE)
public void testClear() {
 getMap().clear();
 assertTrue("After clear(), a map should be empty.", getMap().isEmpty());
 assertEquals(0, getMap().size());
 assertFalse(getMap().entrySet().iterator().hasNext());
}

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

/**
 * A sensible definition of {@link #isEmpty} in terms of the {@code iterator} method of {@link
 * #entrySet}. If you override {@link #entrySet}, you may wish to override {@link #isEmpty} to
 * forward to this implementation.
 *
 * @since 7.0
 */
protected boolean standardIsEmpty() {
 return !entrySet().iterator().hasNext();
}

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

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

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

/**
 * Called after {@link #successorIterator} is exhausted. Advances {@link #node} to the next node
 * and updates {@link #successorIterator} to iterate through the successors of {@link #node}.
 */
protected final boolean advance() {
 checkState(!successorIterator.hasNext());
 if (!nodeIterator.hasNext()) {
  return false;
 }
 node = nodeIterator.next();
 successorIterator = graph.successors(node).iterator();
 return true;
}

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

public static <K, V> Entry<K, V> mapEntry(K key, V value) {
 return Collections.singletonMap(key, value).entrySet().iterator().next();
}

代码示例来源:origin: square/okhttp

@Override public void testRunFinished(Result result) throws Exception {
  Thread.setDefaultUncaughtExceptionHandler(oldDefaultUncaughtExceptionHandler);
  System.err.println("Uninstalled aggressive uncaught exception handler");

  synchronized (exceptions) {
   if (!exceptions.isEmpty()) {
    throw Throwables.rethrowAsException(exceptions.keySet().iterator().next());
   }
  }
 }
}

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

private static void assertEmpty(Set<? extends List<?>> set) {
 assertTrue(set.isEmpty());
 assertEquals(0, set.size());
 assertFalse(set.iterator().hasNext());
}

代码示例来源: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

@Override
public String toString() {
  Iterator<Map.Entry<String, Object>> entries = entrySet().iterator();
  StringBuilder sb = new StringBuilder("{");
  while (entries.hasNext()) {
    Map.Entry<String, Object> entry = entries.next();
    sb.append(entry.getKey());
    sb.append('=');
    sb.append(valueToString(entry.getValue()));
    sb.append(entries.hasNext() ? ", " : "");
  }
  sb.append("}");
  return sb.toString();
}

代码示例来源:origin: hankcs/HanLP

public String getMostLikelyLabel()
  {
    return labelMap.entrySet().iterator().next().getKey();
  }
}

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

static <K extends Enum<K>> Class<K> inferKeyType(Map<K, ?> map) {
 if (map instanceof EnumBiMap) {
  return ((EnumBiMap<K, ?>) map).keyType();
 }
 if (map instanceof EnumHashBiMap) {
  return ((EnumHashBiMap<K, ?>) map).keyType();
 }
 checkArgument(!map.isEmpty());
 return map.keySet().iterator().next().getDeclaringClass();
}

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

public void testEmptyRangeSubMultiset(SortedMultiset<E> multiset) {
 assertTrue(multiset.isEmpty());
 assertEquals(0, multiset.size());
 assertEquals(0, multiset.toArray().length);
 assertTrue(multiset.entrySet().isEmpty());
 assertFalse(multiset.iterator().hasNext());
 assertEquals(0, multiset.entrySet().size());
 assertEquals(0, multiset.entrySet().toArray().length);
 assertFalse(multiset.entrySet().iterator().hasNext());
}

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

@Override
public boolean remove(Object o) {
 Iterator<Entry<K, V>> entryItr = unfiltered.entrySet().iterator();
 while (entryItr.hasNext()) {
  Entry<K, V> entry = entryItr.next();
  if (predicate.apply(entry) && Objects.equal(entry.getValue(), o)) {
   entryItr.remove();
   return true;
  }
 }
 return false;
}

代码示例来源:origin: alibaba/druid

public void clear() {
  Iterator<Entry<PreparedStatementKey, PreparedStatementHolder>> iter = map.entrySet().iterator();
  while (iter.hasNext()) {
    Entry<PreparedStatementKey, PreparedStatementHolder> entry = iter.next();
    closeRemovedStatement(entry.getValue());
    iter.remove();
  }
}

代码示例来源: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: google/guava

@MapFeature.Require({FAILS_FAST_ON_CONCURRENT_MODIFICATION, SUPPORTS_REMOVE})
@CollectionSize.Require(SEVERAL)
public void testRemovePresentConcurrentWithKeySetIteration() {
 try {
  Iterator<K> iterator = getMap().keySet().iterator();
  getMap().remove(k0());
  iterator.next();
  fail("Expected ConcurrentModificationException");
 } catch (ConcurrentModificationException expected) {
  // success
 }
}

相关文章