0

I have two lists which contain the same type of POJO Objects. But POJO objects of one of the list gets modified, so I should update other lists which also contains the same POJO object by comparing its name.

Example :

List 1 contains 100 cars..

List 2 also contains same 100 cars

(Model objects. But in list 2 some of the cars(Model) object gets modified. So need to update those values in List 1.

Instead of iteration is there any other way to do comparison efficiently and update POJO objects in another list

Community
  • 1
  • 1
  • 1
    is that two list will contain same elements always ? – RAJKUMAR NAGARETHINAM Apr 30 '19 at 13:30
  • Yes raj same POJO objects...which are having same set of parameters...(Example) List 1 contains ...5 POJO like Toyoto,Maruti,Amaze add List 2 also contains same POJO objects......But in List 2 Maruti has updated it's value ...like say engine type....This parameter comparision should happen with maruti object of List 1....How to do that comparision efficiently insttead of iteration – Anand Natarajan' Apr 30 '19 at 16:22

2 Answers2

0

If your lists will always contain same object, you catch a listener to update of first list and update the second list in that listener.

user699848
  • 109
  • 7
  • let me clarify further my question...List 1 contains POJO objects which belong to one system. Where as List 2 contains POJO Objects which belong to different system. My requirement if any update happens at SYSTEM 2 related to any POJO object that should be at SYNC at SYSTEM 1. Say for example in system 2 one car object has update on engine type , that engine type should be updated at SYSTEM 1 for that particular CAR object only ..based on name comparision – – Anand Natarajan' Apr 30 '19 at 14:08
  • In that case Listener pattern should work for you... SYSTEM1 can be listening to any updates that are happening on SYATEM2... get the update details and update itself accordingly – user699848 Apr 30 '19 at 15:01
0

You can have same reference objects like below but need more clarification from your end inorder to solve for good.

List<MyClass> list1=new List<>();
List<MyClass> list2=new List<>();
MyClass obj = new MyClass();
list1.add(obj);
list2.add(obj);

Here if you edit any value in obj this will be changed in both the list , since we have the reference.But i dont this is what you looking for.

RAJKUMAR NAGARETHINAM
  • 1,408
  • 1
  • 15
  • 26
  • let me clarify further my question...List 1 contains POJO objects which belong to one system. Where as List 2 contains POJO Objects which belong to different system. My requirement if any update happens at SYSTEM 2 related to any POJO object that should be at SYNC at SYSTEM 1. Say for example in system 2 one car object has update on engine type , that engine type should be updated at SYSTEM 1 for that particular CAR object only ..based on name comparision – Anand Natarajan' Apr 30 '19 at 14:07
  • Sorry the actual requirement is i have to compare each and every POJO object of two list(which are two different systems but having same model objects with same name) and do all parameters comparison..for each POJO object when case arise particular parameter of One POJO object has been modified in SYSTEM 2 , then i have to do compare same POJO object in System 1 by using name and then have to check whether that particular parameter has same updated value of SYSTEM 2 or not.... Is there any efficient way to do this kind of comparisons with minimal code.. – Anand Natarajan' Apr 30 '19 at 14:17
  • Adding example Suppose List 1 contains 5 cars toyoto,Maruti,Honda where as List 2 also contains same three cars with same...But at list 2 ...Maruti has updated one parameter...Now i want to compare Maruti POJO of system 1 (list1) with that of updated parameter of List 2 ... – Anand Natarajan' Apr 30 '19 at 14:20