如何根据列表中的值使用流过滤列表?

时间:2015-09-11 06:48:45

标签: java java-8 java-stream

我有List<List<Double>>。我想根据索引过滤行,即如果索引4处的元素值小于0.2,那么,跳过该行?结果List<List<Double>>应该小于或等于输入的 MPMoviePlayerViewController * moviePlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:local_url]; moviePlayer.moviePlayer.movieSourceType = MPMovieSourceTypeFile; [moviePlayer.moviePlayer setControlStyle:MPMovieControlStyleFullscreen]; [moviePlayer.view setTranslatesAutoresizingMaskIntoConstraints:YES]; [self presentMoviePlayerViewControllerAnimated:moviePlayer];

1 个答案:

答案 0 :(得分:3)

您可以使用Stream.filter。请注意,您必须选择要采用的行,而不是要跳过的行:

List<List<Double>> input = ...;

List<List<Double>> result = input.stream()
              .filter(row -> row.get(4) >= 0.2)
              .collect(Collectors.toList());

Stream API的替代方法是使用Collection.removeIf进行就地修改:

List<List<Double>> input = ...;

input.removeIf(row -> row.get(4) < 0.2);