Collectors.toMap给出编译错误(String,List <String)不适用于参数

mmathank:
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class StreamExample {

    public static void main(String[] args) {

        Student student = new Student();
        StudentDB studentDb = new StudentDB();

        System.out.println("All Elements from the List \n");
        studentDb.getStudentList().forEach(System.out::println);

        Map<String, List<String>> studentMap = studentDb.getStudentList().stream()
                .collect(Collectors.toMap(student.getName(), student.getActivities()));

    }
}
Azro:

使用的是东西,student.getName()它只是在对象上调用方法,因此您将获得其名称和活动,将获得值,没有通用方法来终止Stream


Collectors.toMap希望你传递函数(Function<Student,String>关键,Function<Student,List<String>>对于价值),即从Student会给别的东西

  • 您可以使用lambda表示它: s -> s.getName()

    Map<String, List<String>> studentMap = studentDb.getStudentList().stream()
                   .collect(Collectors.toMap(s -> s.getName(), s -> s.getActivities()));
    
  • 或方法参考Student::getName,您可以使用方法本身,而不是应用于特定对象

    Map<String, List<String>> studentMap = studentDb.getStudentList().stream()
                   .collect(Collectors.toMap(Student::getName, Student::getActivities));
    

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

RestTemplate不适用于参数(URL,Request,Class <String>)

类型JavaConversions不适用于参数(Set <String>)

Map <String,List <Object >>的Collectors.toMap无法解析

函数类型中的方法replace(String,String,String)不适用于参数(StringBuffer,String,String)

如果条件不适用于String

DateFormat类型的方法parse(String)不适用于参数(List <String>)

类型为List <String>的方法add(String)不适用于参数(Map)

JsonObject类型的方法add(String,JsonElement)不适用于参数(String,String)

错误:TextView类型的setText(CharSequence)方法不适用于参数(String [])

JdbcTemplate类型的方法query(String,ResultSetExtractor <T>)不适用于参数(String,BeanPropertyRowMapper)

类型PrintStream的方法printf(String,Object [])不适用于参数(...)

Map<Integer,Integer> 类型中的 put(Integer, Integer) 方法不适用于参数 (String, Integer)

Android:Toast类型的方法makeText(Context,CharSequence,int)不适用于参数(ListViewAdapter,String,int)

Assert 类型中的方法 assertEquals(Object, object) 不适用于参数 (String, Void)

Java 乘法(在类型 PrintStream 中不适用于参数 (String, int))

“类型中的方法不适用于参数”错误

Mockito错误不适用于该参数(无效)

错误:该方法不适用于参数

thenComparing 中的错误不适用于参数 Comparator<..DTO>

List <PdfPCell>不适用于参数(字符串)

Integer.parseInt返回错误:Integer类型的方法parseInt(String)不适用于参数(R.string)

make_pair不适用于string,适用于string&

在 HttpServletRequest 的 headerNames 枚举上使用 Collectors.toMap 创建 HttpHeaders 时出现编译错误

Collectors.toMap无法编译

Localizable.strings 适用于 String,但不适用于 Double 和 Integer 32

Iterable <Map <String,Object >>类型的forEach(Consumer <?super Map <String,Object >>)方法不适用于参数(((<no type> k

类型为SearchStack <E>的searchingStacks(Stack <E>,E)方法不适用于参数(Stack <String>,String)

Stream<Task> 类型中的方法 reduce(Task, BinaryOperator<Task>) 不适用于参数 (HashMap<String,Map<String,Object>>

MediaPlayer.setDataSource(String)不适用于本地文件