Java中使用迭代器来迭代LinkedHashMap的值。
让我们首先创建LinkedHashMap-
LinkedHashMap<String,String> l = new LinkedHashMap<String,String>();
将一些元素添加到LinkedHashMap-
l.put("1", "Jack"); l.put("2", "Tom"); l.put("3", "Jimmy"); l.put("4", "Morgan"); l.put("5", "Tim"); l.put("6", "Brad");
遍历值-
Collection res = l.values(); Iterator i = res.iterator(); while (i.hasNext()){ System.out.println(i.next()); }
以下是迭代LinkedHashMap的值的示例-
import java.util.*; public class Demo { public static void main(String[] args) { LinkedHashMap<String,String> l = new LinkedHashMap<String,String>(); l.put("1", "Jack"); l.put("2", "Tom"); l.put("3", "Jimmy"); l.put("4", "Morgan"); l.put("5", "Tim"); l.put("6", "Brad"); System.out.println("LinkedHashMap elements..."); Collection res = l.values(); Iterator i = res.iterator(); while (i.hasNext()) { System.out.println(i.next()); } } }
输出结果
LinkedHashMap elements... Jack Tom Jimmy Morgan Tim Brad