MMap之Map.Entry接口

本文详细介绍了Java中Map集合的两种遍历方法:通过Map.keySet遍历key和value,以及通过Map.entrySet使用iterator遍历key和value。文章强调了Map集合不能直接使用迭代器或者foreach进行遍历,但在转换为Set后即可实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Map是java中的一个接口,Map.Entey是Map的一个内部接口
Map.Entey是Map声明的一个内部接口,此接口为泛型,定义为Entey<K,V>。他表示Map中的一个实体(键值对)。接口中有getKey(),getValue()方法。

//第一种:
System.out.println(“通过Map.keySet遍历key和value:”);
Set keySet = a.keySet();
Iterator it = keySet.iterator();

while (it.hasNext()){
String key = it.next();//迭代器中的元素都是key
String value = a.get(key);//通过get()方法找到key对应的value
System.out.println(value);
}

//第二种 效率相对第一种更高
System.out.println(“通过Map.entrySet使用iterator遍历key和value:”);
Set<Map.Entry<String,String>> entrySet = a.entrySet();
Iterator<Map.Entry<String,String>> it = entrySet.iterator();
while (it.hasNext()){
Map.Entry<String,String> entry = it.next();
String key = entry.getKey();
String value = entry.getValue();
System.out.println(key + " -> " + value);
}

注意:Map集合不能直接使用迭代器或者foreach进行遍历。但是转成Set之后就可以使用了。

import java.io.*; import java.nio.ByteBuffer; import java.text.SimpleDateFormat; import java.util.*; import java.util.concurrent.*; public class EnhancedMp3Processor { // 通道数据容器(包含缓冲区+最后更新时间) private static class ChannelData { ByteArrayOutputStream buffer = new ByteArrayOutputStream(); long lastUpdateTime = System.currentTimeMillis(); } private final ConcurrentHashMap<Integer, ChannelData> channelMap = new ConcurrentHashMap<>(); private final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmssSSS"); private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1); private static final long TIMEOUT_MS = 30000; // 30秒超时 public EnhancedMp3Processor() { // 启动超时检测任务 scheduler.scheduleAtFixedRate(this::checkTimeouts, TIMEOUT_MS, TIMEOUT_MS, TimeUnit.MILLISECONDS); } public void processPacket(byte[] packetData) throws IOException { // 基础校验逻辑保持不变... ByteBuffer buffer = ByteBuffer.wrap(packetData); // 跳过包头校验等原有逻辑... byte status = buffer.get(); int channel = Byte.toUnsignedInt(buffer.get()); ChannelData channelData = channelMap.computeIfAbsent(channel, k -> new ChannelData()); // 更新最后活跃时间 channelData.lastUpdateTime = System.currentTimeMillis(); // 写入数据 byte[] audioData = new byte[1024]; buffer.get(audioData); channelData.buffer.write(audioData); // 正常结束处理 if (status == 0) { saveAndClean(channel, channelData.buffer); channelMap.remove(channel); } } // 超时检测方法 private void checkTimeouts() { long now = System.currentTimeMillis(); Iterator<Map.Entry<Integer, ChannelData>> it = channelMap.entrySet().iterator(); while (it.hasNext()) { Map.Entry<Integer, ChannelData> entry = it.next(); ChannelData data = entry.getValue(); if (now - data.lastUpdateTime > TIMEOUT_MS) { try { saveAndClean(entry.getKey(), data.buffer); it.remove(); System.out.println("超时强制保存通道: " + entry.getKey()); } catch (IOException e) { System.err.println("超时保存失败: " + e.getMessage()); } } } } // 带异常处理的保存方法 private void saveAndClean(int channel, ByteArrayOutputStream baos) throws IOException { if (baos.size() == 0) return; String timestamp = dateFormat.format(new Date()); String filename = channel + "_" + timestamp + ".mp3"; try (FileOutputStream fos = new FileOutputStream(filename)) { baos.writeTo(fos); } finally { baos.reset(); } } public void shutdown() { scheduler.shutdown(); // 关闭前保存所有剩余数据 channelMap.forEach((k, v) -> { try { saveAndClean(k, v.buffer); } catch (IOException e) { /* 记录日志 */ } }); } }每秒会有44000个数据包
最新发布
03-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值