-2

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)

1 Answers1

0

The exception occurs when your code tries to add a new element to a list created by Arrays.asList() from an array. This method produces a fixed-size list and new elements can not be added to it. See javadoc of asList() for further explanation.

An easy way to fix this is to use an ArrayList (or other implementation of List) instead, and initialize it with the list obtained from Arrays.asList() call.

From the stack traces you posted I assume it's the list you put as a value to the map here: fileNeedCheck.put(list1,Arrays.asList( new FC(part1,part2,path,stagPath,file,a)) );

Change this to: fileNeedCheck.put(list1, new ArrayList<FC>(Arrays.asList(new FC(part1,part2,path,stagPath,file,a))));

david a.
  • 5,283
  • 22
  • 24
  • hi David, how i can solve this – Pallvi Mahajan Sapen Jul 12 '17 at 09:37
  • Hi David, but i want to add value to existing key .when i tried ur code, it only shows me last value for the key, not 2 value. – Pallvi Mahajan Sapen Jul 12 '17 at 09:48
  • I updated my answer. Also note that the code you posted contained references to classes that were not included. It was not possible to compile it and it was a little hard to understand. See how to create a code example here: https://stackoverflow.com/help/mcve – david a. Jul 12 '17 at 09:49
  • HI David, how i can add another class here, – Pallvi Mahajan Sapen Jul 12 '17 at 09:52
  • @PallviMahajanSapen, about your 2nd comment: I don't see how it can be caused by the change I proposed. Can you explain what your test is? Also, are you able to see two values in the list without my change in place (or is it just the exception then) ? – david a. Jul 12 '17 at 09:53
  • @PallviMahajanSapen `if (!fileNeedCheck.containsKey(list1)) { fileNeedCheck.put(list1, new ArrayList(Arrays.asList(new FC(part1, part2, path, stagPath, file, a)))); } else { fileNeedCheck.get(list1).add(new FC(part1, part2, path, stagPath, file, a)); }` this should be how your if-else looks like – XtremeBaumer Jul 12 '17 at 09:54
  • HI Baumer, yes my if else block looks like that, if key is not present, then they add key, value pair and if key is present then it only add value on that keys in map . I have declared my map like that at top of my code. Map, List> fileNeedCheck = new HashMap, List>();' – Pallvi Mahajan Sapen Jul 12 '17 at 10:01
  • HI David, i added ur code i have existing key and for that i want to add another value but by doing that way it only add one value not 2 value. I tested and it did not work. It only shows me last value . – Pallvi Mahajan Sapen Jul 12 '17 at 10:02
  • I suggest you to debug and see if both branches of that `if` statement are indded called and when they're called. – david a. Jul 12 '17 at 10:09