lasutton
asked on
Convert a String to an Array and Back Again without Duplicates
I am looking for the best way to convert a String to an Array and Back Again without any Duplicates.
Basically I have a comma delimited string that includes names. I want to return a string that contains a comma delimited string but without any duplicates.
Basically I have a comma delimited string that includes names. I want to return a string that contains a comma delimited string but without any duplicates.
You will have to use
String[] split(String regex)
method in String class and then remove duplicates manualy.
String[] split(String regex)
method in String class and then remove duplicates manualy.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
And to move the list back into a delimited string, do I need to loop through each element and add to the new String?
ASKER
For the...
array = (String[])new ArrayList<String>(new LinkedHashSet<String>(new ArrayList<String>(Arrays.a sList(arra y)))).toAr ray(new String[0]);
...solution, I then parse through the array and move it into a String manually, or is there something that can do the whole thing in a single line?
array = (String[])new ArrayList<String>(new LinkedHashSet<String>(new ArrayList<String>(Arrays.a
...solution, I then parse through the array and move it into a String manually, or is there something that can do the whole thing in a single line?
>>...solution, I then parse through the array and move it into a String manually, or is there something that can do the whole thing in a single line?
Yes, you do. Unfortunately there's no 'join' method in Java
Yes, you do. Unfortunately there's no 'join' method in Java
ASKER
Thanks CEHJ, this works perfectly based on your help:
public String removeRepeatingElementsInD elimitedSt ring(Strin g aStringToParse) throws Exception {
String[] originalList = aStringToParse.split(",");
String noduplicatesListDelimitedR esult = "";
originalList = (String[])new ArrayList<String>(new LinkedHashSet<String>(new ArrayList<String>(Arrays.a sList(orig inalList)) )).toArray (new String[0]);
int newListLength = originalList.length;
for(int x = 0;x<newListLength;x++)
{
noduplicatesListDelimitedR esult = noduplicatesListDelimitedR esult + originalList[x] + ",";
}
noduplicatesListDelimitedR esult = noduplicatesListDelimitedR esult.subs tring(0, noduplicatesListDelimitedR esult.leng th()-1);
return noduplicatesListDelimitedR esult;
}
public String removeRepeatingElementsInD
String[] originalList = aStringToParse.split(",");
String noduplicatesListDelimitedR
originalList = (String[])new ArrayList<String>(new LinkedHashSet<String>(new ArrayList<String>(Arrays.a
int newListLength = originalList.length;
for(int x = 0;x<newListLength;x++)
{
noduplicatesListDelimitedR
}
noduplicatesListDelimitedR
return noduplicatesListDelimitedR
}
Good - it would be better to append using a StringBuilder though
List unique = new ArrayList(new HashSet(new ArrayList(Arrays.asList(ar