首页 > 编程语言 > 详细

Java中List Set Map集合的遍历

时间:2018-11-26 22:43:06      阅读:185      评论:0      收藏:0      [点我收藏+]

创建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()+"\t"+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()+"\t"+is.next().getAge());
   System.out.println(s.getName()+"\t"+s.getAge());
  }

 

(4)上面3种均可以不适用泛型来遍历,只是需要强转,不仅如此,后面的Set集合,Map集合同理

Iterator i=l.iterator()
  while(i.hasNext()){
  Student s=(Student)i.next();//强转
  System.out.println(s.getName()+"\t"+s.getAge());
}

 

(5)不常用

for(Iterator i=l.iterator();i.hasNext();){
  Student s=(Student)i.next();
  System.out.println(s.getName()+"\t"+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()+"\t"+stu.getAge());
 }

 

(2)适用foreach遍历

for (Student stu : s) {
  System.out.println(stu.getName()+"\t"+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()+"\t"+stu.getValue().getName());
  }

 

(2)先拿到key值再拿取对应的value值

Set<String> set=ms.keySet();
  for (String str : set) {
  System.out.println(str+"\t"+ms.get(str).getName());
  }

 

(3)对于数据量大的Map集合遍历

for(Entry<String, Student>entry:ms.entrySet()){
   System.out.println(entry.getKey()+"\t"+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+"\t"+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()+"\t"+entry.getValue().getName());
}

}

 

Java中List Set Map集合的遍历

原文:https://www.cnblogs.com/xiaohu666/p/10023340.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!