2

Hy, I am having trouble with comparing two list. The goal is to compare two lists, find the same values and store those values into third list. What I have found till yet is just the way to return boolean value, but no way to return list value.. any ideas?

here are my two methods for getting values from database:

@Override
public List<BrojPolice> dohvatiBrojPolice() {
    List<BrojPolice> filter = jdbcTemplate.query("SELECT*FROM ins2.kalk_cpi;",new Object[]{},
            (rs,rowNum) ->{
                BrojPolice bp = new BrojPolice();
                bp.setBroj(rs.getString("BROJ_POLICE"));
                return bp;
            });
    return filter;
}


@Override
public List<BrojPolice> dohvatiBrojPolice2() {
    List<BrojPolice> filter2 = jdbcTemplate.query("SELECT*FROM ins_RAZNO.ADND_DATOTEKA;",new Object[]{},
            (rs,rowNum) ->{
                BrojPolice bp = new BrojPolice();
                bp.setBroj(rs.getString("BROJ_POLICE"));
                return bp;
            });
    return filter2;
}

public List<String> brojPolice(){
    boolean match = dohvatiBrojPolice().contains(dohvatiBrojPolice2());

     //ideas?

    return //List
}
Naman
  • 27,789
  • 26
  • 218
  • 353

3 Answers3

3

Instead of handling this in code you could write an SQL statement that gives the desired result

SELECT 
    BROJ_POLICE
FROM 
    ins2.kalk_cpi AS a
INNER JOIN 
    ins_RAZNO.ADND_DATOTEKA AS b
ON 
    a.BROJ_POLICE = b.BROJ_POLICE

That way you waste less memory on getting 2 possibly huge lists with only some common values.

// QUERY = query from above, omitted for readability
List<BrojPolice> list = jdbcTemplate.query(QUERY, new Object[0], 
    (rs,rowNum) -> {
            BrojPolice bp = new BrojPolice();
            bp.setBroj(rs.getString("BROJ_POLICE"));
            return bp;
    });

How to do it in java:

List<A> a = list1();
List<A> b = list2();
// A has to implement equals and hashCode
List<A> result = new ArrayList<A>();
for (A element : a) {
    if (b.contains(element)) // this requires a proper equals implementations
        result.add(element);
}

return result;

Note that doing list1.contains(list2) would always be false in this case because contains checks whether the element is contained.

Minn
  • 5,688
  • 2
  • 15
  • 42
1

You can use List.retainAll as :

List<BrojPolice> common = new ArrayList<>(dohvatiBrojPolice());
common.retainAll(dohvatiBrojPolice2());

This would require the BrojPolice to be comparable with proper hashCode and equals.

If you're looking for a stream or forEach solution for this

List<BrojPolice> common = dohvatiBrojPolice().stream()
                                     .filter(a -> dohvatiBrojPolice2().contains(a))
                                     .collect(Collectors.toList());
Naman
  • 27,789
  • 26
  • 218
  • 353
  • Additionally to @nullpointer's answer, you should also implement an "equals" method for the BrojPolice class for your objects to be comparable – NickAth Dec 05 '18 at 11:17
  • Is there any way to do it by using forEach? –  Dec 05 '18 at 12:02
  • @ZoranBajcer there is, I've updated the answer, but then given something can be completed using an inbuilt API, you shall stick to using it. **Aside**: your method should then also possibly have return type `List.` – Naman Dec 05 '18 at 12:15
1

First your BrojPolice class needs to have hashCode and equals method implemented so that contains method works as expected. Try below method then:

public List<BrojPolice> intersection(List< BrojPolice > list1, List<BrojPolice> list2) {
    List<BrojPolice> intersection = new ArrayList<BrojPolice>();

    for (BrojPolice bp : list1) {
        if(list2.contains(bp)) {
            list.add(bp);
        }
    }

    return intersection;
}
Aditya Narayan Dixit
  • 2,105
  • 11
  • 23