0

I have POJO List<TravelRequestDTO> and I want to group and create filtered List<TravelRequestDTO> if leavingFrom,goingTo,onwarDate,returnDate are same add passenger to same object

Example :

Passanger,     onWard,      return,      leavingFrom,     goingTo

  A,            1-2-20,      3-2-20,        BLR,            PUNE

  B,            1-2-20 ,     3-2-20,        BLR,            PUNE

final List<TravelRequestDTO> should contain :

Passanger,     onWard,      return,      leavingFrom,     goingTo

  A,B          1-2-20      3-2-20        BLR            PUNE
public class TravelRequestDTO {

    private List<Pax> passangers; 
    private String leavingFrom;
    private String goingTo;
    private String onwarDate;
    private String onwardTime;
    private String returnDate;
    private String returnTime;
    private SegmentTypeEnum segmentType;
    private TravelModeEnum travelMode;
    private String purposeOfVisit;
     }


    public class Pax{

    private String name;
    private String age;
    private String mobile;
    }
3
  • What is your question? Commented Feb 4, 2020 at 5:01
  • How do you get Pax object, where does DTO stores information of First passenger Commented Feb 4, 2020 at 5:02
  • I guess former List<TravelRequestDTO> and later List<TravelRequestDTO> names must be different. Commented Feb 4, 2020 at 7:58

2 Answers 2

1

If you need older java version, than you can do like this:

Map<Object, List<TravelRequestDTO>> hashMap = new HashMap<Object, List<TravelRequestDTO>>();
for (TravelRequestDTO value: initList) {
    List<Object> key = Arrays.asList(value.getOnWard(),value.getReturn(),value.getLeavingFrom(),value.getGoingTo());
    if (!hashMap.containsKey(key)) {
        List<TravelRequestDTO> list = new ArrayList<TravelRequestDTO>();
        list.add(value);
        hashMap.put(key, list);
    } else {
        hashMap.get(key).add(value);
    }
}

Check this question for other solution.

It is only half way to what you want. After that you have to extract final result from this map. Or you can do it in one step:

Map<Object, TravelRequestDTO> hashMap = new HashMap<Object, TravelRequestDTO>();
for (TravelRequestDTO value: initList) {
    List<Object> key = Arrays.asList(value.getOnWard(),value.getReturn(),value.getLeavingFrom(),value.getGoingTo());
    if (!hashMap.containsKey(key)) {
        TravelRequestDTO item = value; // pass first value or copy it to new
        hashMap.put(key, item);
    } else {
        hashMap.get(key).getPassangers().addAll(value.getPassangers());
    }
}
List<TravelRequestDTO> result = new ArrayList<>(hashMap.values());
Sign up to request clarification or add additional context in comments.

1 Comment

@abhinandanabhinandan then you can vote up this answer :)
1

you can use the below code to get a List of TravelRequestDTO objects based on properties.

Function<TravelRequestDTO, List<Object>> compositeKey = travelRecord ->
    Arrays.<Object>asList(travelRecord.getOnWard(),travelRecord.getReturn(),travelRecord.getLeavingFrom(),travelRecord.getGoingTo());

Map<Object, List<TravelRequestDTO>> map =
people.collect(Collectors.groupingBy(compositeKey, Collectors.toList()));

2 Comments

Thanks for answer, but we need answer for java 1.7
Please any one ? help me

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.