List,Set,Map,Queue,Deque,Stack的遍历方式

1.List(有序,允许重复)的遍历方式

// List的实现类
LinkedList<String> list1 = new LinkedList<String>(); // 基于双向链表的集合
ArrayList<String> list2 = new ArrayList<String>(); // 单列集合

方式一:for循环

方式二:for...each循环

      

方式三:迭代器遍历

2.Set(无序,不允许重复(实现类中treeSet可自动排序,LinkedHashSet有序))的遍历方式

// Set的实现类(Set接口不保证有序,SortedSet接口保证元素有序)
HashSet<String> set1 = new HashSet<String>(); // 无序,不允许重复
LinkedHashSet<String> set2 = new LinkedHashSet<String>(); // 有序,不允许重复
TreeSet<String> set3 = new TreeSet<String>(); // 有序,不允许重复(实现SortedSet接口)	

方式一:for...each循环

方式二:iterator迭代器遍历

 3.Map(无序,唯一(实现类中LinkedHashMap有序))的遍历方式

// Map(键值对集合)(无序且唯一)的实现类
HashMap<String, Integer> map = new HashMap<String, Integer>(); 
HashMap<String, String> linkedMap = new LinkedHashMap<String, String>(); // 有序		
Map<String,String> treeMap = new TreeMap<String,String>(); // treeMap是Map的子类
		

方式一:for...each循环

方式二:遍历Map对象的entrySet()集合

 4.Queue(先进先出)的遍历方式

Queue<String> queue = new LinkedList<String>();
        queue.offer("yellow");
        queue.offer("white");
        queue.offer("pink");
        queue.offer("blue");

方式1:for...each循环

方式2:iterator迭代器遍历

 5.Deque(双端队列)的遍历方式

Deque<String> d = new LinkedList<String>();
        d.offer("blue");
        d.offer("yellow");
        d.offer("white");
        d.offer("pink");

方式1.for...each循环

方式2.while循环

 方式3.iterator迭代器遍历

 6.Stack(先进后出)的遍历方式

Stack<String> stack = new Stack<String>();    
        stack.push("A1");
        stack.push("A2");
        stack.push("A3");
        stack.push("A4");
        stack.push("A5");

方式1:遍历并出栈---->while循环(后进先出)

方式2:for...each循环(先进先出)

方式3:Iterator迭代器遍历(先进先出)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值