Java Lambda List 转 Map

Java Lambda List 转 Map  


 

In some development scenarios, the need for filtering on the List list object and store useful data to the Map.

For example: the alarm object, comprising an alarm uuid (alarmUuid) and equipment uuid (objUuid), alarm objUuid = -1 needs to be filtered, and alarmUuid objUuid and stored in the form of key-value pairs after filtration alarm data to the Map.

 

1, the alarm object is defined as follows:

/**
 * 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, the filter code is as follows:

/**
 * 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("\n=============  方式2  ====================");
        alarmObjUuidMap2.forEach((alarmUuid, objUuid) -> System.out.println(alarmUuid + " : " + objUuid));
    }
}

 

3. The results are as follows:

============= embodiment ====================. 1 
Alarm2: 2 
ALARM1: . 1 
alarm3: . 3 

====== ======= embodiment ==================== 2 
Alarm2: 2 
ALARM1: . 1 
alarm3: . 3

 

Guess you like

Origin www.cnblogs.com/miracle-luna/p/12508281.html