I need a help in this program. I am trying to add list value to existing key in Map. Please find below program. Kindly let me know where I am wrong and how I can do. `
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();
}
`
The issue is here: - in the else part of the code . Please let me know how I can do this
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);
}
I am getting the error below:-
>
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)