Java常用遍历栈、队列、Map

一、Map的遍历
 
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;


public class TestMap {
        public static void main(String[] args) {
                Map<String, String> map = new HashMap<String, String>();
                map.put("1""a");
                map.put("2""b");
                map.put("3""c");

                //最简洁、最通用的遍历方式
                for (Map.Entry<String, String> entry : map.entrySet()) {
                        System.out.println(entry.getKey() + " = " + entry.getValue());
                }
                //Java5之前的比较简洁的便利方式1
                System.out.println("----1----");
                for (Iterator<Map.Entry<String, String>> it = map.entrySet().iterator(); it.hasNext();) {
                        Map.Entry<String, String> entry = it.next();
                        System.out.println(entry.getKey() + " = " + entry.getValue());
                }
                //Java5之前的比较简洁的便利方式2
                System.out.println("----2----");
                for (Iterator<String> it = map.keySet().iterator(); it.hasNext();) {
                        String key = it.next();
                        System.out.println(key + " = " + map.get(key));
                }
        }
}
 
3 = c
2 = b
1 = a
----1----
3 = c
2 = b
1 = a
----2----
3 = c
2 = b
1 = a

Process finished with exit code 0
 
二、Queue的遍历
 
import java.util.Queue;
import java.util.concurrent.LinkedBlockingQueue;


public class TestQueue {
        public static void main(String[] args) {
                Queue<Integer> q = new LinkedBlockingQueue<Integer>();
                //初始化队列
                for (int i = 0; i < 5; i++) {
                        q.offer(i);
                }
                System.out.println("-------1-----");
                //集合方式遍历,元素不会被移除
                for (Integer x : q) {
                        System.out.println(x);
                }
                System.out.println("-------2-----");
                //队列方式遍历,元素逐个被移除
                while (q.peek() != null) {
                        System.out.println(q.poll());
                }
        }
}
 
-------1-----
0
1
2
3
4
-------2-----
0
1
2
3
4

Process finished with exit code 0
 
三、Stack的遍历
 
import java.util.Stack;


public class TestStack {
        public static void main(String[] args) {
                Stack<Integer> s = new Stack<Integer>();
                for (int i = 0; i < 10; i++) {
                        s.push(i);
                }
                //集合遍历方式
                for (Integer x : s) {
                        System.out.println(x);
                }
                System.out.println("------1-----");
                //栈弹出遍历方式
//                while (s.peek()!=null) {    //不健壮的判断方式,容易抛异常,正确写法是下面的
                while (!s.empty()) {
                        System.out.println(s.pop());
                }
                System.out.println("------2-----");
                //错误的遍历方式
//                for (Integer x : s) {
//                        System.out.println(s.pop());
//                }
        }
}
 
0
1
2
3
4
------1-----
4
3
2
1
0
------2-----

Process finished with exit code 0
  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
可以使用Redis的数据结构之一:List(列表)来实现这个功能。具体实现方法如下: 1. 初始化一个List,用来存放Map元素; 2. 每次添加元素时,先判断List的长度是否已达到容量,如达到,则从队尾出队一个元素; 3. 将新元素添加到队头; 4. 获取元素时,遍历List,查找对应的key。 下面是Java代码示例: ```java import redis.clients.jedis.Jedis; public class FixedSizeMap { private Jedis jedis; private String key; private int maxSize; public FixedSizeMap(String host, int port, String key, int maxSize) { jedis = new Jedis(host, port); this.key = key; this.maxSize = maxSize; } public void put(String field, String value) { if (jedis.llen(key) >= maxSize) { jedis.rpop(key); } jedis.lpush(key, field + ":" + value); } public String get(String field) { for (String item : jedis.lrange(key, 0, -1)) { String[] parts = item.split(":"); if (parts[0].equals(field)) { return parts[1]; } } return null; } } ``` 使用示例: ```java FixedSizeMap map = new FixedSizeMap("localhost", 6379, "myMap", 3); map.put("key1", "value1"); map.put("key2", "value2"); map.put("key3", "value3"); map.put("key4", "value4"); // key1已被挤出队列 String value2 = map.get("key2"); // 返回"value2" ``` 需要注意的是,List的遍历操作是线性的,如果List中元素数量非常多,这个过程可能会比较慢。如果需要快速查询元素,可以考虑使用Redis的另一种数据结构:Hash(哈希表)。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值