gpt4 book ai didi

java - 将列表作为值插入映射中

转载 作者:行者123 更新时间:2023-12-01 13:30:58 25 4
gpt4 key购买 nike

我收到了 List<String>形成 hibernate,其中包含 completeFileAddress .

CompleteFileAddress我从 List 获取是:

192.168.1.1/var/local/files/20140116/8800399421_4007_20140116_151718
192.168.1.2/var/local/files/monitor/20140115/9654180710_4006_20140115
192.168.1.1/var/local/files/20140116/8800399421_4007_20140116_120042
192.168.1.1/var/local/files/20140116/138_4007_20140116_122719

然后我 split 了CompleteFileAddress获取IPAddressFilePath分别将值放入 Map<String, List<String>> 中其中键是 IPAddress值是 FilePath 的列表.

我的代码是:

   List list = criteria.list();
for (Iterator it = list.iterator(); it.hasNext();) {
String completeFileAddress = (String) (it.next());
System.out.println("Complete add : " + completeFileAddress);
if (completeFileAddress != null) {
int index = completeFileAddress.indexOf("/");
String serverIP = completeFileAddress.substring(0, index);
String filePath = completeFileAddress.substring(index, completeFileAddress.length()) + ".WAV";
filesMap.put(serverIP, sourceFileList); //sourceFileList is declared as an instance variable
sourceFileList.add(filePath);
}
}
System.out.println("List '1' size :" + filesMap.get("192.168.1.1").size());
System.out.println("List '2' size :" + filesMap.get("192.168.1.2").size());

我正在获取两者的大小 List4 ,但我想要 List1 = 3 的大小和List2 = 1因为我在服务器上有 3 个文件 192.168.1.1只有一个在 192.168.1.2 .

我怎样才能实现这个目标???

最佳答案

  • 您将相同的对放入映射中,并为两个键引用相同的列表。
  • 您可以检查映射是否包含键(如果存在)只需检索值(即列表)并将filePath添加到该列表
  • 如果 key 不存在,则创建新列表并将 filePath 添加到新创建的列表中,最后将 keyvalue 添加到映射<

试试这个代码

String serverIP = completeFileAddress.substring(0, index);
String filePath = completeFileAddress.substring(index, completeFileAddress.length()) + ".WAV";
if(filesMap.containsKey(serverIP))
{
ArrayList<String> sourceList = filesMap.get(serverIP);
sourceList.add(filePath);
}
else
{
ArrayList<String> sourceList = new ArrayList<String>();
sourceList.add(filePath);
filesMap.put(serverIP, sourceFileList);
}

关于java - 将列表作为值插入映射中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21575502/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com