Vini
Vini

Reputation: 2134

Comparing List<mylist> by elements

I have two enum lists. I want to search for a string value from List in the other.

public class AzureFileModel {
    public String imageName;
    public String classifierClass ="";
    public double score;
}
public class WatsonFileModel {
    public String imageName;
    public String classifierClass ="";
    public double score;
}
private List<WatsonFileModel> watsonFileModelList = new ArrayList<>();    
private List<AzureFileModel> azureFileModelList = new ArrayList<>();

How do I compare both the lists for same image name in an efficient way??

Current approach

I have a list of all the image names from each list and I am checking if the image name matches the other by iterating through one list.

Example Data

AzureFileModelList
{
  [imagename=image1,class=football,score=9.87],
  [imagename=image2,class=tennis,score=9.7],
  [imagename=image1,class=tennis,score=0.87] //chances of same imagename
  [imagename=image3,class=football,score=9.87],  
}
WatsonFileModelList
{
  [imagename=image1,class=football,score=5.6],
  [imagename=image2,class=football,score=9.7],
  [imagename=image4,class=tennis,score=0.87]
}

End result

CombinedModelList{
 [imagename=image1,azureclass=football,azurescore=9.87,watsonclass=football, watsonscore =5.6],
 [imagename=image2,azureclass=tennis,azurescore=9.7,watsonclass=football,watsonscore=9.7]
}

Upvotes: 0

Views: 73

Answers (2)

OneCricketeer
OneCricketeer

Reputation: 191743

Why do you need two classes with the exact same fields?

Make both one class. Then store instances of Azure or Watson data in one list

If you need differences in the classes, you use an abstract class for the common fields

If you want to compare the objects, you might want to try implementing the Comparable interface

Comparing images requires reading binary data, not just a filename

Upvotes: 2

davidxxx
davidxxx

Reputation: 131346

You could use a Map instead of a List and specify as key a String that represents imageName.
Map retrievals are fast.

Note that it may also be a good thing to introduce a parent class to these two classes in order to manipulate them in an uniform way :

Map<String, FileModel> fileModelsByImageName;

If the order of element insertion matters, you could instantiate a LinkedHashMap.
Otherwise, a HashMap would be enough.

Upvotes: 0

Related Questions