2
\$\begingroup\$

I have to read a line of strings and split it for whitespace:

ArrayList<HashSet<String>> setOfStrings = new ArrayList<HashSet<String>>();
while ((currentLine = bufferedReader.readLine()) != null) {
        String[] tokens = currentLine.split(" ");
        for (int i = 0; i < tokens.length; i++) {
        setOfStrings.add(new HashSet<String>(Arrays.asList(tokens[i])));
        }
    }

i have to do this ,and for this reason i need for each tokens a new hashSet

i need to make operations on tokens like

[afaf] -> one hashset in first position inside the arrayList
[bafafa] -> one hashset in second position inside the arrayList
[cssss]
[dggg]
...

with your solution ,to make a unique hashset for the tokens, i can't do separate operations for each tokens.

How can I improve the split of the currentLine and the insertion into the arrayList? As I did it, it's very slow because I first have to split and then iterate on the array of tokens. How can I optimize this operation?

\$\endgroup\$
1
  • \$\begingroup\$ Why not a List for the tokens? \$\endgroup\$ Jun 9, 2014 at 0:04

1 Answer 1

5
\$\begingroup\$

Is the use of a Set for storing the tokens deliberate? That's because you can only have unique elements in the Set, and the output of the following code snippet will be 1, not 2:

System.out.println( new HashSet<>( Arrays.asList( "a a".split( " " ) ) ).size() );

Also, what you are doing is to store one token into a new List via Arrays.asList(), this is then iterated for elements to be stored into a new HashSet, which is finally added to setOfStrings. May we know what exactly are you trying to do here? I'll attempt to illustrate the output for the following:

Input:

a b c
d e f

Output:

setOfStrings
    new HashSet [ "a" ]
    new HashSet [ "b" ]
    new HashSet [ "c" ]
    new HashSet [ "d" ]
    new HashSet [ "e" ]
    new HashSet [ "f" ]

edit:

After reading your comment, I think what you're looking for is this:

while ((currentLine = bufferedReader.readLine()) != null) {
    String[] tokens = currentLine.split(" ");
    setOfStrings.add(new HashSet<String>(Arrays.asList(tokens)));
}

You do not have to iterate through tokens... Alternative suggestion relying on the utility method from the Collections class:

while ((currentLine = bufferedReader.readLine()) != null) {
    Set<String> hashSet = new HashSet<>();
    Collections.addAll( hashSet, currentLine.split( " " ) );
    setOfStrings.add(hashSet);
}

Do pay attention to its Javadoc: "The behavior of this convenience method is identical to that of c.addAll(Arrays.asList(elements)), but this method is likely to run significantly faster under most implementations." :)

\$\endgroup\$
9
  • \$\begingroup\$ i need to do this,i know that the hashset can only have unique value,i want that!,i need to insert each token in a new hashset,how i can improve this operation? you didn't answer that. \$\endgroup\$
    – OiRc
    Jun 9, 2014 at 6:48
  • \$\begingroup\$ If that's the case then you should be looking at adding all your tokens from a line into a single HashSet instance, not to create a HashSet instance per token. \$\endgroup\$
    – h.j.k.
    Jun 9, 2014 at 6:53
  • \$\begingroup\$ so for example u suggest to do this setOfStrings.add(new HashSet<String>(Arrays.asList(tokens))); ? is there a way to add the tokens without converting them into an array with this Arrays.asList(tokens)) \$\endgroup\$
    – OiRc
    Jun 9, 2014 at 6:58
  • \$\begingroup\$ please see question update. \$\endgroup\$
    – OiRc
    Jun 9, 2014 at 7:20
  • \$\begingroup\$ Just to be sure we're clear on each other definitions, a line "a a" will yield two tokens after split(), and there will only be one unique value a, is that your understanding too? \$\endgroup\$
    – h.j.k.
    Jun 9, 2014 at 7:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.