Punter Vicky
Punter Vicky

Reputation: 16992

UnSupported Operation Exception - Fetching list from map and adding data to map

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;
}



}

Upvotes: 4

Views: 5038

Answers (2)

Thomas Kl&#228;ger
Thomas Kl&#228;ger

Reputation: 21490

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.

Upvotes: 2

Eran
Eran

Reputation: 393846

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.

Upvotes: 10

Related Questions