Solved

ArrayList trick

Posted on 2009-07-13
5
327 Views
Last Modified: 2013-12-29
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 ?
0
Comment
Question by:cofactor
5 Comments
 
LVL 23

Assisted Solution

by:Ajay-Singh
Ajay-Singh earned 50 total points
ID: 24839573
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)
0
 
LVL 20

Accepted Solution

by:
a_b earned 60 total points
ID: 24839585
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
0
 
LVL 1

Expert Comment

by:nitin22
ID: 24839598
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);

    tmp.addAll(setB);

    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))

        tmp.add(x);

    return tmp;

  }
 

  public static <T> Set<T> difference(Set<T> setA, Set<T> setB) {

    Set<T> tmp = new TreeSet<T>(setA);

    tmp.removeAll(setB);

    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>();
 

    set1.add('A');

    set1.add('B');

    set1.add('C');

    set1.add('D');
 

    set2.add('C');

    set2.add('D');

    set2.add('E');

    set2.add('F');
 

    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);
 

    set3.remove('D');

    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

0
 
LVL 1

Expert Comment

by:nitin22
ID: 24839607
Your program will populate the sets by tokenizing the strings that you have.
0
 
LVL 8

Assisted Solution

by:ozlevanon
ozlevanon earned 40 total points
ID: 24840548
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("-"));

			list1.removeAll(list2);

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Which is the latest version of Apache Tomcatl 3 49
best (free) software to access postgres db (java) 1 32
base64 decode encode 12 119
Java Options in Zimbra Server 2 44
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now