现在的位置: 首页 > 综合 > 正文

Java 集合 Stack、Queue、Map插入、移除和遍历

2014年02月04日 ⁄ 综合 ⁄ 共 2901字 ⁄ 字号 评论关闭

1、Stack 操作

Java代码  收藏代码
  1. // Stack 类表示后进先出(LIFO)的对象堆栈  
  2. Stack<String> stack = new Stack<String>();  
  3. // 把数据项压入堆栈顶部  
  4. stack.push("a");  
  5. stack.push("b");  
  6. stack.push("c");  
  7.   
  8. // peek() 查看堆栈顶部的对象,但不从堆栈中移除它  
  9. System.out.println(stack.peek());  
  10.   
  11. System.out.println("-----------------------------");  
  12.   
  13. // 集合方式遍历,元素不会被移除  
  14. for (String s : stack) {  
  15.     System.out.println(s);  
  16. }  
  17.   
  18. System.out.println("-----------------------------");  
  19.   
  20. // 栈弹出遍历方式,元素会被移除  
  21. // empty() 测试堆栈是否为空  
  22. while (!stack.empty()) {  
  23.     // pop() 移除堆栈顶部的对象,并作为此函数的值返回该对象  
  24.     System.out.println(stack.pop());  
  25. }  

    结果:

Java代码  收藏代码
  1. c  
  2. -----------------------------  
  3. a  
  4. b  
  5. c  
  6. -----------------------------  
  7. c  
  8. b  
  9. a  

 

2、Queue 操作

 

Java代码  收藏代码
  1. Queue<String> queue = new LinkedBlockingDeque<String>();  
  2. // 将指定的元素插入此队列(如果立即可行且不会违反容量限制),  
  3. // 当使用有容量限制的队列时,此方法通常要优于 add(E),  
  4. // 后者可能无法插入元素,而只是抛出一个异常。  
  5. queue.offer("a");  
  6. queue.offer("b");  
  7. queue.offer("c");  
  8.   
  9. // 获取但不移除此队列的头;如果此队列为空,则返回 null  
  10. System.out.println(queue.peek());  
  11.   
  12. System.out.println("----------------------");  
  13.   
  14. // 集合方式遍历,元素不会被移除  
  15. for (String s : queue) {  
  16.     System.out.println(s);  
  17. }  
  18.   
  19. System.out.println("----------------------");  
  20.   
  21. // 队列方式遍历,元素逐个被移除  
  22. while (!queue.isEmpty()) {  
  23.     // 获取并移除此队列的头,如果此队列为空,则返回 null  
  24.     System.out.println(queue.poll());  
  25. }  

 结果:

Java代码  收藏代码
  1. a  
  2. ----------------------  
  3. a  
  4. b  
  5. c  
  6. ----------------------  
  7. a  
  8. b  
  9. c  

 

 3、Map 操作

 

Java代码  收藏代码
  1. // 将键映射到值的对象。一个映射不能包含重复的键;每个键最多只能映射到一个值。  
  2. Map<Integer, String> map = new HashMap<Integer, String>();  
  3. map.put(1"a");  
  4. map.put(2"b");  
  5. map.put(3"c");  
  6.   
  7. // 获取键对应的值  
  8. System.out.println("获取map的键为2的值 : " + map.get(2));  
  9.   
  10. System.out.println("-----------------------");  
  11.   
  12. // Entry方式遍历map,简洁  
  13. for (Map.Entry<Integer, String> entry : map.entrySet()) {  
  14.     System.out.println("Key : " + entry.getKey() + ", Value : " + entry.getValue());  
  15. }  
  16.   
  17. System.out.println("------------------------");  
  18.   
  19. // 这种遍历方式有点繁琐  
  20. Iterator<Entry<Integer, String>> it = map.entrySet().iterator();  
  21. while (it.hasNext()) {  
  22.     Entry<Integer, String> entry = it.next();  
  23.     System.out.println("Key : " + entry.getKey() + ", Value : " + entry.getValue());  
  24. }  
  25.   
  26. System.out.println("------------------------");  
  27.   
  28. // Key遍历  
  29. Iterator<Integer> itKey = map.keySet().iterator();  
  30. while (itKey.hasNext()) {  
  31.      System.out.println(itKey.next());  
  32. }  
  33.   
  34. System.out.println("------------------------");  
  35.   
  36. // Value遍历  
  37. Iterator<String> itValue = map.values().iterator();  
  38. while (itValue.hasNext()) {  
  39.      System.out.println(itValue.next());  
  40. }  
  41.   
  42. System.out.println("------------------------");  
  43.   
  44. // map 移除元素  
  45. // 如果存在一个键的映射关系,则将其从此映射中移除  
  46. System.out.println(map.remove(2));  

 结果:

Java代码  收藏代码
  1. 获取map的键为2的值 : b  
  2. -----------------------  
  3. Key : 1, Value : a  
  4. Key : 2, Value : b  
  5. Key : 3, Value : c  
  6. ------------------------  
  7. Key : 1, Value : a  
  8. Key : 2, Value : b  
  9. Key : 3, Value : c  
  10. ------------------------  
  11. 1  
  12. 2  
  13. 3  
  14. ------------------------  
  15. a  
  16. b  
  17. c  
  18. ------------------------  
  19. b  

抱歉!评论已关闭.