Say we have this method which is my main concern for this question. It's really not optimal way to get values out of it. Each time I'm running through list twice for to just get one field value for NotificationSettingsDtoprivate NotificationSettingsDto notificationSettingsCustomPairToDto(List<CustomPair> notificationSettingsCustomPair) {    return NotificationSettingsDto.builder()            .campaignEvents(notificationSettingsCustomPair.stream()                    .anyMatch(customPair -> customPair.getKey().equals(campaignEvents)) ?                    (Boolean) notificationSettingsCustomPair.stream().filter(customPair -> customPair.getKey().equals(campaignEvents)).collect(Collectors.toList()).get(0).getValue() : null)            .drawResultEvents(notificationSettingsCustomPair.stream()                    .anyMatch(customPair -> customPair.getKey().equals(drawResultEvents)) ?                    (Boolean) notificationSettingsCustomPair.stream().filter(customPair -> customPair.getKey().equals(drawResultEvents)).collect(Collectors.toList()).get(0).getValue() : null)            .transactionEvents(notificationSettingsCustomPair.stream()                    .anyMatch(customPair -> customPair.getKey().equals(transactionEvents)) ?                    (Boolean) notificationSettingsCustomPair.stream().filter(customPair -> customPair.getKey().equals(transactionEvents)).collect(Collectors.toList()).get(0).getValue() : null)            .userWonEvents(notificationSettingsCustomPair.stream()                    .anyMatch(customPair -> customPair.getKey().equals(userWonEvents)) ?                    (Boolean) notificationSettingsCustomPair.stream().filter(customPair -> customPair.getKey().equals(userWonEvents)).collect(Collectors.toList()).get(0).getValue() : null)            .build();}And I'm passing following List which consists of CustomPairList<CustomPair> notificationSettingsCustomPair = Arrays.asList(        new CustomPair<>(campaignEvents, true),        new CustomPair<>(drawResultEvents, true),        new CustomPair<>(transactionEvents, true),        new CustomPair<>(userWonEvents, true));List can also have different shapes for example like this :   List<CustomPair> notificationSettingsCustomPair = Arrays.asList(        new CustomPair<>(campaignEvents, true),        new CustomPair<>(transactionEvents, false));Or like this, you get the idea it can be in any order, with any of these 4 different fields:     List<CustomPair> notificationSettingsCustomPair = Arrays.asList(        new CustomPair<>(transactionEvents, true));Model for CustomPair :public class CustomPair<K, V> {private K key;private V value;}Model for NotificationSettingsDto : @Builderpublic class NotificationSettingsDto {private Boolean campaignEvents;private Boolean drawResultEvents;private Boolean transactionEvents;private Boolean userWonEvents;}
Building object from unusually custom object
java;performance;object oriented