Java中List Set Map集合的遍历
Posted 小胡666
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java中List Set Map集合的遍历相关的知识,希望对你有一定的参考价值。
创建Student实体
package com.smic.test; public class Student { private String name = null; private Integer age = null; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Student() { super(); // TODO Auto-generated constructor stub } public Student(String name, Integer age) { super(); this.name = name; this.age = age; } }
一、List集合的遍历
Student s1=new Student("卫庄", 32); Student s2=new Student("盖聂", 35); Student s3=new Student("黑白玄翦", 36); List<Student> l=new ArrayList<Student>(); l.add(s1); l.add(s2); l.add(s3);
(1)for循环遍历
for (int i = 0; i < l.size(); i++) { System.out.println(l.get(i).getName()); }
(2)foreach循环遍历
for (Student stu : l) { System.out.println(stu.getName()+" "+stu.getAge()); }
(3)Iterator迭代器遍历(List,Set,Map都通用)
Iterator<Student> is=l.iterator(); while(is.hasNext()){ Student s=is.next(); //错误书写,只能用一次is.next()迭代器 // System.out.println(is.next().getName()+" "+is.next().getAge()); System.out.println(s.getName()+" "+s.getAge()); }
(4)上面3种均可以不适用泛型来遍历,只是需要强转,不仅如此,后面的Set集合,Map集合同理
Iterator i=l.iterator() while(i.hasNext()){ Student s=(Student)i.next();//强转 System.out.println(s.getName()+" "+s.getAge()); }
(5)不常用
for(Iterator i=l.iterator();i.hasNext();){ Student s=(Student)i.next(); System.out.println(s.getName()+" "+s.getAge()); }
二、Set集合
Student s1=new Student("卫庄", 32); Student s2=new Student("盖聂", 35); Student s3=new Student("黑白玄翦", 36); Set<Student> s=new HashSet<Student>(); s.add(s1); s.add(s2); s.add(s3);
(1)使用Iterator泛型来遍历
Iterator<Student> is=s.iterator(); while(is.hasNext()){ Student stu=is.next(); System.out.println(stu.getName()+" "+stu.getAge()); }
(2)适用foreach遍历
for (Student stu : s) { System.out.println(stu.getName()+" "+stu.getAge()); }
三、Map集合
Student s1=new Student("卫庄", 32); Student s2=new Student("盖聂", 35); Student s3=new Student("黑白玄翦", 36); Map<String,Student> ms=new HashMap<String,Student>(); ms.put("stu1", s1); ms.put("stu2", s2); ms.put("stu3", s3);
(1)Iterator遍历 key值要不一样 不然拿到一个Student
Iterator<Map.Entry<String, Student>> it=ms.entrySet().iterator();//Map.可以不写 while(it.hasNext()){ Map.Entry<String, Student> stu=it.next(); System.out.println(stu.getKey()+" "+stu.getValue().getName()); }
(2)先拿到key值再拿取对应的value值
Set<String> set=ms.keySet(); for (String str : set) { System.out.println(str+" "+ms.get(str).getName()); }
(3)对于数据量大的Map集合遍历
for(Entry<String, Student>entry:ms.entrySet()){ System.out.println(entry.getKey()+" "+entry.getValue().getName()); }
(4)只遍历Map的值
Collection<Student> col=ms.values(); for (Student stu : col) { System.out.println(stu.getName()); }
四、集合嵌套
(1)List<Map<String,Student>>
Student s1=new Student("卫庄", 32); Student s2=new Student("盖聂", 35); Student s3=new Student("黑白玄翦", 36); Map<String,Student> ms=new HashMap<String,Student>(); ms.put("stu1", s1); ms.put("stu2", s2); ms.put("stu3", s3); List<Map<String,Student>> list=new ArrayList<Map<String,Student>>(); list.add(ms);
------------------------------------------------------------------------------------------------------------------
for (int i = 0; i < list.size(); i++) { Map<String,Student> map=list.get(i); Set<String> set=map.keySet(); for (String str : set) { System.out.println(str+" "+map.get(str).getName()); } }
------------------------------------------------------或者等等-------------------------------------------------
for (int i = 0; i < list.size(); i++) { Map<String,Student> map=list.get(i); for(Entry<String, Student> entry:map.entrySet()){ System.out.println(entry.getKey()+" "+entry.getValue().getName()); } }
以上是关于Java中List Set Map集合的遍历的主要内容,如果未能解决你的问题,请参考以下文章
结果集存放在 List<Map<String,Object>>中怎么使用Java代码遍历获取String,Object的值?