Difference between revisions of "Initialize a Collection"

From CodeCodex

(Java)
 
(Implementations)
Line 2: Line 2:
 
===Java===
 
===Java===
 
The regular way  
 
The regular way  
<highlightsyntax language="java122">
+
<pre class="java">
 
  import java.util.List;
 
  import java.util.List;
 
import java.util.ArrayList;
 
import java.util.ArrayList;
Line 23: Line 23:
 
   }
 
   }
 
}
 
}
</highlightsyntax>
+
</pre>
 
Using a static initializer
 
Using a static initializer
<highlightsyntax language="java122">
+
<pre class="java">
 
  import java.util.List;
 
  import java.util.List;
 
import java.util.ArrayList;
 
import java.util.ArrayList;
Line 49: Line 49:
 
   }
 
   }
 
}
 
}
</highlightsyntax>
+
</pre>
 
Using the Double Brace Initialization.
 
Using the Double Brace Initialization.
 
Careful with this one because it can cause memory leak.  
 
Careful with this one because it can cause memory leak.  
<highlightsyntax language="java122">
+
<pre class="java">
 
  import java.util.List;
 
  import java.util.List;
 
import java.util.ArrayList;
 
import java.util.ArrayList;
Line 76: Line 76:
 
   }
 
   }
 
}
 
}
</highlightsyntax>
+
</pre>
 
Use Arrays.asList()  
 
Use Arrays.asList()  
<highlightsyntax language="java122">
+
<pre class="java">
 
  import java.util.List;
 
  import java.util.List;
 
import java.util.Arrays;
 
import java.util.Arrays;
Line 94: Line 94:
 
   }
 
   }
 
}
 
}
</highlightsyntax>
+
</pre>
 
Using Static import  
 
Using Static import  
 
First a small static class  
 
First a small static class  
<highlightsyntax language="java122">
+
<pre class="java">
 
  package com.rgagnon.howto;
 
  package com.rgagnon.howto;
  
Line 108: Line 108:
 
   }
 
   }
 
}
 
}
</highlightsyntax>
+
</pre>
 
then  
 
then  
<highlightsyntax language="java122">
+
<pre class="java">
 
  package com.rgagnon.howto;
 
  package com.rgagnon.howto;
  
Line 129: Line 129:
 
   }
 
   }
 
}
 
}
</highlightsyntax>
+
</pre>
[[Category:Java]]
+
 
 +
=== Tcl ===
 +
<pre class="tcl">
 +
set list {Bart Lisa Marge Homer Maggie}
 +
puts $list
 +
</pre>
 +
 
 +
[[Category:Java]] | [[Category:Tcl]]

Revision as of 17:27, 17 February 2011

Implementations

Java

The regular way

 import java.util.List;
import java.util.ArrayList;
public class InitCollections {
  public static void main(String args[]) {
    ArrayList<String> list = new ArrayList<String>();
    list.add("Bart");
    list.add("Lisa");
    list.add("Marge");
    list.add("Barney");
    list.add("Homer");
    list.add("Maggie");
    dump(list);
  }
  
  public static void dump(List<String> list) {
    for (String s:list) {
      System.out.println(s);
    }
  }
}

Using a static initializer

 import java.util.List;
import java.util.ArrayList;
public class InitCollections {
  static ArrayList<String> list = new ArrayList<String>();
  
  static {
    list.add("Bart");
    list.add("Lisa");
    list.add("Marge");
    list.add("Barney");
    list.add("Homer");
    list.add("Maggie");
  }
  public static void main(String args[]) {
    dump(list);
  }
  
  public static void dump(List<String> list) {
    for (String s:list) {
      System.out.println(s);
    }
  }
}

Using the Double Brace Initialization. Careful with this one because it can cause memory leak.

 import java.util.List;
import java.util.ArrayList;
public class InitCollections {
  
  static ArrayList<String> list = new ArrayList<String>() {{
    add("Bart");
    add("Lisa");
    add("Marge");
    add("Barney");
    add("Homer");
    add("Maggie");
  }};

  public static void main(String args[]) {
    dump(list);
  }
  
  public static void dump(List<String> list) {
    for (String s:list) {
      System.out.println(s);
    }
  }
}

Use Arrays.asList()

 import java.util.List;
import java.util.Arrays;
public class InitCollections {

  public static void main(String args[]) {
    List list = Arrays.asList("Bart", "Lisa", "Marge", "Homer", "Maggie");
    dump(list);
  }
  
  public static void dump(List list) {
    for (String s:list) {
      System.out.println(s);
    }
  }
}

Using Static import First a small static class

 package com.rgagnon.howto;

import java.util.Arrays;
import java.util.List;

public class CollectionUtils {
  public static <T> List<T> List(T...elems){
    return Arrays.asList( elems );
  }
}

then

 package com.rgagnon.howto;

import static com.rgagnon.howto.CollectionUtils.List;
import java.util.List;

public class InitCollections {

  public static void main(String args[]) {
    List<String> list = List("Bart", "Lisa", "Marge", "Homer", "Maggie"); 
    dump(list);
  }
  
  public static void dump(List<String> list) {
    for (String s:list) {
      System.out.println(s);
    }
  }
}

Tcl

set list {Bart Lisa Marge Homer Maggie}
puts $list
|