Java 如何使用字符串筛选我的列表?

Java 如何使用字符串筛选我的列表?,java,collections,java-stream,Java,Collections,Java Stream,有人能解释一下为什么我不能通过.stream().filter过滤列表中的字符串吗?我只收到未过滤的字符串! 我会很感激的 public static void main(String[] args) throws FileNotFoundException { List<Country> countries = CsvToBeanConverterCountry("country.csv"); countries .

有人能解释一下为什么我不能通过.stream().filter过滤列表中的字符串吗?我只收到未过滤的字符串! 我会很感激的

public static void main(String[] args) throws FileNotFoundException {

        List<Country> countries = CsvToBeanConverterCountry("country.csv");
        countries
                .stream().filter(s -> s.getName().contains("xx")).collect(Collectors.toList());
        countries.forEach(s -> System.out.println(s.getName()));
    }

    private static List CsvToBeanConverterCountry(String csvFilename) throws FileNotFoundException {

        CsvToBean csv = new CsvToBean();
        CSVReader csvReader = new CSVReader(new FileReader(csvFilename), ';', '"', 1);
        List list = csv.parse(setColumMappingCountry(), csvReader);
        return list;
    }

    private static ColumnPositionMappingStrategy setColumMappingCountry() {
        ColumnPositionMappingStrategy strategy = new ColumnPositionMappingStrategy();
        strategy.setType(Country.class);
        String[] columns = new String[]{"country_id", "city_id", "name"};
        strategy.setColumnMapping(columns);
        return strategy;
    }
}
publicstaticvoidmain(字符串[]args)抛出FileNotFoundException{
列表国家=CsvToBeanConverterCountry(“country.csv”);
国家
.stream().filter(s->s.getName().contains(“xx”)).collect(Collectors.toList());
countries.forEach(s->System.out.println(s.getName());
}
私有静态列表CsvToBeanConverterCountry(字符串csvFilename)引发FileNotFoundException{
CsvToBean csv=新的CsvToBean();
CSVReader CSVReader=新的CSVReader(新文件读取器(csvFilename),';','”,1);
List List=csv.parse(setColumMappingCountry(),csvReader);
退货清单;
}
私有静态列PositionMappingStrategy setColumMappingCountry(){
ColumnPositionMappingStrategy=新ColumnPositionMappingStrategy();
策略.setType(Country.class);
字符串[]列=新字符串[]{“国家id”、“城市id”、“名称”};
策略.setColumnMapping(列);
回报策略;
}
}

您应该将结果分配给
国家/地区

countries = countries.stream().filter(s -> s.getName()
             .contains("xx")).collect(Collectors.toList());
countries.forEach(s -> System.out.println(s.getName()));
countries.stream().filter(…)
不内联过滤
国家列表,它运行一个流,结果被忽略

此外,您可以直接在流上运行
forEach
,而无需先收集到列表中:

countries.stream().filter(s -> s.getName()
             .contains("xx"))
             .forEach(s -> System.out.println(s.getName()));

您忽略了收集的结果:

countries.stream().filter(s -> s.getName().contains("xx"))
         .collect(Collectors.toList());
因此,您应该将结果分配给一个新列表,并仅打印该列表

List<Country> result = countries.stream()...
result.forEach(...)
List result=countries.stream()。。。
result.forEach(…)

当然,发生的情况是:.stream()不会更改实际列表

您可以做的是:

List<Country> countries = CsvToBeanConverterCountry("country.csv");
countries
    .stream()
    .filter(s -> s.getName().contains("xx"))
    .forEach(s -> System.out.println(s.getName()));
List countries=CsvToBeanConverterCountry(“country.csv”);
国家
.stream()
.filter(s->s.getName().contains(“xx”))
.forEach(s->System.out.println(s.getName());

似乎您的目的是修改原始列表,而不是生成包含某些元素的新列表

在这种情况下,您只需删除不需要的元素:

countries.removeIf(c -> !c.getName().contains("xx"));
这比创建流和新集合更有效,但它会改变列表