1

I have two Array list say currentTopicsDetailsList and updatedTopicsDetailsList. The Lists are of object Topic(class) i.e.,

List<Topic> currentTopicsDetailsList;
List<Topic> updatedTopicsDetailsList; 

The class Topic has properties

  • rank
  • socialIndex
  • twitterIndex
  • newsIndex

I want to update the values in currentTopicsDetailsList with updatedTopicsDetailsList values
e.g. at

  • currentTopicsDetailsList[4].rank=158
  • currentTopicsDetailsList[4].socialIndex= +245
  • currentTopicsDetailsList[4].twitterIndex=-345
  • currentTopicsDetailsList[4].newsIndex=+340

  • updatedTopicsDetailsLIst[4].rank=null

  • updatedTopicsDetailsLIst[4].socialIndex= 300
  • updatedTopicsDetailsLIst[4].twitterIndex=-56
  • updatedTopicsDetailsLIst[4].newsIndex=+340

I want to overwrite the currentTopicsDetailsList[4] with updatedTopicsDetailsList[4] with a condition that whichever has null should be ignore i.e., rank property should be 158 eventhough its null in updatedTopicsDetailsList[4].rank

Right now am doing it with FOR loop index level comparison for null & empty strings, but is there a alternative and quick way of doing things.

2 Answers 2

3

Simply put a method in Topic class to update

public void update(Topic updatedTopic){
   if(updatedTopic.getRank()!=null){
      this.rank = updatedTopic.getRank();
   }
   // similarly u can check others
}

and for merging to list you can do like

int size = currentTopicsDetailsList.size();
for(int i=0;i<size;i++) {
  Topic uT = updatedTopicsDetailsLIst.get(i);
  Topic cT = currentTopicsDetailsList.get(i);
  cT.update(uT); 
}

Make sure both list contain ordered same Topic

2
  • Thanks, but initially i am doing the same. Commented Nov 2, 2012 at 7:18
  • Well, since its a urgent fix, i am sticking to the initial one, which is also mentioned by you. So, i am hoping its a genuine way of doing even in production environment scenario. Thanks. Commented Nov 2, 2012 at 10:56
3

Just use Set/HashSet. Convert one of list to Set. And use addAll method.

Note Make sure override equals and hashcode method of Topic.

List<Topic> currentTopicsDetailsList;
List<Topic> updatedTopicsDetailsList; 
HashSet<Topic> set = new HashSet<Topic>(currentTopicsDetailsList);
set.addAll(updatedTopicsDetailsList);

updatedTopicsDetailsList = new ArrayList<Topic>(set);

Update Comment

 do i have to include all the fields for generating equals() & hascode() ?

Sorry, I am not sure, all fields for generating equals() & hascode() because of it is depend on your requirement.

For example :

Topic have a field name with id. As two Topic instances have same id, if we assume these two instance are same, you just need to put id into equals() & hascode(). The rest of fields don't need to put.

8
  • 1
    +1 for possibly understanding what the OP was asking for. You should also mention that in addition to equals, hashcode should be overridden too.
    – Axel
    Commented Nov 2, 2012 at 6:54
  • @CycDemo, thanks for the details info, as per the NOTE- i need to override equals & hashcode, do i have to include all the fields for generating equals() & hascode() ? Commented Nov 2, 2012 at 7:16
  • @CycDemo i am not sure how this answer will solve problem, usually set.addAll(anotherset) will invoke equals method and add missing object. if duplicates found will simply reject.
    – vels4j
    Commented Nov 2, 2012 at 7:22
  • @Stivel, I told that, it is depend on requirement. Normally, we put all of fields. Commented Nov 2, 2012 at 7:27
  • 1
    @SangramAnand Taht would be the case if the objects being added are not equal to any of those already contained in the set, or if you break the contract for hashcode and equals. (Read on here: stackoverflow.com/questions/27581/…)
    – Axel
    Commented Nov 2, 2012 at 8:51

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.