Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As @Jared Russell pointed out, this is not exactly the best specimen to show how the improvements Guava can help you make in your code.</p> <p>That said, if you're just looking for a demonstration of how Guava can find its way into this code, here's an shot:</p> <pre><code>import com.google.common.base.Joiner; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList.Builder; public class Question6842887 { public static void main(String[] args) { Builder&lt;Integer&gt; builder = ImmutableList.builder(); for (int i = 1; i &lt; 6; i++) { builder.add(i); } ImmutableList&lt;Integer&gt; list = builder.build(); System.out.println(Joiner.on(System.getProperty("line.separator")).join(list)); } </code></pre> <p>The two Guava things I'm using here are <a href="http://guava-libraries.googlecode.com/svn-history/r09/trunk/javadoc/com/google/common/collect/ImmutableList.html" rel="nofollow">immutable lists</a> and <a href="http://guava-libraries.googlecode.com/svn-history/r09/trunk/javadoc/com/google/common/base/Joiner.html" rel="nofollow">joiner</a>.</p> <p>This is a probably not the type of situation you'd normally need an ImmutableList, but it's good practice to get used to using it regardless. The great advantage of immutable collections (and really any immutable object) is that they are thread-safe. You never need to make defensive copies or otherwise worry about the contents changing after passing it off. You can simply pass it around freely, or cache it, and be happy and secure about its immutability. For more information on the subject of immutability, I'd refer you to <a href="http://www.google.com/products/catalog?hl=en&amp;cp=14&amp;gs_id=3&amp;xhr=t&amp;q=effective%20java&amp;safe=active&amp;bav=on.2,or.r_gc.r_pw.&amp;biw=1920&amp;bih=1001&amp;um=1&amp;ie=UTF-8&amp;tbm=shop&amp;cid=3195399426295624424&amp;sa=X&amp;ei=5gkwTuL9JYHq0gGdpsDSAQ&amp;sqi=2&amp;ved=0CFAQ8wIwAg" rel="nofollow">Effective Java</a>, item 15.</p> <p>The other Guava class I'm demonstrating here is Joiner. Joiner is just a nice and easy way to shove together a collection of strings with a separator. It is fairly customizable in that you can specify how it will deal with nulls as well as what separator you'd like to use. In this case, I've specified <code>System.getProperty("line.separator")</code> as my separator; you could instead choose to use <code>", "</code> or similar. I've also chosen not to deal with nulls, since I know there are no null values in our ImmutableList. You could, however, choose an option like <code>skipNulls</code> or <code>useForNull</code> if your list could have null values.</p> <p>The next release of Guava is expected to include <a href="http://guava-libraries.googlecode.com/svn-history/trunk/javadoc/com/google/common/collect/Range.html" rel="nofollow">Range</a> support. This could be used to simplify your list building to something like <code>ContiguousSet&lt;Integer&gt; list = Ranges.closedOpen(1, 6).asSet(DiscreteDomains.integers());</code> Though I'd not recommend it, your entire program could be one line.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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