-2

I am having 2 arraylists with one key field. Is there any smart way how to join values from one arraylist with values from another arraylist based on the key value?

Problem is that sometimes one arraylist is bigger and contains keys which the other doesn't and sometimes its reversed.

Should I just loop through them, compare the fields etc? Or is there anything better?

By key I mean ArrayList<Record>

Where Record contains:

String blabla;
String keyOrWhatever... just some unique name;

And then there is another ArrayList<Record2>

Where Record2 contains:

String differentbleble;
String theSameKeyASRecord;

Sometimes one List or another might have different amount of values.

The result I imagine to have is Result3

Having fields:

String differentbleble;
String blabla;
String keyOrWhatever... just some unique name;
11
  • 6
    There is no key in an ArrayList Commented Oct 31, 2014 at 12:30
  • 1
    Do you mean Hashmap or something? Commented Oct 31, 2014 at 12:31
  • 1
    What exactly do you mean when you refer to keys in an ArrayList? An ArrayList doesn't have keys - are you perhaps referring to indices? Could you share some code to make the question clearer? Commented Oct 31, 2014 at 12:31
  • 1
    Do you mean you have two instances of ArrayList<Key> and you want to join them, only adding duplicate values once? Commented Oct 31, 2014 at 12:32
  • 1
    So, you have some Record which has a key field? Commented Oct 31, 2014 at 12:36

4 Answers 4

0

The problem probably lies with your Record classes. It indicates some serious design flaw if you have two different classes with different fields that are connected by the same key.

But ok, let's work with it. The only solution that comes to mind is:

for(record1 in record1ArrayList)
   for(record2 in record2ArrayList)
      if(record1.getKey().equals(record2.getKey()){
         outputArrayList.put(new Record3(record1.getKey(), 
                                      record1.getFieldValue(), record2.getAnotherFieldValue());
      }
Sign up to request clarification or add additional context in comments.

2 Comments

+1 for the "design flaw".
That's how I did it. Design flow is like that because I load records from 2 files into 2 different arrays. It must be like that. Since I did what you have written, even though it was before you have written ... I mark it as an answer. Thank you.
0

You can use indexOf and equals method of Arraylist. Say for example, you have recordArrayList, record2ArrayList and you want to get record3ArrayList.

Now, You need to make one loop for recordArrayList as below.

for(Record recordObject: recordArrayList){
    Record2 tmpRecord2 = new Record2();
    record2.setTheSameKeyAsRecord(recordObject.getTheKeyWhateveer());
    Record2 actualRecord2Object= record2ArrayList.indexOf(tmpRecord2);  // This will return the actual object from the list. But you need to implement the equals method inside Record2.java. If there is no corresponding object then it returns null. So handle it and continue for the loop.

// Now you have both recordObject and actualRecord2Object. You can create object of Record3 and put inside the list.
}

So at the end of the loop, you would have record3ArrayList which would contains the instance of Record3. If you want to remove the same key object from recordArrayList and record2ArrayList then you can do in above for loop after creating Record3.

Comments

-1

You can create a set joining the two arraylist, because set removes duplicates, but you need to modify your record adding equals and hashcode in function of the "key" field.

1 Comment

Not an answer, question wants merging instances, not replacing one instance with another
-1

Assuming your objects implement hashCode() and equals() correctly then just use a set:

Set<Record> allItems = new HashSet<>();
allItems.addAll(arrayList1);
allItems.addAll(arrayList2);
// .. allItems contains a unique set of items from the two lists.
// Convert back to an ArrayList with List<Item> newList = new ArrayList<>(allItems);
// if you like.

2 Comments

Not an answer, question wants merging instances, not replacing one instance with another
Is it really that hard to imagine doing "arrayList1.clear(); arrayList1.addAll(allItems);"?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.