1

I have two arrays of objects in Java, which contain some fields that I need to compare, but the thing is I need to compare element by element, that means, that I want the compare a field from the first object in my first array with the first object in my second array, my second object from the first array with my second object from the second array and so on. This is what I have done so far, but the idea is that I do not know what should be the limit for my second array. From my point of view the second array should start from the index of the first array like this:

for(int i = 0; i < resultEntries.size(); i++) {
    for(int j = i; j < resultColorEntries.size(); j++) {
        if(resultEntries.get(i).getColor())...
    };
}

Another solution or a solution to my problem would be welcome. Thanks in advance!

4
  • Your second loop will make the second array run through all the elements in it from where the first array index is. When checking index 0 on Array 1, array 2 will be checked from index 0 to N. When checking index 1 on Array 1, Array 2 will be checked from index 1 to N. Commented Feb 22, 2017 at 8:10
  • Possible duplicate of How to compare two object arrays in Java? Commented Feb 22, 2017 at 8:12
  • Also, check this: stackoverflow.com/questions/6652085/… Commented Feb 22, 2017 at 8:12
  • Why do you want to compare all fields? And do you know all the fields at compile-time? Commented Feb 22, 2017 at 8:15

5 Answers 5

2

if you want to compare more efficiently with O(N), you need to implement your object hash(), and add your item to a hashset, example hasSetA for arrayA and hashsetB for your arrayB, then compare these two hashsets.

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

Comments

1

You can keep check of index that it is less than the size of both arrays.

for(int i=0;i<resultEntries.size() && i<resultColorEntries.size();i++){
             if(resultEntries.get(i).getColor()) {
             }
     }

Comments

1

You don't have to make 2 loops, you can achieve this only with one:

for(int i = 0; i < resultEntries.size() && i < resultColorEntries.size(); i++) {
    if(resultEntries.get(i).getColor().equals(resultColorEntries.get(i).getColor()) {
         // Same!
    }
}

Comments

0

that I want the compare a field from the first object in my first array with the first object in my second array, my second object from the first array with my second object from the second array and so on

You shouldn't have to write nested loop then. One loops will do.

Codes should be

for(int i=0;i<resultEntries.size();i++){

 if(resultEntries.get(i).getColor().equals(resultColorEntries.get(i).getColor()) )...

Assuming you have the same size of lists.

Comments

0

The whole thing only makes sense when both arrays have equal dimensions; so you could go for:

Whatever a[] = ...
Somthinelse b[] = ...

check for equal length

for (int i=0; i < a.length; i++) {
  if (a[i].whatever.equals(b[i].whatever))...

Point is: no need for two loops!

But: there is no nice way of generalizing that (like: writing a method that compares those arrays; but on different fields for example). Well, you could use method references, and using method references, ending up with something like

public void compareArraysOn(Whatever[] a, Other[] b, Function<T,U> extractorForA, ...) {

that could be called like compareArraysOn(a, b, Whatever::getColor(), SomethinElse::getColor())

Comments

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.