0

I have 2 lists in java, and I need to validate when there is a match, I set a field of the first list, I tried to do it with streams, but I don't know how to compare the two lists, list don't have same kind of elements I want to do something like this:

public static List<TransactionalityIdDBDTO> getVariationDateRange(List<TransactionalityIdDBDTO> list1,
                                                                      List<TransactionalityIdDBDTO> list2){
        List<TransactionalityIdDBDTO> idDBDTOS= new ArrayList<>();
        Iterator prueba=list1.iterator();

        while(prueba.hasNext()){
            TransactionalityIdDBDTO transactionalityIdDBDTO=(TransactionalityIdDBDTO) prueba.next();
            if(transactionalityIdDBDTO.get_id().equals(list2.get_id())){
                transactionalityIdDBDTO.setVariation("1232");
                idDBDTOS.add(transactionalityIdDBDTO);
            }
        }
        return idDBDTOS;
    }

Clase TransactionalityIdDBDTO

public class TransactionalityIdDBDTO extends AbstractDTO {
    private TransactionalityDBDTO _id;
    private String totalTransaction;
    private List<String> idResult;
    private String variation;
3
  • What is list2.get_id() supposed to mean? Commented Feb 4, 2022 at 16:19
  • list2.get_id() It's a String Commented Feb 4, 2022 at 17:01
  • 1
    List has no such method. If you're trying to find an element in the list, you'll need to iterate over it. Commented Feb 4, 2022 at 18:41

1 Answer 1

2

You can compare lists using equals. e.g

list1.equals(list2);

However,

  • order is important. If lists contain the same content but in different order they will be considered unequal.
  • the objects the lists hold must override equals. The criteria for what constitutes two objects of TransactionalityIdDBDTO to be equal are up to you. Based on your code, it seems that id is a good start.
  • if you plan on using those objects as keys in a map, you must override hashCode too (it's a good idea to do this anyway).
  • if the lists are not in order but you want to consider them equal, then you should probably sort them first based on the id. You can do that like this.
list1.sort(Comparator.comparing(TransTransactionalityIdDBDTO::get_id));
list2.sort(Comparator.comparing(TransTransactionalityIdDBDTO::get_id));

Objects like String, Integer, and other wrapper classes implement Comparable which means they can be compared to each other of the same type using the above. But instead of get_id which returns a String, say you had get get_Foo. Then the Foo class would need to implement comparable or you would have to provide a more detailed comparator on which to sort.

There are many examples of Comparing objects on the site. I recommend you search them using the [hashCode], [equals], [comparable] and [comparator] tags. Imo, understanding these and how they work are essential for a reasonable working knowledge of Java.

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

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.