java.util.Iterator.hasNext()方法的使用及代码示例

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

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

Iterator.hasNext介绍

[英]Returns true if there is at least one more element, false otherwise.
[中]

代码示例

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

List<String> list = new ArrayList<>();

// This is a clever way to create the iterator and call iterator.hasNext() like
// you would do in a while-loop. It would be the same as doing:
//     Iterator<String> iterator = list.iterator();
//     while (iterator.hasNext()) {
for (Iterator<String> iterator = list.iterator(); iterator.hasNext();) {
  String string = iterator.next();
  if (string.isEmpty()) {
    // Remove the current element from the iterator and the list.
    iterator.remove();
  }
}

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

private void processPendingCommands() {
 Iterator<Runnable> iterator = pendingCommands.iterator();
 while (iterator.hasNext()) {
  Runnable command = iterator.next();
  command.run();
  iterator.remove();
 }
}

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

/**
  * Collects the remaining objects of the given iterator into a List.
  * 
  * @return a new List with the remaining objects.
  */
 public static <E> List<E> toList(Iterator<E> iterator) {
  List<E> copy = new ArrayList<>();
  while (iterator.hasNext()) {
   copy.add(iterator.next());
  }
  return copy;
 }
}

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

public void testAdvance_pastEnd() {
 List<String> list = newArrayList();
 list.add("a");
 list.add("b");
 Iterator<String> iterator = list.iterator();
 advance(iterator, 5);
 assertFalse(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: google/guava

public void testIteratorNoSuchElementException() {
  Iterator<E> iterator = collection.iterator();
  while (iterator.hasNext()) {
   iterator.next();
  }

  try {
   iterator.next();
   fail("iterator.next() should throw NoSuchElementException");
  } catch (NoSuchElementException expected) {
  }
 }
}

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

@Override
public String getPath() {
  StringBuilder pathBuilder = new StringBuilder();
  pathBuilder.append(PATH_DELIMITER);
  for (Iterator<String> iterator = this.pathSegments.iterator(); iterator.hasNext(); ) {
    String pathSegment = iterator.next();
    pathBuilder.append(pathSegment);
    if (iterator.hasNext()) {
      pathBuilder.append(PATH_DELIMITER);
    }
  }
  return pathBuilder.toString();
}

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

public void clean() {
  // the lock protects removal from a concurrent put which could otherwise mutate the
  // queue after it has been removed from the map
  synchronized (unsent) {
    Iterator<ConcurrentLinkedQueue<ClientRequest>> iterator = unsent.values().iterator();
    while (iterator.hasNext()) {
      ConcurrentLinkedQueue<ClientRequest> requests = iterator.next();
      if (requests.isEmpty())
        iterator.remove();
    }
  }
}

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

public synchronized void finishSplits(int splits)
{
  List<Map.Entry<PlanNodeId, Split>> toRemove = new ArrayList<>();
  Iterator<Map.Entry<PlanNodeId, Split>> iterator = this.splits.entries().iterator();
  while (toRemove.size() < splits && iterator.hasNext()) {
    toRemove.add(iterator.next());
  }
  for (Map.Entry<PlanNodeId, Split> entry : toRemove) {
    this.splits.remove(entry.getKey(), entry.getValue());
  }
  updateSplitQueueSpace();
}

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

/**
 * @return the count of remaining objects of the current Iterable
 */
public final int getRemainingElementsCount() {
 int counter = 0;
 Iterator<E> iterator = iterator();
 while (iterator.hasNext()) {
  iterator.next();
  counter++;
 }
 return counter;
}

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

public void testIteratorMinExhaustsIterator() {
 List<Integer> ints = Lists.newArrayList(9, 0, 3, 5);
 Iterator<Integer> iterator = ints.iterator();
 assertEquals(0, (int) numberOrdering.min(iterator));
 assertFalse(iterator.hasNext());
}

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

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

代码示例来源:origin: skylot/jadx

public static void remove(List<InsnNode> list, InsnNode insn) {
  for (Iterator<InsnNode> iterator = list.iterator(); iterator.hasNext(); ) {
    InsnNode next = iterator.next();
    if (next == insn) {
      iterator.remove();
      return;
    }
  }
}

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

/**
 * Splits {@code sequence} into string components and returns them as an immutable list. If you
 * want an {@link Iterable} which may be lazily evaluated, use {@link #split(CharSequence)}.
 *
 * @param sequence the sequence of characters to split
 * @return an immutable list of the segments split from the parameter
 * @since 15.0
 */
@Beta
public List<String> splitToList(CharSequence sequence) {
 checkNotNull(sequence);
 Iterator<String> iterator = splittingIterator(sequence);
 List<String> result = new ArrayList<>();
 while (iterator.hasNext()) {
  result.add(iterator.next());
 }
 return Collections.unmodifiableList(result);
}

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

public void testGet_basic() {
 List<String> list = newArrayList();
 list.add("a");
 list.add("b");
 Iterator<String> iterator = list.iterator();
 assertEquals("b", get(iterator, 1));
 assertFalse(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: spring-projects/spring-framework

/**
 * Return a string representation of the given list of {@code HttpRange} objects.
 * <p>This method can be used to for an {@code Range} header.
 * @param ranges the ranges to create a string of
 * @return the string representation
 */
public static String toString(Collection<HttpRange> ranges) {
  Assert.notEmpty(ranges, "Ranges Collection must not be empty");
  StringBuilder builder = new StringBuilder(BYTE_RANGE_PREFIX);
  for (Iterator<HttpRange> iterator = ranges.iterator(); iterator.hasNext(); ) {
    HttpRange range = iterator.next();
    builder.append(range);
    if (iterator.hasNext()) {
      builder.append(", ");
    }
  }
  return builder.toString();
}

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

public static <T> void printList(List<T> list) {
    Log.info("PrintList:");
    Iterator<T> it = list.iterator();
    while (it.hasNext()) {
      Log.info(it.next().toString());
    }

  }
}

相关文章