0

将数组插入 Map 时出现 UnsupportedOperationException。地图的输入是正确的。有什么合适的方法可以正确插入并返回数据吗?

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

1 回答 1

1

Collections.EMPTY_MAP是不可变的,因此不支持此操作。

/**
 * The empty map (immutable).  This map is serializable.
 *
 * @see #emptyMap()
 * @since 1.3
 */
@SuppressWarnings("rawtypes")
public static final Map EMPTY_MAP = new EmptyMap<>();

而是使用

Map<String, ProductClassPeriodData[]> data = new HashMap<>();
于 2020-08-14T06:51:14.207 回答