Java Lambda List 转 Map

Posted 龙凌云端

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java Lambda List 转 Map相关的知识,希望对你有一定的参考价值。

Java Lambda List 转 Map  


 

在有些开发场景,需要对 List 对象列表进行过滤处理,并将有用的数据存放到Map中。

例如:告警对象,包含告警uuid(alarmUuid) 和 设备uuid(objUuid),需要对 objUuid = -1的告警进行过滤,并将过滤后告警数据的alarmUuid和 objUuid以键值对的形式保存到Map中。

 

1、告警对象定义如下:

/**
 * Created by Miracle Luna on 2020/3/16
 */
public class AlarmInfoResponse {
    private String alarmUuid;
    private String objUuid;

    public AlarmInfoResponse(String alarmUuid, String objUuid) {
        this.alarmUuid = alarmUuid;
        this.objUuid = objUuid;
    }

    public String getAlarmUuid() {
        return alarmUuid;
    }

    public void setAlarmUuid(String alarmUuid) {
        this.alarmUuid = alarmUuid;
    }

    public String getObjUuid() {
        return objUuid;
    }

    public void setObjUuid(String objUuid) {
        this.objUuid = objUuid;
    }

    @Override
    public String toString() {
        return "AlarmInfoResponse{" +
                "alarmUuid=‘" + alarmUuid + ‘‘‘ +
                ", objUuid=‘" + objUuid + ‘‘‘ +
                ‘}‘;
    }
}

 

2、过滤代码如下:

/**
 * Created by Miracle Luna on 2020/3/16
 */
public class LambdaFilterListToMap {

    public static void main(String[] args) {
        List<AlarmInfoResponse> alarmInfoResponseList = new ArrayList<>();
        AlarmInfoResponse response0 = new AlarmInfoResponse("alarm0", "-1");
        AlarmInfoResponse response1 = new AlarmInfoResponse("alarm1", "1");
        AlarmInfoResponse response2 = new AlarmInfoResponse("alarm2", "2");
        AlarmInfoResponse response3 = new AlarmInfoResponse("alarm3", "3");
        alarmInfoResponseList.add(response0);
        alarmInfoResponseList.add(response1);
        alarmInfoResponseList.add(response2);
        alarmInfoResponseList.add(response3);

        // 方式1:先使用foreach遍历(遍历过程中条件判断)
        Map<String, String> alarmObjUuidMap1 = new HashMap<>();
        alarmInfoResponseList.forEach(alarmInfoResponse -> {
            if(!"-1".equals(alarmInfoResponse.getObjUuid())) {
                alarmObjUuidMap1.put(alarmInfoResponse.getAlarmUuid(), alarmInfoResponse.getObjUuid());
            }
        });
        System.out.println("=============  方式1  ====================");
        alarmObjUuidMap1.forEach((alarmUuid, objUuid) -> System.out.println(alarmUuid + " : " + objUuid));


        // 方式2:使用流过滤,再使用foreach遍历
        Map<String, String> alarmObjUuidMap2 = new HashMap<>();
        alarmInfoResponseList.stream().
        filter(alarmInfoResponse -> !"-1".equals(alarmInfoResponse.getObjUuid())).
        forEach(alarmInfoResponse -> alarmObjUuidMap2.put(alarmInfoResponse.getAlarmUuid(), alarmInfoResponse.getObjUuid()));
        System.out.println("
=============  方式2  ====================");
        alarmObjUuidMap2.forEach((alarmUuid, objUuid) -> System.out.println(alarmUuid + " : " + objUuid));
    }
}

 

3、运行结果如下:

=============  方式1  ====================
alarm2 : 2
alarm1 : 1
alarm3 : 3

=============  方式2  ====================
alarm2 : 2
alarm1 : 1
alarm3 : 3

 

以上是关于Java Lambda List 转 Map的主要内容,如果未能解决你的问题,请参考以下文章

java map对象转换成list

java中怎么list 转成 map

java中怎么list 转成 map?

关于java的容器类 list,map等的toString()方法

java:如何删掉list里面的重复的map?

java中map可以放在list里吗