将hashmap里的数据放到list中,数据都重复了


   public DataResponse<List<HashMap<String, String>>> adsConfigList() {
        List<AccountConfig> accountConfigs = accountConfigMapper.selectExist();

        List<HashMap<String, String>> list = new ArrayList<>();
        HashMap<String, String> params = new HashMap<>();
        for (AccountConfig accountConfig : accountConfigs) {
            params.put("accountId",accountConfig.getAccountId());
            params.put("accountName",accountConfig.getAccountName());
            list.add(params);
        }

        return DataResponse.successful(list);

为什么将数据这样插入list,list 的数据都是重复 的

img

因为for循环里面放的都是同一个params,应该要改成在for循环里面去new HashMap<>()

你这每次循环后面的key值都把前面的覆盖了,想用map替换对象需要在循环里面定义.

你在循环外面new,都指向同一个地址,所以最终添加的都是最后一条数据。你要在循环里面去new。