我需要这个程序的帮助。我正在尝试将列表值添加到Map中的现有键。请找到以下程序。请告诉我我错在哪里以及如何做。 `
public class UpStreamFileCount
{
public static void main(String[] args) throws Exception
{
String App_code = args[0];
String App_Ctry_code = args[1];
String hostname=null;
Map<List<String>, List<FC>> fileNeedCheck = new HashMap<List<String>, List<FC>>();
try
{
InetAddress inetAddress = InetAddress.getLocalHost();
hostname=inetAddress.getHostName();
}
catch(UnknownHostException unknownHostException)
{
unknownHostException.printStackTrace();
}
BatchDateCalculation cmd = new BatchDateCalculation ();
String Batch_date = cmd.Compare();
Date currTime = cmd.CurrTime();
Date timeBeforeOnehour = cmd.BeforOneHour();
Pattern pattern = Pattern.compile(",");
try (BufferedReader in = new BufferedReader(new FileReader("FC.csv"));)
{
Map<List<String>,List<FC>> grouped = in
.lines()
.skip(1)
.map(line -> {
String[] arr = pattern.split(line);
return new FC(arr[0],
arr[1],
arr[2],
arr[3],
arr[4],
arr[5]);
})
.collect(Collectors.groupingBy(x -> {
return new ArrayList<String>(Arrays.asList(x.getUpstreamAppCode(), x.getUpstreamGrpAppCode()));
}));
File data = new File("newFile.txt");
if (data.exists())
{
data.delete();
}
FileWriter fw = new FileWriter(data,true);
for(Map.Entry<List<String>,List<FC>> entry : grouped.entrySet() )
{
List<String> list1 = entry.getKey();
for(FC value : entry.getValue() )
{
String a = value.getSlaTime() ;
Date newTime = cmd.covertTimeToDate(a);
if (timeBeforeOnehour.before( newTime ) && currTime.after(newTime))
{
String result = String.join(",", list1);
String[] parts = result.split(",");
String part1 = parts[0];
String part2 = parts[1];
String path = value.getPath();
String stagPath = value.getStagPath();
String file = value.getFile();
String s = result + "," + path + "," + stagPath + "," + file + "," + a ;
String addMapValue = path + "," + stagPath + "," + file + "," + a ;
fw.write(s);
fw.write(System.getProperty( "line.separator" ));
if(!fileNeedCheck.containsKey(list1))
{
fileNeedCheck.put(list1,Arrays.asList( new FC(part1,part2,path,stagPath,file,a)) );
}
else
{
List<FC> t = new ArrayList<String>(Arrays.asList(new FC(part1,part2,path,stagPath,file,a)));
t.add(value);
fileNeedCheck.get(list1).put(t);
}
}
}
}
fw.close();
}
`
问题在于: - 在代码的else部分。请告诉我如何做到这一点
if(!fileNeedCheck.containsKey(list1))
{
fileNeedCheck.put(list1,Arrays.asList( new FC(part1,part2,path,stagPath,file,a)) );
}
else
{
List<FC> t = new ArrayList<FC>(Arrays.asList(new FC(part1,part2,path,stagPath,file,a)));
t.add(value);
fileNeedCheck.get(list1).add(value);
// fileNeedCheck.get(list1).put(t);
}
我收到以下错误: -
&GT;
Exception in thread "main" java.lang.UnsupportedOperationException at java.util.AbstractList.add(AbstractList.java:148) at java.util.AbstractList.add(AbstractList.java:108) at java.util.AbstractCollection.addAll(AbstractCollection.java:344) at UpStreamFileCount.main(UpStreamFileCount.java:107)
答案 0 :(得分:0)
当您的代码尝试将新元素添加到由Arrays.asList()
从数组创建的列表时,会发生异常。此方法生成固定大小的列表,并且无法向其添加新元素。有关详细说明,请参阅javadoc of asList()
。
解决此问题的一种简单方法是使用ArrayList
(或List
的其他实现),并使用从Arrays.asList()
调用获得的列表对其进行初始化。
从您发布的堆栈跟踪中我假设它是您在此处作为值放入的列表:
fileNeedCheck.put(list1,Arrays.asList( new FC(part1,part2,path,stagPath,file,a)) );
将此更改为:
fileNeedCheck.put(list1, new ArrayList<FC>(Arrays.asList(new FC(part1,part2,path,stagPath,file,a))));