-3

I'm facing a small issue. I want to concat two or more object attributes if they have attributes in common. fusionAgenda is my arrayList where I want to compare objects and then concat. appointments is my final arrayList, where I want to add the result of the concatenation.

for(PSTAppointment fusion : fusionAgenda){
  System.out.println("------------------");
  System.out.println(fusion.getSubject() + " / " + fusion.getDisplayTo() );
  for(PSTAppointment fusion2 : fusionAgenda){
    if(fusion.getSubject().equals(fusion2.getSubject()) &&
       fusion.getStartTime().equals(fusion2.getStartTime()) &&
       fusion.getLocation().equals(fusion2.getLocation()) &&
       !fusion.getDisplayTo().contains(fusion2.getDisplayTo()) ) {

      fusion.setlisteFusion(fusion.getDisplayTo() + "; "+ fusion2.getDisplayTo());
    }
  }
  System.out.println(fusion.getListeFusion());
  appointements.add(fusion);
}

Here's my fusionAgenda data:

  • Evenement 1 / Personne 1
  • Evenement 1 / Personne 2; Personne3
  • Evenement 2 / Personne 4
  • Evenement 2 / Personne 5
  • Evenement 2 / Personne 6
  • Evenement 2 / Personne 7

Expected result:

  • Evenement 1 / Personne 1; Personne 2; Personne 3
  • Evenement 2 / Personne 4; Personne 5; Personne 6; Personne 7

Actual result:

  • Evenement 1 / Personne 1; Personne 2; Personne 3
  • Evenement 1 / Personne 2; Personne 3; Personne 1
  • Evenement 2 / Personne 4; Personne 5
  • Evenement 2 / Personne 6; Personne 5
  • Evenement 2 / Personne 7; Personne 5
  • Evenement 2 / Personne 5; Personne 7

2 Answers2

0

In PSTAppointment create method which compares two PSTAppointment objects. This will be easier. For example:

    Map<PSTAppointment,PSTAppointment> groupedAppointments = fusionAgenda.stream().collect(Collectors.groupingBy(
                                                             PSTAppointment::getSubject,
                                                             PSTAppointment::getStartTime,
                                                             PSTAppointment::getLocation)
Rafalsonn
  • 430
  • 7
  • 23
0

This is what i've done to finally make this work :

for(PSTAppointment fusion : fusionAgenda){
              System.out.println("------------------");
              System.out.println(fusion.getSubject() + " / " + fusion.getDisplayTo() );
              if(appointements.contains(fusion)) {continue;}
              String attendees = fusion.getDisplayTo()+";";
              for(PSTAppointment fusion2 : copy)
                {
                if(fusion.getSubject().equals(fusion2.getSubject()) && fusion.getStartTime().equals(fusion2.getStartTime()) &&
                   fusion.getLocation().equals(fusion2.getLocation()) && !fusion.getDisplayTo().equals(fusion2.getDisplayTo()) ) {
                     attendees += fusion2.getDisplayTo() +";";
                }
              }
              fusion.setlisteFusion(attendees);
              appointements.add(fusion);
            }