list筛选出包含特定字符串的元素,元素再反转,再去重(使用jdk8 stream)
去重可以用set,这里使用jdk8 stream去重。
例如:
1 public static void main(String[] args) { 2 String subStr = "a"; 3 List<String> list = new ArrayList<>(Arrays.asList("abc", "test", "ivy", "absent", "total", "jack", "woo", "java", "Jenkins", "docker","test","Docker")); 4 List<String> tempList = new ArrayList<>(); 5 //筛选包含a的元素 6 for (String s :list) { 7 if (!s.contains(subStr)) { 8 //元素反转 9 String reverse = new StringBuffer(s).reverse().toString(); 10 tempList.add(reverse); 11 } 12 } 13 14 //去重 15 List<String> result = tempList.stream().distinct().collect(Collectors.toList()); 16 System.out.println(result.get(0)); 17 System.out.println(result.get(1)); 18 System.out.println(result.get(2)); 19 20 }