how to compare two string's id and concat into one unique id string?

Hi
I get 1 string and a string array got from a checkbox:
String A = "4,6,8,12,45";
String[] B = request.getParameterValues("id"); //has the value: 2   4   7   9  12   34

How can I compare them and make a unique string like:
2,4,6,7,8,9,12,34,45

There are no duplicated id in the new string.
techquesAsked:
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.

KuldeepchaturvediCommented:
which java version you are using?

you can use Hashset to make the union of these 2 arrays if you are using jdk 6
0
techquesAuthor Commented:
yes, I am using jdk 6.
How to make use of Hashset?
0
techquesAuthor Commented:
Hi
I write the hashset code and could you give me some comments where is it wrong?

in = "4     5    9   12   54   103"
A = "2,5,12,67,103";

result = "2,4,5,9,12,54,67,103"
import java.util.Iterator;
import java.util.HashSet;
import java.util.Set;
 
String [] in = request.getParameterValues("id"); //4,5,9,12,54,103
String A = "2,5,12,67,103";
String [] arrA = A.split("\\,");
HashSet hashSet=new HashSet();
hashSet.add(in);		    
		    
if(in.length > arrA.length) //Compare length of 2 arrays
{
for(int i=0; i<in.length;i++)
{
if(!hashSet.add(arrA[i])){
hashSet.add(arrA[i]);
}
}
}
else
{
for(int i=0; i<arrA.length;i++)
{
if(!hashSet.add(arrA[i])){
hashSet.add(arrA[i]);
}
}
}
Iterator iterator = hashSet.iterator();
while (iterator.hasNext()){
String result += (iterator.next() + ",");  
}

Open in new window

0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

KuldeepchaturvediCommented:
import java.util.Iterator;
import java.util.HashSet;
import java.util.Set;
 
String [] in = request.getParameterValues("id"); //4,5,9,12,54,103
String A = "2,5,12,67,103";
String [] arrA = A.split("\\,");
HashSet hashSet=new HashSet();
hashSet.addAll(in);
hashSet.addAll(arrA);

String yourArray = hashSet.toArray();

that is all you shd need.

0
KuldeepchaturvediCommented:
oops sorry
last line needs to be

String[] yourArray = hashSet.toArray();
0
techquesAuthor Commented:
Hi

There is a compile error for hashSet.addAll(in) and hashSet.addAll(arrA);

It said "The method addAll(Collection) in the type AbstractCollection is not applicable for the arguments String[]

String[] yourArray = hashSet.toArray();
It said cannot convert from Object[] to String[]
0
KuldeepchaturvediCommented:
okay lest give it collections..

import java.util.Iterator;
import java.util.HashSet;
import java.util.Set;
 
String [] in = request.getParameterValues("id"); //4,5,9,12,54,103
List lin = Arrays.asList(in);
String A = "2,5,12,67,103";
String [] arrA = A.split("\\,");
List listA = Arrays.asList(arrA);
HashSet<String> hashSet=new HashSet<String>();
hashSet.addAll(lin);
hashSet.addAll(listA);

String[] yourArray = hashSet.toArray();



0
techquesAuthor Commented:
String[] yourArray = hashSet.toArray();
It said cannot convert from Object[] to String[]

This error still happens
0
KuldeepchaturvediCommented:
Hmm
String[] yourArray = hashSet.toArray();
change to

String[] yourArray = (String[])hashSet.toArray();

0
techquesAuthor Commented:
Thanks for help.
Since I need to convert the yourArray to a string like the result = "2,4,5,9,12,54,67,103"
String result="";
for(int i=0; i<yourArray.length; i++)
 {
result += yourArray[i] + ",";
}
I use breakpoint to view the value of result, it is null.
Did I do sth wrong?
0
KuldeepchaturvediCommented:
for(int i=0; i<yourArray.length; i++)
 {
result += yourArray[i] + ",";
}

System.out.println(result);
lets see what it prints
0
techquesAuthor Commented:
result = "" 

0
KuldeepchaturvediCommented:
Hmm thats strange...

lets try some debugging..

mport java.util.Iterator;
import java.util.HashSet;
import java.util.Set;
 
String [] in = request.getParameterValues("id"); //4,5,9,12,54,103
List lin = Arrays.asList(in);
String A = "2,5,12,67,103";
String [] arrA = A.split("\\,");
List listA = Arrays.asList(arrA);
HashSet<String> hashSet=new HashSet<String>(lin);
System.out.println("The size of HashSet is:"+hashSet.size());
hashSet.addAll(listA);
System.out.println("The modified size of HashSet is:"+hashSet.size());
String[] yourArray = (String[])hashSet.toArray();
for(int i=0; i<yourArray.length; i++)
 {
result += yourArray[i] + ",";
}

System.out.println(result);


0
techquesAuthor Commented:
The size of HashSet is:2

The modified size of HashSet is:3

result = ""
0
techquesAuthor Commented:
sorry, it should be
The size of HashSet is:0

The modified size of HashSet is:3

result = ""

And, I found that it throws exception
[Ljava.lang.Object; cannot be cast to [Ljava.lang.String;

when it executes
String[] yourArray = (String[])hashSet.toArray();
0
techquesAuthor Commented:
sorry, I just set HashSet<String> hashSet=new HashSet<String>();
So, the size of HashSet is:0

When I add lin to HashSet<String> hashSet=new HashSet<String>(lin);
The size of HashSet is:0

However, arrA has 2 values: "4, 8"
But, the modified size of HashSet is still 3. It did not add to the hashset.

And, I found that it throws exception, seems (String[]) casting does not work to object hashSet.
[Ljava.lang.Object; cannot be cast to [Ljava.lang.String;

when it executes
String[] yourArray = (String[])hashSet.toArray();

result = ""

any idea?
0
techquesAuthor Commented:
When I add lin to HashSet<String> hashSet=new HashSet<String>(lin);
The size of HashSet is:3

Sorry for my typo.
0
KuldeepchaturvediCommented:
Try below code, I have tested it and it seems to work.
String [] in = request.getParameterValues("id"); //4,5,9,12,54,103
List lin = Arrays.asList(in);
String A = "2,5,12,67,103";
String [] arrA = A.split(",");
List listA = Arrays.asList(arrA);
Set hashSet=new HashSet();
hashSet.addAll(lin);
System.out.println("The size of HashSet is:"+hashSet.size()+hashSet);
hashSet.addAll(listA);
System.out.println("The modified size of HashSet is:"+hashSet.size()+hashSet);
String key = hashSet.toString();
System.out.print(key.substring(1,key.length()-1));

Open in new window

0

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
techquesAuthor Commented:
when i change the code to
String[] yourArray = (String[])hashSet.toArray(new String[hashSet.size()]);

then it is ok now

Thank you for your reply of http://www.experts-exchange.com/Programming/Languages/Java/J2EE/JSP/Q_24123381.html#a23638538
0
techquesAuthor Commented:
sorry, i forgot to add point again
0
techquesAuthor Commented:
Good and detail instruction
0
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
Java EE

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.