This week's book giveaway is in the General Computing forum.
We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line!
See this thread for details.
The moose likes Java in General and the fly likes HashSet.addAll Arrays.asList split  extra space Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "HashSet.addAll Arrays.asList split  extra space" Watch "HashSet.addAll Arrays.asList split  extra space" New topic
Author

HashSet.addAll Arrays.asList split extra space

Miran Cvenkel
Ranch Hand

Joined: Nov 23, 2010
Posts: 138


Looks , to me like a bug ?


Searchable nature photo gallery: http://agrozoo.net/jsp/Galery.jsp?l2=en
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32833
    
    4
What are you splitting on? The $ sign, or line end?
Miran Cvenkel
Ranch Hand

Joined: Nov 23, 2010
Posts: 138
The $ sign.
Point beeing:


-->
2$3
[1,2, 2, 3]
you see extra spaces before 2 and 3, which appears from nowhere. Hence I get "duplicate" 2

Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 4905
    
    7

Miran Cvenkel wrote:
[1,2, 2, 3]
you see extra spaces before 2 and 3, which appears from nowhere.

I can guarantee you, it does NOT "appear from nowhere". You also haven't shown us the contents of the original string before the split.

In fact, it might be better to show us a bit more of your code (including the split() call), along with the precise contents of the strings before and after splitting.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Miran Cvenkel
Ranch Hand

Joined: Nov 23, 2010
Posts: 138
Here you have, test case that proves that something goes wrong in some cases.
Manifestation of that is that some numbers are getting in it with preceding spaces (WRONG), and some are not (OK)



See last out.print, how 13,33 are together and 44,55 pushed to right with space
Miran Cvenkel
Ranch Hand

Joined: Nov 23, 2010
Posts: 138
ahh damn, the secret of this:


is that 13,33 is all together one element. That is why I was confused.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 4905
    
    7

Miran Cvenkel wrote:is that 13,33 is all together one element. That is why I was confused.

See how good it is when you have to explain a problem in full to someone else?

FYI, To ensure that your elements don't have any extra whitespace in them, you need to trim your input string first and then use something like
split("\\s*,\\s*")
('split on a comma surrounded by any amount of whitespace'). Viz:HIH

Winston
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: HashSet.addAll Arrays.asList split extra space
 
Similar Threads
HashSet and equals()
Finish my code please
java.util.List method addAll(int,Collection)?
Linked List equals method
Comparing two collections...