为什么我无法在 java Stream 的代码中使用收集器? [关闭]

2024-01-02

import static java.util.stream.Collectors.*;
import java.util.*;
import java.lang.*;
//import java.util.Collections;
public class HelloWorld{

 public static void main(String []args){
    System.out.println("Hello World");
    List<String> strings = Arrays.asList("abc", "", "bc", "efg", "abcd","", "jkl");
    List<String> filtered = strings.stream().filter(string -> !string.isEmpty()).collect(Collectors.toList());
    }
}

output

/tmp/java_tdo3eB/HelloWorld.java:10: error: cannot find symbol
    List<String> filtered = strings.stream().filter(string -> !string.isEmpty()).collect(Collectors.toList());
                                                                                         ^
  symbol:   variable Collectors
  location: class HelloWorld
 1 error

所以我想知道为什么我无法使用收集器,因为我也导入了该类


这是你的进口货。这样做:

package experiments;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 *
 * @author Luc Talbot
 */
public class HelloWorld {

 public static void main(String []args){
    System.out.println("Hello World");
    List<String> strings = Arrays.asList("abc", "", "bc", "efg", "abcd","", "jkl");
    List<String> filtered = strings.stream()
                                   .filter(string -> !string.isEmpty())                        
                                   .collect(Collectors.toList());
    }
}

输出是:

跑步: 你好世界 构建成功(总时间:0 秒)

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

为什么我无法在 java Stream 的代码中使用收集器? [关闭] 的相关文章

随机推荐