Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Convert a String to an Array and Back Again without Duplicates

Posted on 2007-10-21
8
Medium Priority
?
1,229 Views
Last Modified: 2008-01-09
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.
0
Comment
Question by:lasutton
  • 4
  • 3
8 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 20117794
Try
            List unique = new ArrayList(new HashSet(new ArrayList(Arrays.asList(array))));
0
 
LVL 7

Expert Comment

by:UrosVidojevic
ID: 20117797
You will have to use
String[] split(String regex)
method in String class and then remove duplicates manualy.
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 2000 total points
ID: 20117805
Sorry:

array = (String[])new ArrayList<String>(new LinkedHashSet<String>(new ArrayList<String>(Arrays.asList(array)))).toArray(new String[0]);
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:lasutton
ID: 20117808
And to move the list back into a delimited string, do I need to loop through each element and add to the new String?
0
 

Author Comment

by:lasutton
ID: 20117815
For the...

array = (String[])new ArrayList<String>(new LinkedHashSet<String>(new ArrayList<String>(Arrays.asList(array)))).toArray(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?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20117827
>>...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
0
 

Author Comment

by:lasutton
ID: 20117952
Thanks CEHJ, this works perfectly based on your help:

public String removeRepeatingElementsInDelimitedString(String aStringToParse) throws Exception {
      String[] originalList = aStringToParse.split(",");
      String noduplicatesListDelimitedResult = "";
      originalList = (String[])new ArrayList<String>(new LinkedHashSet<String>(new ArrayList<String>(Arrays.asList(originalList)))).toArray(new String[0]);
      int newListLength = originalList.length;
      for(int x = 0;x<newListLength;x++)
      {
      noduplicatesListDelimitedResult = noduplicatesListDelimitedResult + originalList[x] + ",";
      }
      noduplicatesListDelimitedResult = noduplicatesListDelimitedResult.substring(0, noduplicatesListDelimitedResult.length()-1);
            
      return noduplicatesListDelimitedResult;
}
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20117957
Good - it would be better to append using a StringBuilder though
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month21 days, 7 hours left to enroll

810 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