Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Remove duplicates and sort comma seperated string 2

Status
Not open for further replies.

hard2bhumble

Technical User
Sep 9, 2006
2
GB
Think I posted this in the wrong forum previously .......

I am trying to remove duplicates from a comma separated list and sort it alphabetically. I'm not sure of the best method. I have achieved what I want the long way, but I think I should be able to use a Sorted Set, list or collection to achieve the same thing far more efficiently.
does anyone have any suggestions?
(e.g.) One, Five, Seven, One, Three, Four --> One, Three, Four, Five, Seven
 
Here is an example.
Code:
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class StringSort {
	public static void main(String args[]){
		String tmp="One,Five,Seven,One,Three,Four";
		String []target=tmp.split(",");
		Set<String> set = new HashSet<String>();
		Arrays.sort(target);
		for(int i=0;i<target.length;i++){
			set.add(target[i]);
		}
		
		System.out.println(set.toString());
			
	}
}

Chinese Java Faq Forum
 
Well, that will sort the alphabetically, you'll need some more work to do what you need.

Byw, you will need Java 1.5 to compile the program above.

Cheers,
Dian
 
I think instead of building the HashSet, I'd just loop through the array doing the output after ensuring that the current item is not equal to the previous item.

Code:
        String temp2 = "" ;
        for(int i=0;i<target.length;i++){
            if ( temp2 != target[i] )
            {
                 System.out.println( target[i] ) ;
                 temp2 = target[i] ;
            }
        }

All that HashSet stuff has to be slower than one variable assignment!
 
No no no ! You are comparing pointers there, not the actual string data !

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
sedj means you need to use the equals method to compare Strings.

Cheers,
Dian
 
I usually use the compareTo() method. I've had problems in the past using equals() on Strings.

Tim
 
If I remember, we were getting equals() returning false for two strings which were definitely identical. One of the Strings may have been demarshalled from a remote J2EE application, don't know if this would have any bearing on the matter.

Tim
 
Never heard about such a thing. Looking at the source code for both methos, I find them exact expect a type check: equals checks is the object compared is a String.

Maybe you were doing something 'cute' with classloaders, but from my experience, the equals methos is reliable and more explicit than compareTo because it returns a boolean and you don't need to remeber if 0 was equality or not.

Cheers,
Dian
 
...something 'cute' with classloaders.

Come to think about it, I recall that the code was inside a Stateless Session Bean hosted by a Borland Enterprise Server. Classloaders would definitely feature I think.

Maybe I should go back to using the equals() method for normal use.

Tim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top