-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;
Ondrej Tokar
  • 4,898
  • 8
  • 53
  • 103
  • 6
    There is no key in an `ArrayList` –  Oct 31 '14 at 12:30
  • 1
    Do you mean Hashmap or something? – brso05 Oct 31 '14 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? – Mureinik Oct 31 '14 at 12:31
  • 1
    Do you mean you have two instances of `ArrayList` and you want to join them, only adding duplicate values once? – blalasaadri Oct 31 '14 at 12:32
  • I know there is no key, only records in my arraylist have field which is unique key .. – Ondrej Tokar Oct 31 '14 at 12:34
  • 1
    So, you have some `Record` which has a key field? – blalasaadri Oct 31 '14 at 12:36
  • So how would you "join" two of these values? – David Conrad Oct 31 '14 at 12:36
  • This [post](http://javarevisited.blogspot.in/2014/02/10-example-of-lambda-expressions-in-java8.html) might help you in Java 8 using **lambda-expressions** – Braj Oct 31 '14 at 12:39
  • You're not going to be able to dynamically add fields. Or create classes with dynamic definition. That's not how java works. Unless Java 8 changed the playing field in a way I'm not aware of – Deltharis Oct 31 '14 at 12:52
  • check this http://stackoverflow.com/questions/9917787/merging-two-arraylists-into-a-new-arraylist-with-no-duplicates-and-in-order-in – Aman Arora Oct 31 '14 at 12:55
  • What is wrong with you? I don't want to concatenate the arraylists I want to merge them into one arraylist with fields from both arraylists. – Ondrej Tokar Oct 31 '14 at 13:29

4 Answers4

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.

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());
      }
Deltharis
  • 2,320
  • 1
  • 18
  • 29
  • +1 for the "design flaw". – Sufian Oct 31 '14 at 13:44
  • 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. – Ondrej Tokar Oct 31 '14 at 13:56
-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.
Community
  • 1
  • 1
BarrySW19
  • 3,759
  • 12
  • 26
-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.

jjlema
  • 850
  • 5
  • 8