Java中的不同遍历方式

已知一个Person类:

 1 public class Person implements Comparable<Person>{
 2     String name;
 3     String id;
 4     public Person(String name,String id) {
 5         this.name=name;
 6         this.id=id;
 7     }
 8     
 9     public String getName() {
10         return name;
11     }
12 
13     public void setName(String name) {
14         this.name = name;
15     }
16 
17     public String getId() {
18         return id;
19     }
20 
21     public void setId(String id) {
22         this.id = id;
23     }
24 
25     public boolean equals(Object o) {
26         if(!(o instanceof Person)) {
27             return false;
28         }
29         Person person=(Person) o;
30         return this.id.equals(person.id);
31     }
32     @Override
33     public int compareTo(Person o) {
34         if(o==null)
35             return 1;
36         return this.id.compareTo(o.id);
37     }    
38 
39     public String toString() {
40         return String.format("%s-%s", name,id);
41     }
42     
43     public int hasCode() {
44         return id.hashCode();
45     }
46 }

1、遍历HashMap的不同方法:

 1 import java.util.HashMap;
 2 import java.util.Map;
 3 import java.util.Map.Entry;
 4 import java.util.Set;
 5 
 6 import difficult.Person;
 7 public class HashMapDemo {
 8     public static void main(String[] args) {
 9         Map<String, Person> map=new HashMap<>();
10         Person p1=new Person("张三", "123");
11         map.put(p1.getId(), p1);
12         map.put("1002", new Person("1002", "李四"));
13         map.put("1003", new Person("1003", "王五"));
14         System.out.println(map);
15         
16         System.out.printf("%s-%s\n","1002",map.get("1002"));
17         //增强型for,元素类型为Entry<String,Person>
18         for(Entry<String, Person> entry:map.entrySet()) {
19             System.out.printf("%s-%s ",entry.getKey(),entry.getValue());
20         }
21         System.out.println();
22         //利用set来遍历
23         Set<String> keys=map.keySet();
24         for(String key:keys) {
25             Person value=map.get(key);
26             System.out.printf("%s-%s ",key,value);
27         }
28         System.out.println();
29     }
30 }

2、遍历list的不同方法

 1 import java.util.ArrayList;
 2 import java.util.Iterator;
 3 import java.util.List;
 4 
 5 import difficult.Person;
 6 
 7 public class IterateList {
 8 
 9     public static void main(String[] args) {
10         List<Person> persons = new ArrayList<>();//,List为一个接口,定义一个泛型为Person的ArrayList表
11         Person p = new Person("1001", "张三");
12         
13         persons.add(p);
14         persons.add(p);
15         persons.add(new Person("1002", "李四"));
16         persons.add(new Person("1003", "王五"));
17         persons.add(new Person("1004", "沈六"));
18         
19         //遍历1:下标
20         int len=persons.size();
21         for(int i=0;i<len;i++) {
22             Person person=persons.get(i);
23             System.out.print(person+"\t");
24         }
25         System.out.println();
26         //遍历2:迭代器
27         Iterator<Person> iterator=persons.iterator();
28         while(iterator.hasNext()) {
29             Person person=iterator.next();
30             System.out.print(person+"\t");
31         }
32         System.out.println();
33         
34         //遍历3:增强型for
35         for(Person person:persons) {
36             System.out.print(person+"\t");
37         }
38         System.out.println();
39         
40         int[] data= {1,3,5,7,9};
41         for(int dat:data) {
42             System.out.print(dat+"\t");
43         }
44     }
45 }

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

 1 import java.util.ArrayList;
 2 import java.util.Enumeration;
 3 import java.util.Iterator;
 4 import java.util.List;
 5 import java.util.Vector;
 6 
 7 import difficult.Person;
 8 
 9 public class VectorAndList {
10 
11     public static void main(String[] args) {
12         List<Person> persons = new ArrayList<>();
13         Person p = new Person("1001", "张三");
14         
15         persons.add(p);
16         persons.add(p);
17         persons.add(new Person("1002", "李四"));
18         persons.add(new Person("1003", "王五"));
19         persons.add(new Person("1004", "沈六"));
20         
21         // 遍历二:迭代器
22         Iterator<Person> iterator=persons.iterator();
23         while(iterator.hasNext()) {
24             Person person=iterator.next();
25             System.out.print(person+"\t");
26         }
27         System.out.println();
28         
29         //向量
30         Vector<Person> vector=new Vector<>();
31         vector.addAll(persons);
32         //通过枚举遍历
33         Enumeration<Person> enu=vector.elements();
34         while(enu.hasMoreElements()) {
35             Person person=enu.nextElement();
36             System.out.print(person+"\t");
37         }
38         System.out.println();
39         
40     }
41 }

 

posted @ 2019-01-02 20:03  里昂静  阅读(228)  评论(0编辑  收藏  举报