UnsupportedOperationException在将数组插入到Map中时发生。

huangapple go评论63阅读模式
英文:

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&lt;String, ProductClassPeriodData[]&gt; getPeriodsByAgreement(String[] productClassIds,String agreementId) 
	{
		Map data = Collections.EMPTY_MAP;
		for (int i = 0; i &lt; 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(&quot;rawtypes&quot;)
public static final Map EMPTY_MAP = new EmptyMap&lt;&gt;();

Instead use

Map&lt;String, ProductClassPeriodData[]&gt; data = new HashMap&lt;&gt;();

huangapple
  • 本文由 发表于 2020年8月14日 14:36:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/63407716.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定