Q:如何在HashMap<String,String> 中将一个List<String>转化为String传入? 方法一:
String str1 = List.toString(); String str = str1.substring(1,str1.length()-1);
map.put("num",str);
方法二:
map.put("num", Joiner.on(",").join(List));
分析:为什么方法一会出错呢?
将方法一打印出来会发现str为 12137575010, 12138616153, 12131437986, 12122293697, 12521437985, 12520143427, 12520143433, 12527519996, 12520143427, 12520143433 每个逗号之后都有空格;
而方法二打印出来,没有空格:
12137575010,12138616153,12131437986,12122293697,12521437985,12520143427,12520143433,12527519996,12520143427,12520143433
/** * @Author wangnian23 * @Date 2020/7/14 15:08 * @Version 1.0 */ @Component public class SellerCountDomain { private final static Logger logger = LoggerFactory.getLogger(SellerCountDomain.class); @Value("${epc.robot.goods.stat.url}") private String QuerySallerCountByOeUrl; @Value("${epc.robot.appKey}") private String appKey; public Map<String, Integer> getSellerCountByOe(List<String> oeList) throws Exception { if (oeList.size() > 0) { //param用来存储传入的参数, Map<String, String> param = new HashMap<>(); Map<String, Integer> map = new HashMap<>(); param.put("appKey", appKey); param.put("marketType", "2"); if (!CollectionUtils.isEmpty(oeList)) { param.put("oeNum", Joiner.on(",").join(oeList)); } try { String response = HttpClientUtils.httpGet(QuerySallerCountByOeUrl, param, CommonConstants.HTTP_CHARSET_UTF8, CommonConstants.HTTP_MAX_TIMEOUT, CommonConstants.HTTP_MAX_TIMEOUT); logger.info(oeList + " 根据oe统计相关商家的数量: " + response); if (StringUtils.isNotEmpty(response)) { JSONObject jsonObject = JSONObject.parseObject(response); if (jsonObject.getInteger("status") == 200 && StringUtils.isNotEmpty(jsonObject.getString("data"))) { LeagueGoodsStatResVo leagueGoodsStatResVo = JSON.parseObject(jsonObject.getString("data"), LeagueGoodsStatResVo.class); List<LeagueGoodsSellerStatResVo> list = leagueGoodsStatResVo.getOeSellerStat(); for (int i = 0; i < list.size(); i++) { map.put(list.get(i).getOeNum(), list.get(i).getCount()); } return map; } else { int status = jsonObject.getInteger("status"); String message = jsonObject.getString("message"); throw new ThirdpartyException(status, message); } } } catch (IOException e) { String message = " 根据oe统计相关商家的数量:" + e.getMessage(); logger.error(message); throw e; } } return null; } }