0

I want to compare 2 array of different dimension and then eliminate the duplicate and put the result on array called tmp

Here is the code

ArrayList<String> list = new ArrayList<String>();
HashSet<String> tmp = new HashSet<String>();

try 
{
    String query1="SELECT ID FROM Apps;";
        ResultSet rs = con.createStatement().executeQuery(query1);
        while(rs.next())
        {
            list.add(rs.getString("ID"));
            tmp.add(rs.getString("ID"));
        }

        for(int i=0;i < check.size();i++)//ciclo  le checkbox selezionate
        {
                check.get(i);
                String query="UPDATE Apps SET Authorized='1' WHERE ID=" +check.get(i);//vado ad eseguire la query di update
                pr=con.prepareStatement(query);
                pr.executeUpdate();
                tmp.add(check.get(i).toString());
        }

        System.out.println(tmp);

The content of the two arrays is:

check -> [1, 2]
list -> [1, 2, 3, 4, 5]

The result I want is tmp -> [3,4,5]

but the console show tmp -> [1,2,3,4,5]

5
  • try deleting the = in j<=check.size() Commented Jan 8, 2019 at 21:22
  • @Nosrep ok, i try but it doesn't work Commented Jan 9, 2019 at 8:02
  • Assuming check Always contains elements present in tmp, iterate over the elements in check and remove these from tmp. Commented Jan 9, 2019 at 9:04
  • @trappski please give me the code! Commented Jan 9, 2019 at 9:40
  • 1
    @lucapellegrini I suggest you check the answer posted by P.Raber and work from there. It's a clean an simple solution. Commented Jan 9, 2019 at 9:45

3 Answers 3

2

You could add everything to a hashSet

    HashSet<String> h = new HashSet<String>(); 

    // Adding elements into HashSet usind add() 
    h.add("Cats");
    h.add("Cats"):
    h.add("Dogs"); 

    System.out.println("List:" + h); 

Output

List:[Dogs, Cats]

The hash set won't store any of the duplicates.

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

1 Comment

I try, but it show me tmp -> [1,2,3,4,5] when i want tmp->[3,4,5]
1

You can remove duplicates like this:

    public static void main(String[] args) {

    Set<Integer> tmp = new HashSet<>();
    List<Integer> check = new ArrayList<>();
    check.add(1);
    check.add(2);
    List<Integer> list = new ArrayList<>();
    for(int i = 1; i <= 5; i++){
        list.add(i);
    }

    tmp.addAll(list);
    tmp.removeAll(check);
    System.out.println(tmp);
}

1 Comment

Thanks!! This is the solution
-1

Try this

public static void main(String[] args)
  {
    List<String> one = new ArrayList<>();
    one.add("one");
    one.add("two");
    one.add("three");
    one.add("in one");
    one.add("in two");

    List<String> two = new ArrayList<>();
    two.add("zero");
    two.add("one");
    two.add("two");
    two.add("four");
    two.add("not in one");
    two.add("in one");
    two.add("in two");

    // Depending on your needs create new or modify existing like one.addAll(two) and stream() from that.
    List<String> unique = one;
    unique.addAll(two);
    unique = unique.stream().distinct().collect(Collectors.toList());

    System.out.println("Unique list: " + unique.stream().collect(Collectors.joining(",")));
    // Unique list: one,two,three,in one,in two,zero,four,not in one
  }

1 Comment

I want to delete the duplicate in array unique.. the code doesn't work well

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.