JAVA 从一个List中匹配另一个List的值并返回一个List
List<LookupValuesVO> valuesVOList =new ArrayList<>(); LookupValuesVO a1=new LookupValuesVO(); a1.setLookupCode("CNY"); a1.setMeaning("人民币CNY"); valuesVOList.add(a1); LookupValuesVO a2=new LookupValuesVO(); a2.setLookupCode("EUR"); a2.setMeaning("欧元EUR"); valuesVOList.add(a2); LookupValuesVO a3=new LookupValuesVO(); a3.setLookupCode("HKD"); a3.setMeaning("港币HKD"); valuesVOList.add(a3); LookupValuesVO a4=new LookupValuesVO(); a4.setLookupCode("OTHER"); a4.setMeaning("其它"); valuesVOList.add(a4); LookupValuesVO a5=new LookupValuesVO(); a5.setLookupCode("USD"); a5.setMeaning("美金USD"); valuesVOList.add(a5); //分组 Map<String,List<LookupValuesVO>> lookupMap = valuesVOList.stream().collect(Collectors.groupingBy(LookupValuesVO :: getLookupCode)); //需处理的数据 String str="HKD,USD,CNY"; //分割 List<String> lookupCode= Arrays.asList(str.split(",")).stream().map(block -> (block.trim())).collect(Collectors.toList()); System.out.println("bf:"+lookupCode); //匹配 lookupCode=lookupCode.stream().map(ro->{ if(!ObjectUtils.isEmpty(lookupMap.get(ro))){ return lookupMap.get(ro).get(0).getMeaning(); } return ro; }).collect(Collectors.toList()); //输出处理后的List System.out.println("af:"+lookupCode); //拼接成字符串 str=String.join(",",lookupCode); System.out.println("str:"+str);