英文:
UnsupportedOperationException when inserting array into Map
问题
在将数组插入到Map时,出现了UnsupportedOperationException异常。Map的输入是正确的。是否有正确的方法可以插入数据并返回数据?
public static Map<String, ProductClassPeriodData[]> getPeriodsByAgreement(String[] productClassIds, String agreementId) 
{
    Map<String, ProductClassPeriodData[]> data = new HashMap<>();
    for (int i = 0; i < productClassIds.length; i++)
    {
        ProductClassPeriodData[] periodData = getInstance().getProductClassPeriodsByAgreement(productClassIds[i], agreementId);
        data.put(String.valueOf(i), periodData);
    }
    return data;
}
英文:
I am getting an UnsupportedOperationException when inserting array into Map. The inputs to the map are correct. Is there any proper way I can get insert properly and return the data?
	public static Map<String, ProductClassPeriodData[]> getPeriodsByAgreement(String[] productClassIds,String agreementId) 
	{
		Map data = Collections.EMPTY_MAP;
		for (int i = 0; i < productClassIds.length; i++)
		{
			ProductClassPeriodData[] periodData = getInstance().getProductClassPeriodsByAgreement(productClassIds[i], agreementId);
			data.put(String.valueOf(i), periodData);
		}
		return data;
	}
答案1
得分: 1
Collections.EMPTY_MAP 是不可变的,因此不支持此操作。
/**
- 空地图(不可变)。此地图可序列化。
 - @see #emptyMap()
 - @since 1.3
*/
@SuppressWarnings("rawtypes")
public static final Map EMPTY_MAP = new EmptyMap<>(); 
改为使用
Map<String, ProductClassPeriodData[]> data = new HashMap<>();
英文:
Collections.EMPTY_MAP is immutable, there for this operation is not supported.
/**
 * The empty map (immutable).  This map is serializable.
 *
 * @see #emptyMap()
 * @since 1.3
 */
@SuppressWarnings("rawtypes")
public static final Map EMPTY_MAP = new EmptyMap<>();
Instead use
Map<String, ProductClassPeriodData[]> data = new HashMap<>();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论