Java中的不同遍历方式

时间:2022-03-19 14:31:58

已知一个Person类:

 public class Person implements Comparable<Person>{
String name;
String id;
public Person(String name,String id) {
this.name=name;
this.id=id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getId() {
return id;
} public void setId(String id) {
this.id = id;
} public boolean equals(Object o) {
if(!(o instanceof Person)) {
return false;
}
Person person=(Person) o;
return this.id.equals(person.id);
}
@Override
public int compareTo(Person o) {
if(o==null)
return ;
return this.id.compareTo(o.id);
} public String toString() {
return String.format("%s-%s", name,id);
} public int hasCode() {
return id.hashCode();
}
}

1、遍历HashMap的不同方法:

 import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set; import difficult.Person;
public class HashMapDemo {
public static void main(String[] args) {
Map<String, Person> map=new HashMap<>();
Person p1=new Person("张三", "");
map.put(p1.getId(), p1);
map.put("", new Person("", "李四"));
map.put("", new Person("", "王五"));
System.out.println(map); System.out.printf("%s-%s\n","",map.get(""));
//增强型for,元素类型为Entry<String,Person>
for(Entry<String, Person> entry:map.entrySet()) {
System.out.printf("%s-%s ",entry.getKey(),entry.getValue());
}
System.out.println();
//利用set来遍历
Set<String> keys=map.keySet();
for(String key:keys) {
Person value=map.get(key);
System.out.printf("%s-%s ",key,value);
}
System.out.println();
}
}

2、遍历list的不同方法

 import java.util.ArrayList;
import java.util.Iterator;
import java.util.List; import difficult.Person; public class IterateList { public static void main(String[] args) {
List<Person> persons = new ArrayList<>();//,List为一个接口,定义一个泛型为Person的ArrayList表
Person p = new Person("1001", "张三"); persons.add(p);
persons.add(p);
persons.add(new Person("1002", "李四"));
persons.add(new Person("1003", "王五"));
persons.add(new Person("1004", "沈六")); //遍历1:下标
int len=persons.size();
for(int i=0;i<len;i++) {
Person person=persons.get(i);
System.out.print(person+"\t");
}
System.out.println();
//遍历2:迭代器
Iterator<Person> iterator=persons.iterator();
while(iterator.hasNext()) {
Person person=iterator.next();
System.out.print(person+"\t");
}
System.out.println(); //遍历3:增强型for
for(Person person:persons) {
System.out.print(person+"\t");
}
System.out.println(); int[] data= {1,3,5,7,9};
for(int dat:data) {
System.out.print(dat+"\t");
}
}
}

3、利用向量(通过枚举)遍历list

 import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.Vector; import difficult.Person; public class VectorAndList { public static void main(String[] args) {
List<Person> persons = new ArrayList<>();
Person p = new Person("1001", "张三"); persons.add(p);
persons.add(p);
persons.add(new Person("1002", "李四"));
persons.add(new Person("1003", "王五"));
persons.add(new Person("1004", "沈六")); // 遍历二:迭代器
Iterator<Person> iterator=persons.iterator();
while(iterator.hasNext()) {
Person person=iterator.next();
System.out.print(person+"\t");
}
System.out.println(); //向量
Vector<Person> vector=new Vector<>();
vector.addAll(persons);
//通过枚举遍历
Enumeration<Person> enu=vector.elements();
while(enu.hasMoreElements()) {
Person person=enu.nextElement();
System.out.print(person+"\t");
}
System.out.println(); }
}