List、Set、Map、Queue、Deque、Stack的遍历方法总结

文章目录


一、遍历List

1,使用get(int)方法遍历,由于List的行为和数组几乎完全相同:List内部存放元素是按照元素的先后顺序进行顺序存放的,每个元素都可以通过索引确定自己的位置,List的索引和数组一样,从0开始

public class Main {
    public static void main(String[] args) {
        List<String> list =  Arrays.asList("杭州", "北京", "上海", "南京");
        for (int i=0; i<list.size(); i++) {
            String s = list.get(i);
            System.out.println(s);
        }
    }
}

2、使用迭代器iterator()遍历,Iterator本身是一个对象,但它是由List的实例调用iterator()方法的时候创建的。Iterator对象知道如何遍历一个List,并且不同的List类型,返回的Iterator对象也是不同的,但总是有最高的访问效率。

boolean hasnext() 判断是否由下一个元素     E next()返回下一个元素

public class Main {
    public static void main(String[] args) {
        List<String> list = Arrays.asList("杭州", "北京", "上海", "南京");
        for (Iterator<String> it = list.iterator(); it.hasNext(); ) {
            String s = it.next();
            System.out.println(s);
        }
    }
}

二、遍历Set

  Set是Collection下面的无序子接口

  特点:无序、没有索引、不能重复

1、使用toArray(),转换成一个数组,对数组进行遍历;

public class Main {
    public static void main(String[] args) {
        Set<String> set = new HashSet<String>();
        Object[ ] objects = set.toArray();
        for(int i = 0; i < objects.length; i++){
              System.out.println(objects[i]+"");
        }
  }
}

2、使用迭代器iterator()遍历

public class Main {
    public static void main(String[] args) {
        Set<String> set = new HashSet<String>();
          for (Iterator<String> it = set.iterator(); it.hasNext(); ) {
               String s = it.next();
            System.out.println(s);
        }
  }
}

3、使用foeach循环进行遍历

public class Main {
    public static void main(String[] args) {
        Set<String> set = new HashSet<String>();
        for (String s : set){
            System.out.println(s);
        }
   }
}

三、遍历Map

1、使用foreach循环遍历Map实例的keySet()方法返回的Set集合

public class Main {
    public static void main(String[] args) {
        Map<String,Integer> map = new HashMap<>();
        for (String key : map.keySet()){
                Integer value = map.get(key);
                System.out.println(key + "=" + value);
        }
   }
}

2、遍历使用foreach循环遍历Map对象的entrySet()集合,它包含每一个key-value的映射

public class Main {
    public static void main(String[] args) {
        Map<String,Integer> map= new HashMap<>();
        for(Map.Entry<String,Integer> entry : map.entrySet())){
                      String key = entry.getKey();
                      Integer value = entry.getValue();
                      System.out.println(key + "=" + value);
        }
   }
}

四、遍历Queue

1、使用foreach循环遍历queue

public class Main {
    public static void main(String[] args) {
        Queue<String> queue = new LinkedList<String>();
        for(String temp:queue){
           System.out.println(temp);
        }
   }
}

2、使用迭代器iterator()遍历


public class Main {
    public static void main(String[] args) {
        Queue<String> queue = new LinkedList<String>();
        Iterator it = queue.iterator();
        while(it.hasnext()){
           System.out.println(it.next);
        }
   }
}

3、使用队列Queue的自带方法

boolean isEmpty() 判断队列中元素是否为空

E poll() 取出队首元素并删除


public class Main {
    public static void main(String[] args) {
        Queue<String> queue = new LinkedList<String>();
        while(!queue.isEmpty()){
             System.out.println(queue.poll());
        }
   }
}

五、遍历Deque

1、foreach循环遍历

public class Main {
    public static void main(String[] args) {
        Deque<String> deque = new LinkedList<String>();
        for (String string : deque) {
			System.out.println(string);
		}
   }
}

2、使用迭代器遍历

public class Main {
    public static void main(String[] args) {
        Deque<String> deque = new LinkedList<String>();
        Iterator<String> it = deque.iterator();
		while(it.hasNext()) {
			System.out.println(it.next());
		}
   }
}

六、遍历stack

1、while循环遍历

​
public class Main {
    public static void main(String[] args) {
        Stack<String> stack = new Stack<String>();
        while(stack.isEmpty()) {
			System.out.println(stack.pop());
		}
   }
}

​

2、foreach循环遍历

​
public class Main {
    public static void main(String[] args) {
        Stack<String> stack = new Stack<String>();
        Iterator<String> it = stack.iterator();
		while(it.hasNext()) {
			System.out.println(it.next());
		}
   }
}

​

  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值