3

I am trying to read a list from a map and trying to add data to the list. I am getting java.lang.UnsupportedOperationException. Please can you let me know how this can be resolved

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;




public class ListOperation {

    public static void main(String[] args){



        Map<String,List<Item>> packageMap = new HashMap<String,List<Item>>();
        Item item = new Item();
        item.billingIdentifier =  "pkg1";
        item.name="pkg1";

        Item item1 = new Item();
        item1.billingIdentifier =  "pkg2";
        item1.name="pkg2";

        Item item2 = new Item();
        item2.billingIdentifier =  "pkg3";
        item2.name="pkg3";

        ItemList itemList = new ItemList();
        itemList.setItem(item);

        ItemList itemList1 = new ItemList();
        itemList1.setItem(item1);       

        ItemList itemList2 = new ItemList();
        itemList2.setItem(item2);   

        List<ItemList> itemLists = new ArrayList<ItemList>();
        itemLists.add(itemList);
        itemLists.add(itemList1);
        itemLists.add(itemList2);

        for(ItemList itList:itemLists){
            Item it = itList.getItem();
            if(it != null){
                packageMap.put(it.getBillingIdentifier(),Arrays.asList(it));
            }
        }

  List<Item> pkgitjj = packageMap.get("pkg3");
    pkgitjj.add(new Item());



    }

}

ITEMLIST

import java.util.List;

public class ItemList {

    public Item item;

    public Item getItem() {
        return item;
    }

    public void setItem(Item item) {
        this.item = item;
    }






}

ITEM

public class Item {
public String name;
public String billingIdentifier;
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public String getBillingIdentifier() {
    return billingIdentifier;
}
public void setBillingIdentifier(String billingIdentifier) {
    this.billingIdentifier = billingIdentifier;
}



}
Punter Vicky
  • 15,954
  • 56
  • 188
  • 315
  • 1
    Add the complete stacktrace please – Jens Dec 11 '15 at 19:34
  • Possible duplicate of [Why I get UnsupportedOperationException when trying to remove from the List?](http://stackoverflow.com/questions/2965747/why-i-get-unsupportedoperationexception-when-trying-to-remove-from-the-list) – Atri Dec 11 '15 at 19:34
  • 1
    Where is the code in which you are fetching the list from the map (as the title suggests)? – Eran Dec 11 '15 at 19:36
  • How are Item and ItemList defined? – Thomas Kläger Dec 11 '15 at 19:37

2 Answers2

7

Arrays.asList(it) produces a fixed sized List. Therefore you can't add or remove elements to/from that List.

That's the reason why

pkgitjj.add(new Item());

throws UnsupportedOperationException.

You can replace:

packageMap.put(it.getBillingIdentifier(),Arrays.asList(it))

with:

packageMap.put(it.getBillingIdentifier(),new ArrayList<Item>(Arrays.asList(it)))

To fix this problem.

Eran
  • 387,369
  • 54
  • 702
  • 768
2

Arrays.asList(it) returns an fixed size list. Therefore this code will fail:

List<Item> pkgitjj = packageMap.get("pkg3");
pkgitjj.add(new Item());

You should write

new ArrayList<>(Arrays.asList(it)

instead - this creates a new resizable ArrayList.

Thomas Kläger
  • 17,754
  • 3
  • 23
  • 34
  • 1
    The list is not unmodifiable. It is fixed-size. You can still set any value you want inside the list, but can't add or remove elements. – JB Nizet Dec 11 '15 at 19:50