-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
15
  • Please share the code for PSTAppointment Commented Aug 10, 2018 at 10:36
  • github.com/rjohnsondev/java-libpst/blob/develop/src/main/java/… I've added manually an attribute (listeFusion, so setListeFusion) Commented Aug 10, 2018 at 10:40
  • You don't check if that eveniment was already compared. I suggest to check first if that eveniment don't exist in final list. Commented Aug 10, 2018 at 10:41
  • What line d I need to change ? Commented Aug 10, 2018 at 10:46
  • In first for, before the second for, add an if which check if "fusion.evenement" isn't already in final list, and if it is, you go to next fusion. Atleast you want to group all persons by evenement. Commented Aug 10, 2018 at 10:50

2 Answers 2

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)
Sign up to request clarification or add additional context in comments.

4 Comments

How can I use this after ? (sorry, I'm very new to java...)
No problem, Map it's a key value list, when You will ask for key using object it will return value assigned to that key
So this goes into PSTAppointment, but on my main, how do I get this Map ?
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);
            }

Comments

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.