Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 276
  • Last Modified:

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.
0
techques
Asked:
techques
  • 13
  • 8
1 Solution
 
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 13
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now