List、Set、Map遍历容器的方法

一、List(有序,可重复)
1.Foreach

public class TestCase {
  public class St(){
  public int id;
  public String name;
  St(int id, String name){this.id=id;this.name=name}
  }//构造函数
    public static void main(String[] args) {
        St st=new St(1,"Jack");
        St st1=new St(3,"Jack5");
        St st2=new St(2,"Jack7");
        ArrayList<St>list=new ArrayList();
        list.add(st);list.add(st1);list.add(st2);
        for (St s:list){
           System.out.println(s);
                 }
   }`

2.For循环

  public class TestCase {
  public class St(){
  public int id;
  public String name;
  St(int id, String name){this.id=id;this.name=name}
  }
    public static void main(String[] args) {
        St st=new St(1,"Jack");
        St st1=new St(3,"Jack5");
        St st2=new St(2,"Jack7");
        ArrayList<St>list=new ArrayList();
        list.add(st);list.add(st1);list.add(st2);
        for (int i=1;i<=list.size();i++){
            System.out.println(list.get(i));
        }
        }} 

3.迭代器

  public class TestCase {
   public class St(){
  public int id;
  public String name;
  St(int id, String name){this.id=id;this.name=name}`
`  }
    public static void main(String[] args) {
        St st=new St(1,"Jack");
        St st1=new St(3,"Jack5");
        St st2=new St(2,"Jack7");
        ArrayList<St>list=new ArrayList();
        list.add(st);list.add(st1);list.add(st2);
     for (ListIterator iterator=list.listIterator();iterator.hasNext();)
        {
            System.out.println(iterator.next());
        }
        for (ListIterator iterator1=list.listIterator();iterator1.hasPrevious();)
        {
            System.out.println(iterator1.Previous());                 //Listiterator可以对集合进行双向遍历
        }
    }
    }

二、Set(无序,不可重复)
1.迭代器

  public class TestCase {
   public class St(){
  public int id;
  public String name;
  St(int id, String name){this.id=id;this.name=name}`
 ` }
    public static void main(String[] args) {
        St st=new St(1,"Jack");
        St st1=new St(3,"Jack5");
        St st2=new St(2,"Jack7");
       Set<St>set=new Hashset()
               set.add(st);set.add(st1);set.add(st2);
     for (ListIterator iterator=list.listIterator();iterator.hasNext();)
        {
            System.out.println(iterator.next());
        }
}

2.Foreach

public class TestCase {
   public class St(){
  public int id;
  public String name;
  St(int id, String name){this.id=id;this.name=name}//构造函数`
 ` }
    public static void main(String[] args) {
        St st=new St(1,"Jack");
        St st1=new St(3,"Jack5");
        St st2=new St(2,"Jack7");
       Set<St>set=new Hashset()
               set.add(st);set.add(st1);set.add(st2);
    for(St s1:set){
    Object o=(Object)s1;
    System.out.println(o)
    }
    }}

三、Map(无序,重复覆盖)
迭代器
(1) 通过Set容器(keyset)

 public class TestCase {
 public static void main(String[] args) {
        Map<String,String>map=new Hashmap();
        map.put("1","a");
        map.put("2","b");
        map.put("3","c");
        Set<String>set=map.keyset();
        iterator it=set.iterator();
        while(it.hasNext()){
        String c=it.next();
        System.out.println(c+map.get(c));
        }
    }
    }}
(2)    通过Set容器(Entryset)
 public class TestCase {
 public static void main(String[] args) {
        Map<String,String>map=new Hashmap();
        map.put("1","a");
        map.put("2","b");
        map.put("3","c");
        Set<Map.Entry<String,String>>set=map.Entryset();
        iterator<Map.Entry<String,String> it=set.iterator();
        while(it.hasNext())
        {
       Object c=it.next();
        System.out.println(c);
        }
    }
    }

猜你喜欢

转载自blog.csdn.net/qq_41168200/article/details/82155140