Note that there are some explanatory texts on larger screens.

plurals
  1. POEffective way to build a list of tokens from multiple strings in Java
    text
    copied!<p>I'm looking for an efficient way to obtain a list of String tokens extracted from multiple Strings (e.g. with a whitespace separator).</p> <p>Example:</p> <pre><code>String s1 = "My mom cook everyday"; String s2 = "I eat everyday"; String s3 = "Am I fat?"; LinkedList&lt;String&gt; tokens = new LinkedList&lt;String&gt;(); //any code to efficiently get the tokens //final result is tokens make of a list of the following tokens: //"My", "mom", "cook", "everyday", "I", "eat", "everyday", "Am", "I", "fat?". </code></pre> <p>Now</p> <ol> <li>I'm not sure that <code>LinkedList</code> is the most effective collection class to be used (Apache Commons, Guava, may they help?)!</li> <li>I was going to use <code>StringUtils</code> from Apache Commons, but the <code>split</code> method returns an array! So, I should extract with a for cycle the Strings from the array of String objects returned by split. Is that efficient: I don't know, <code>split</code> creates an array!</li> <li>I read about <code>Splitter</code> from Guava, but <a href="http://tomjefferys.blogspot.it/2012/02/guava-splitter-vs-stringutils.html" rel="nofollow">this post</a> states that <code>StringUtils</code> is better in practice.</li> <li>What about <code>Scanner</code> from <code>Java.util</code>. It seems to not allocate any additional data structures. Isn't it?</li> </ol> <p>Please, draw the most efficient Java solution, even by using additional widely used library, like <em>Guava</em> and <em>Apache Commons</em>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload