2

I have to display two lists, where li lists the old values and newli lists the new values. Based on this requirement, How do I insert unique values in mysql using java?

List<String> li =new ArrayList<String>();
        li.add("abcd");
        li.add("efgh");
        li.add("ijkl");
        List<String> newli=new ArrayList<String>();
        newli.add("mnop");
        newli.add("qrst");
        newli.add("uvwx");
        newli.add("yz");
        newli.add("abcd");

            Iterator it = newli.iterator();
            while(it.hasNext()){
                String str=(String) it.next();
                if(str.equals(li))
                {
                    System.out.println("This element Already present");
                    break;
                }
                else
                    System.out.println(str);


            }
            System.out.println("Already present");
        }

1 Answer 1

1
Set<String> uniqueSet = new HashSet<String>(li);
uniqueSet.addAll(newLi);

That will give you all the unique values from the two lists in one Set.

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.