ArrayList trick

I have two Strings .

d1="40-60-70"; // the data are id values basically

d2="60-70-80"; // the data are id values basically

I want to make an ArrayList with id's which are not present in d2 but in d1.

So, my expected output is ArrayList {"40"}

whats the easiest way to do this ?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

You can use the following,
1. Split the string by "-", So you will get an array
2. Sort the array
3. Iterate over array to find out the missing element(s)
1. Tokenize str1 and store in arrayList1
2. Tokenize str2 and store in arrayList2
3. for each token in arrayList1 check arrayList2.contains(token)
4. for each token in arrayList2 check arrayList1.contains(token)
5. Do processing accordingly

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Check if this helps!

import java.util.Set;
import java.util.TreeSet;
public class Main {
  public static <T> Set<T> union(Set<T> setA, Set<T> setB) {
    Set<T> tmp = new TreeSet<T>(setA);
    return tmp;
  public static <T> Set<T> intersection(Set<T> setA, Set<T> setB) {
    Set<T> tmp = new TreeSet<T>();
    for (T x : setA)
      if (setB.contains(x))
    return tmp;
  public static <T> Set<T> difference(Set<T> setA, Set<T> setB) {
    Set<T> tmp = new TreeSet<T>(setA);
    return tmp;
  public static <T> Set<T> symDifference(Set<T> setA, Set<T> setB) {
    Set<T> tmpA;
    Set<T> tmpB;
    tmpA = union(setA, setB);
    tmpB = intersection(setA, setB);
    return difference(tmpA, tmpB);
  public static <T> boolean isSubset(Set<T> setA, Set<T> setB) {
    return setB.containsAll(setA);
  public static <T> boolean isSuperset(Set<T> setA, Set<T> setB) {
    return setA.containsAll(setB);
  public static void main(String args[]) {
    TreeSet<Character> set1 = new TreeSet<Character>();
    TreeSet<Character> set2 = new TreeSet<Character>();
    System.out.println("set1: " + set1);
    System.out.println("set2: " + set2);
    System.out.println("Union: " + union(set1, set2));
    System.out.println("Intersection: " + intersection(set1, set2));
    System.out.println("Difference (set1 - set2): " + difference(set1, set2));
    System.out.println("Symmetric Difference: " + symDifference(set1, set2));
    TreeSet<Character> set3 = new TreeSet<Character>(set1);
    System.out.println("set3: " + set3);
    System.out.println("Is set1 a subset of set2? " + isSubset(set1, set3));
    System.out.println("Is set1 a superset of set2? " + isSuperset(set1, set3));
    System.out.println("Is set3 a subset of set1? " + isSubset(set3, set1));
    System.out.println("Is set3 a superset of set1? " + isSuperset(set3, set1));

Open in new window

Your program will populate the sets by tokenizing the strings that you have.
Here is a code to do that.
Note that the first list is created with new since Arrays.asList returns an immutable list, and we want to remove items from it.
			String d1="40-60-70"; // the data are id values basically
			String d2="60-70-80"; // the data are id values basically
			List<String> list1 = new ArrayList<String>(Arrays.asList(d1.split("-")));
			List<String> list2 = Arrays.asList(d2.split("-"));

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.