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.

Sangram Anand
  • 10,526
  • 23
  • 70
  • 103

2 Answers2

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.

Zaw Than oo
  • 9,651
  • 13
  • 83
  • 131
  • 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 Nov 02 '12 at 06: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() ? – Sangram Anand Nov 02 '12 at 07: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 Nov 02 '12 at 07:22
  • @Stivel, I told that, it is depend on requirement. Normally, we put all of fields. – Zaw Than oo Nov 02 '12 at 07:27
  • @CycDemo his requirement is merging two `Topic` objects based on some comparison not merging to Topic Lists. – vels4j Nov 02 '12 at 07:34
  • actually, when i tried the Hashset and addAll() method, the objects within the List are not updating, rathing more objects are added i.e., if the initial list has 10 Topic objects and if i modify values of 3 objects, then the update list has 13 objects instead of 10. – Sangram Anand Nov 02 '12 at 07:49
  • 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: http://stackoverflow.com/questions/27581/overriding-equals-and-hashcode-in-java) – Axel Nov 02 '12 at 08:51
  • Ok, thanks for the link, will look into it and update the status shortly. – Sangram Anand Nov 02 '12 at 12:20
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

vels4j
  • 11,208
  • 5
  • 38
  • 63