1

I have two arraylist name preBusinessList, businessList. In business List I have data from server, and in preBusinessList is the local one. In lists I have id, count value Betterly demonstrate as below

enter image description here

Now I wanted to make a newBusinessList like this

image 2

How can I do it in java, please help me to solve this

5
  • 4
    Question is unclear. Do you want to take the count from the server? Or the sum of both? Commented May 4, 2013 at 8:31
  • I want to only merge these two lists, based on when I not have data in businesslist than take data from preBusinessList. Commented May 4, 2013 at 8:32
  • on what criteria do you want to do the merge? On list index? on value? from your example it seems to be none of these. so could you clarify the merge criteria. Commented May 4, 2013 at 8:36
  • based on Id, and Index. Commented May 4, 2013 at 8:37
  • Actually, I want to put value from preBusinesslIst when I am not getting value from businessList on a particular index. Commented May 4, 2013 at 8:41

5 Answers 5

4

Then I would use a map to do the merge using id as the key and convert it back to your list of (id,value) pairs

Sign up to request clarification or add additional context in comments.

3 Comments

I agree with you, mine solution below wouldn't remove duplicates.
can you please give me some example code because I am really stuck on my end.
@user900779 - something like my answer i think
1

You can use:

Collections.sort(new ArrayList<...>(preBusinessList).addAll(businessList), comparator)

Where comparator is a class that implements Comparator interface (will be responsible for sorting as you wish)

4 Comments

can you please give me the implementation of comparator.
but he wants items from businessList to "overwrite" those from preBusinessList
@user900779 Please tell me what the type of ArrayList is (is it a String ?)
it's a class model <placeItems> public class PlaceItems { public final static String BUSINESS_ID = "id"; public final static String BUSINESS_RESULT = "count"; public String businessId, businessResult; }
0

assumming i understood your problem correctly (big if...):

also, i assume each element in the lists is a Pair - as it looks from your data (just a dumb wrapper class that holds 2 integers). if its some other class you'll need to adjust this code.

private Map<Integer,Integer> finalValues = new HashMap<Integer,Integer>();

for (Pair<Integer,Integer> entry : preBusinessList) {
    finalValues.put(entry.getFirst(), entry.getSecond());
}
//2nd list overwrites values from 1st (anything not overwritten remains)
for (Pair<Integer,Integer> entry : businessList) {
    finalValues.put(entry.getFirst(), entry.getSecond());
}
ArrayList<Pair<Integer,Integer>> finalList = new ArrayList<>();
for (Map.Entry<Integer,Integer> entry : finalValues) {
   finalList.add(new Pair(entry.getKey(), entry.getValue());
}
//and now sort the list
Collections.sort(finalList, new Comparator<Pair<Integer,Integer>> {
    int compare(Pair<Integer,Integer> a, Pair<Integer,Integer>b) {
       return a.getFirst.compareTo(b.getFirst()); //compare by 1st number in pair only
    }
});

5 Comments

what is entry.getFirst() and getSecond?
@user900779 getter methods for the class that holds 2 integers. im assumming you use something like it in your lists since each entry in your list has 2 values - so its definitely not a lit of simple objects
I am using this class and have only two variables in it. public class PlaceItems { public final static String BUSINESS_ID = "id"; public final static String BUSINESS_RESULT = "count"; public String businessId, businessResult; }
I am getting error in eclipse at these two lines I change your code according to my class implementation: ArrayList<PlaceItems> finalList = new ArrayList<PlaceItems>(); for (Map.Entry<String,String> entry : finalValues) { finalList.add(new PlaceItems(entry.businessId, entry.businessResult); } //and now sort the list Collections.sort(finalList, new Comparator<PlaceItems> { int compare(Pair<Integer,Integer> a, Pair<Integer,Integer>b) { return a.getFirst.compareTo(b.getFirst()); //compare by 1st number in pair only } });
@user900779 - the compare() method for a Comparator<PlaceItems> would be public int compare (PlaceItems a, PlaceItems b). you forgot to update it
0

Assuming something like:

public class Info {
    public int id;
    public int info;
}

You could merge them on the basis of wanting the keep the one with higher info field as follows:

// Assumes:
// - that the ArrayLists are sorted to have id in order going up
// - no repeated ids in a or in b (but same id can be in both a and b)
ArrayList<Info> merge(ArrayList<Info> a, ArrayList<Info> b) {
    int aLength = a.size();
    int bLength = b.size();

    int ai = 0;
    int bi = 0;

    ArrayList<Info> result = new ArrayList<Info>();

    while ((ai < aLength) && (bi < bLength))
        Info aInfo = a.get(ai);
        Info bInfo = b.get(bi);
        if (aInfo.id == bInfo.id) {
            if (aInfo.info >= bInfo.info) result.add(aInfo);
            else                          result.add(bInfo);
            ai++;
            bi++;
        }
        else if (aInfo.id < bInfo.id) {
            result.add(aInfo);
            ai++;
        }
        else  {
            result.add(bInfo);
            bi++;
        }
    }

    // Add the remaining terms - only one of the loops will actually do anything
    for (; ai<aiLength; ai++) {
        result.add(a.get(ai));
    }
    for (; bi<biLength; bi++) {
        result.add(b.get(bi));
    }
}

Comments

0

Pseudocode :

  1. Iterate over preBusinessList.

  2. Fetch key and see if this key(1,2,3,4,5,6) exists in businesslist

  3. If yes conitnue

  4. Else If no, then add it to businesslist

    for(Map.Entry<Integer, Integer> keyValue : preBusinessList.entrySet()) {
        if(!businesslist.containsKey(keyValue.getKey())) {
            businesslist.put(keyValue.getKey(), keyValue.getValue());
        }
    }
    

Updated Answer as per new requirements

boolean ifExists = false;
for(PlaceItems itemPreBusinessList : preBusinessList) {
   ifExists = false;
   for(PlaceItems itemBusinessList : businessList) {
    if(itemBusinessList.businessId == itemPreBusinessList.businessId) {
        // Already exists
        ifExists = true;
        break;
    }
   }

   if(!isExists) {
    businessList.add(itemPreBusinessList);
   }
 }

6 Comments

Hi Anirudh, I have a Class for Arraylist Items name <placeItems>. Which have two String variables
public class PlaceItems { public final static String BUSINESS_ID = "id"; public final static String BUSINESS_RESULT = "count"; public String businessId, businessResult; }
Can you suggest me what should I write instead of Mao.Entry<Integer, Integer>
Any specific reason for doing this? This will degrade performance of your app since for every item in list you will need to create a separate PlaceItems object.
actually this class holds many variables in future that's why I am using this.
|

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.