java8 list转Map报错Collectors.toMap :: results in "Non-static method cannot be refernced from static context"
1.问题:java8 list转Map
报错Collectors.toMap :: results in "Non-static method cannot be refernced from static context"

解决:将第二个参数传入function

原因:Collectors.toMap参数接收为function
2.解决key重复问题:
Map<String,String> map2=list.stream().collect(Collectors.toMap(Person::getName,o->"",(key1,key2)->key2));
输出:{cc=, bb=, aa22=, aa11=}
3.value为对象本身,注意map泛型为Person
Person
public class Person {
private String id;
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public Person(String name) {
this.name = name;
}
public Person(String id, String name) {
this.id = id;
this.name = name;
}
@Override
public String toString() {
return "Person{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
'}';
}
map
List<Person> list=new ArrayList<>();
list.add(new Person("aa","aa11"));
list.add(new Person("bb",""));
list.add(new Person("aa","aa22"));
list.add(new Person("cc",""));
Map<String,Person> map2=list.stream().collect(Collectors.toMap(o->o.getId(),o->o,(key1,key2)->key2));//重复时取后面的
//Map<String,Person> map2=list.stream().collect(Collectors.toMap(o->o.getId(), Function.identity(),(key1, key2)->key2));//优雅写法
System.out.println(map2);
输出:{cc=Person{id='cc', name=''}, bb=Person{id='bb', name=''}, aa=Person{id='aa', name='aa22'}}
4.排序:
Map<String,String> map2=list.stream().collect(Collectors.toMap(Person::getId,Person::getName ,(key1, key2)->key2,TreeMap<String,String>::new));
输出{aa=aa22, bb=, cc=}
其它:java8--List转为Map、分组、过滤、求和等操作
java8 list转Map报错Collectors.toMap :: results in "Non-static method cannot be refernced from static context"的更多相关文章
- ISE MAP报错: Unsupported programming for BSCAN block and JTAG_CHAIN attribute value 1的解决方法
2014-04-16 17:35:30 ISE MAP报错: Unsupported programming for BSCAN block and JTAG_CHAIN attribute valu ...
- Xcode报错Expected selector for Objective-C and Expected method body
昨天把键盘拿起来拍一下清清灰,然后就发现Xcode报错了,Xcode报错Expected selector for Objective-C and Expected method body,也不知道什 ...
- ceph集群jewel版本 rbd 块map 报错-故障排查
测试信息如下: [root@ceph_1 ~]# ceph osd pool lsrbdchy_123swimmingpool #新建rbd 块: rbd create swimmingpool/ba ...
- vue项目中使用echarts map报错Cannot read property 'push' of undefined nanhai.js
在vue中绘制地图需要加载一个本地china.json文件,我用的是get请求的方法加载的,而不是直接import,因为我怕import请求到的部署到线上的时候会有问题.如下是get请求方法: thi ...
- SSH项目练习的时候报错:[applicationContext.xml]: Invocation of init method failed;
这里是控制台的报错信息:org.springframework.beans.factory.BeanCreationException: Error creating bean with name ' ...
- Spring Scheduled定时任务报错 java.lang.IllegalStateException: Encountered invalid @Scheduled method 'xxx': For input string: "2S"
报错信息如下: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ding ...
- laravel Tinker报错 BadMethodCallException with message 'Call to undefined method Illuminate\Database\Query\Builder
进行模型关联操作, php artisan tinker 执行 $user = App\Models\User::find(1) $user->followings()->attach([ ...
- Jmeter BeanShell 引用变量报错jmeter.util.BeanShellInterpreter: Error invoking bsh method: eval Parse error at line 14, column 181 : Error or number too big for integer
如果你通过CSV Data Set Config或者_StringFromFile函数来参数化你的请求,需要特别注意当参数为纯数字时,jmeter会默认将其识别成int型数据,说明jmeter并不是默 ...
- 关于Flutter启动项目白屏,报错[ERROR:flutter/shell/gpu/gpu_surface_gl.cc(58)] Failed to setup Skia Gr context.问题的解决方案
首先,环境如下: 1.系统:windows10 64位 Android SDK version: 28.0.3 Flutter SDK: v1.5.4-hotfix.2 模拟器: 网易Mu ...
随机推荐
- 数据分析画图,使用原生sql查询数据
1.使用工具 https://www.hcharts.cn/ http://echarts.baidu.com/ 2.子表查询 id 创建时间 内容 处理者 1 2017-02-01 11:11 1 ...
- nginx在windows下配置反向代理
转自:https://blog.csdn.net/comeonyangzi/article/details/72466310 下载地址:http://nginx.org/download/ 下载后直接 ...
- 45. Jump Game II (JAVA)
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- hdu 4633 Who's Aunt Zhang(polya+逆元)
Who's Aunt Zhang Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- Tensorflow 2.0 学习资源
我从换了新工作才开始学习使用Tensorflow,感觉实在太难用了,sess和graph对 新手很不友好,各种API混乱不堪,这些在tf2.0都有了重大改变,2.0大量使用keras的 api,初步使 ...
- 35C3 CTF - Web - php
参考链接 https://ctftime.org/task/7404 题目描述 PHP's unserialization mechanism can be exceptional. 解题流程 程序代 ...
- springboot+HttpInvoke 实现RPC调用
开始用springboot2+hession4实现RPC服务时,发现第一个服务可以调用成功,但第二个就一直报 '<' is an unknown code.第一个服务还是可以调用的.参考网上的方 ...
- java环境contos上solr-5.5.0 安装部署
本人qq群也有许多的技术文档,希望可以为你提供一些帮助(非技术的勿加). QQ群: 281442983 (点击链接加入群:http://jq.qq.com/?_wv=1027&k=29Lo ...
- docker安装MySQL5.7示例!!坑
docker pull mysql 一.错误的启动 [root@localhost ~]# docker run ‐‐name mysql01 ‐d mysql 42f0981990 ...
- 模块打包 webpack
1.模块打包工具 2.工作方式: 1)将存在依赖关系的模块按照特定规则合并为单个JS文件,一次全部加载进页面中 2)在页面初始时加载一个入口模块,其他模块异步的进行加载 3.优势: 1)支持AMD,C ...