Java generics examples -- use generics in collection

Here are some most simple and common use of generics with collection.

Example 1:

List<String> names = new ArrayList<String>();
System.out.printf("List<String> names: %s%n", names);
In the above example, names is a List of String. When retrieving elements from the List, the return value is of type String. So no need for casting, which is a big advantage over the old, non-parameterized collection.

Example 2:
Map<Integer, String> idToName = new HashMap<Integer, String>();
idToName.put(0, "John");
System.out.printf("Map<Integer, String> idToName: %s%n", idToName);
In the above example, idToName is a Map with a Integer key and String value. The output is:
Map<Integer, String> idToName: {0=John}

Example 3:
List<List<String>> listOfList = new ArrayList<List<String>>();
List<String> sublist1 = new ArrayList<String>();
sublist1.add("A String inside sublist1");

List<String> sublist2 = new LinkedList<String>();
sublist2.add("A String inside sublist2");
System.out.printf("List<List<String>> listOfList: %s%n", listOfList);
The above example shows a List whose elements are of type List, i.e., a List of List. The inner List declares that it can only hold String elements. The first inner list is an ArrayList of String, and the second is a LinkedList of String. Running this code snippet prints:
List<List<String>> listOfList: [[A String inside sublist1], [A String inside sublist2]]

Example 4:
private static <T> List<T> extractElements(List bag, Class<T> type) {
List<T> result = new ArrayList<T>();
for(Object e : bag) {
//if(e instanceof T) can't use instanceof
if(type.isAssignableFrom(e.getClass())) {
result.add((T) e);
return result;
This method takes a List of mixed elements and extracts those elements of the desired type. The following shows how to call this method:
List bag = new ArrayList();
bag.add(new Integer(0));
bag.add(new Integer(1));
bag.add(new Double(2008.5));
bag.add("a string");
List<Number> numbersInBag = extractElements(bag, Number.class);
System.out.printf("All elements in bag: %s%nNumber elements in bag: %s%n",
bag, numbersInBag);
List<Integer> integersInBag = extractElements(bag, Integer.class);
System.out.printf("All elements in bag: %s%nInteger elements in bag: %s%n",
bag, integersInBag);
-------- output -----------

All elements in bag: [0, 1, 2008.5, a string]
Number elements in bag: [0, 1, 2008.5]
All elements in bag: [0, 1, 2008.5, a string]
Integer elements in bag: [0, 1]


Copy plugins between NetBeans installations

NetBeans installer does not migrate plugins, and with every new installation, your old plugins are gone. This makes sense because the new NetBeans installation may not support the old plugins. So you will need to re-install these plugins through update centers or manually installing downloaded *.nbm files.

Another option is to copy the plugin jar and configuration files, from the old .netbeans directory to the new .netbeans directory. For example, this is how I copied whichelement plugin from NetBeans 6.0 to 6.1:
cd $HOME/.netbeans/6.0/modules
cp org-netbeans-modules-whichelement.jar $HOME/.netbeans/6.1/modules/

cd $HOME/.netbeans/6.0/config/Modules/
cp org-netbeans-modules-whichelement.xml $HOME/.netbeans/6.1/config/Modules/
Restart NetBeans and you will see the new plugin is enabled. These copied plugins will always appear installed in Tools | Plugins window, and grayed out. So there is no way to deactivate or uninstall them, since they were not installed with the wizard in the first place. But I guess you can always configure them by editing their config files under .netbeans/6.1/config/Modules. The following is the config file for whichelement (org-netbeans-modules-whichelement.xml):
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE module PUBLIC "-//NetBeans//DTD Module Status 1.0//EN"
<module name="org.netbeans.modules.whichelement">
<param name="autoload">false</param>
<param name="eager">false</param>
<param name="enabled">true</param>
<param name="jar">modules/org-netbeans-modules-whichelement.jar</param>
<param name="reloadable">false</param>
<param name="specversion">1.5</param>
This is not the recommended approach in most cases and may not work for some plugins. However, it can be useful if you can't find it in update centers or in *.nbm form.

Update on 12/07/2008:
I've copied whichelement plugin from my NetBeans 6.1 installation to 6.5. It works fine so far.


How to customize NetBeans look and feel

NetBeans by default uses the platform native look and feel: windows look and feel on Windows, and Gtk look and feel on Linux and Solaris where Gtk 2 is available. While the windows look and feel is fine and pretty close to native windows applications, NetBeans doesn't look the best with Gtk look and feel. Here are some options to adjust them, in order of my preference:

1. Plastic looks from JGoodies. Go to JGoodies download page, choose "JGoodies Looks" among the list of download links. Runs with Java 1.4 or greater.

Unzip the download file (looks-2.1.4.zip) to somewhere, e.g., /home/xxx. The zip file contains a top-level root directory so no need to make a temp dir for unzipping.

Edit NetBeans-dir/etc/netbeans.conf file, appending the following options to netbeans_default_options (You may want to copy and comment out the original line for safety):

--cp:p /home/xxx/looks-2.1.4/looks-2.1.4.jar --laf com.jgoodies.looks.plastic.Plastic3DLookAndFeel
Note that netbeans_default_options value is quoted so after your editing, it should still end with ". Restart NetBeans to see the new look. You can also pass these options in command line when you start NetBeans and command line options will override netbeans_default_options in netbeans.conf file:

NetBeans-dir/bin/netbeans --cp:p /home/xxx/looks-2.1.4/looks-2.1.4.jar --laf com.jgoodies.looks.windows.WindowsLookAnd
JGoodies looks comes with the following look and feels:
All of them are cross-platform, which means you can also use com.jgoodies.looks.windows.WindowsLookAndFeel on Linux. But it's not much different from swing metal look and feel. While trying Plastic3DLookAndFeel, PlasticLookAndFeel and PlasticXPLookAndFeel on my Linux laptop, I didn't see any noticeable difference among them.

2. Use the classic swing metal look and feel. Although not visually appealing, it has some benefits: (1) no additional jar on classpath; (2) consistent look and feel across platform; and (2) maybe a bit faster. To use metal, edit NetBeans-dir/etc/netbeans.conf:
netbeans_default_options="... --laf javax.swing.plaf.metal.MetalLookAndFeel"
Since NetBeans always sets look and feel whether you specify --laf option or not, the swing default look and feel does to take effect. Adding a file in JAVA_HOME/jre/lib/swing.properties has no effect either. It is not possible to use swing Windows look and feel on non-Windows platform.

3. If NetBeans is running on JDK 6 or greater, Install NetBeans Substance plugin. This is listed # 3, though recommended at NetBeans wiki page. You can download the plugin as a *.nbm file at NetBeans Plugin Portal, and install it following menu Tools | Plugins | Downloaded tab | Add Plugins... button. Once installed, a wide variety of skins can be chosen from menu View | Skins. You can change skin at any time without restarting NetBeans.

I tried all skins and they seem to be very similar, differing only in colors used. The good ones (good to my eyes) are "Moderate" and "Business *". One thing I don't like is that they all auto-hide the plus sign in front of the folder icon inside File and Favorite panel, which I found is very annoying and distracting. When the mouse moves on the left panel, the on-focus elements changes background color. I find it confusing since the current selected element is also highlighted.

After trying it for a couple of days, I decide to unininstall it (Tools | Plugins | Installed tab, select the plugin and click Deactivate on the right, or click Uninstall button on the bottom).

4. Napkin look and feel, making you feel like coding on a napkins. I don't quite like its informal style but others may. Links: overview & snapshots, download, instructions.

5. Nimbus look and feel in JDK 6 update 10 (beta as of 5/15/2008). See here for a comparison to metal. To use it with NetBeans, install JDK 6 update 10, set netbeans_jdkhome to JDK 6u10, and netbeans_default_options to include "--laf com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel" in NetBeans-dir/etc/netbeans.conf.

Wtih Nimbus, I can only see the selected items on my left panel (File, Favorite, or Project views). Other elements all have the same color as the white background so are invisible. Will try it again when the final JDK 6u10 is out.