Java Generics and ArrayLists This abomination of code creates arrays based off input from the user. Sample input would be something like "S 4 aba bab baa aab" where:


args[0] = array type (Int, Double, String)
args[1] = length of array
args[2...] = contents of the array


The creation of the arrays in my code is horrible, but I am not quite sure how else to do it while incorporating generics. The point of the code is to be able to pass any of the created arrays to the methods at the bottom, hence generics.

import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;

public class Prog7a&lt;E&gt; {
public static void main(String[] args) throws FileNotFoundException {

    checkUsage(args);

    //This would be nice, but I get "Cannot make static reference to non-static E"
    //      ArrayList&lt;E&gt; array = new ArrayList&lt;&gt;();
    //
    //      if (args[0].matches("I|i")) 
    //          for (int i = 2; i &lt; args.length; i++) 
    //              array.add((E) new Integer(args[i]));
    //
    //       else if (args[0].matches("S|s")) 
    //           for (int i = 2; i &lt; args.length; i++) 
    //                  array.add((E) new String(args[i]));
    //       else 
    //           for (int i = 2; i &lt; args.length; i++) 
    //                  array.add((E) new Double(args[i]));
    //
    //
    //      System.out.println("Original: " + array);
    //      System.out.println("  Unique: " + removeDuplicates(array));
    //      shuffle(array);
    //      System.out.println("Shuffled: " + array);
    //      System.out.println(" Maximum: " + max(array));


My question is here: I have the loop to fill in the array and call to the methods in each conditional, which makes me think I could rewrite this in such a way that it is not so repetitive.

    if (args[0].matches("I|i")) {
        ArrayList&lt;Integer&gt; iL = new ArrayList&lt;Integer&gt;();
        for (int i = 2; i &lt; args.length; i++) {
            iL.add(Integer.parseInt(args[i]));
        }
        System.out.println("Original: " + iL);
        System.out.println("  Unique: " + removeDuplicates(iL));
        shuffle(iL);
        System.out.println("Shuffled: " + iL);
        System.out.println(" Maximum: " + max(iL));

    } else if (args[0].matches("S|s")) {
        ArrayList&lt;String&gt; sL = new ArrayList&lt;String&gt;();
        for (int i = 2; i &lt; args.length; i++) {
            sL.add(args[i]);
        }
        System.out.println("Original: " + sL);
        System.out.println("  Unique: " + removeDuplicates(sL));
        shuffle(sL);
        System.out.println("Shuffled: " + sL);
        System.out.println(" Maximum: " + max(sL));
    } else {
        ArrayList&lt;Double&gt; dL = new ArrayList&lt;Double&gt;();
        for (int i = 2; i &lt; args.length; i++) {
            dL.add(Double.parseDouble(args[i]));
        }
        System.out.println("Original: " + dL);
        System.out.println("  Unique: " + removeDuplicates(dL));
        shuffle(dL);
        System.out.println("Shuffled: " + dL);
        System.out.println(" Maximum: " + max(dL));
    }




}

public static &lt;E&gt; ArrayList&lt;E&gt; removeDuplicates(ArrayList&lt;E&gt; list) {
    new HashSet&lt;E&gt;(list);
    list = new ArrayList&lt;E&gt;(new HashSet&lt;E&gt;(list));              
    return list;

}

public static &lt;E&gt; void shuffle(ArrayList&lt;E&gt; list) {
    Collections.shuffle(list);
}

public static &lt;E extends Comparable&lt;E&gt;&gt; E max(ArrayList&lt;E&gt; list) {
    return Collections.max(list);
}

public static void checkUsage(String[] args)
{
    if (args.length &lt; 2) 
    {
        System.out.println("Please supply more than 2 arguments");
        System.exit(1);
    }
    if (!(args[0]=="s") &amp;&amp; (args[0]=="S") &amp;&amp;
        (args[0]=="d") &amp;&amp; (args[0]=="D") &amp;&amp;
        (args[0]=="i") &amp;&amp; (args[0]=="I")){
        System.out.println("Incorrect format of command.");
        System.exit(1);
    }
}
}
There are several problems with your code. I will start with the method checkUsage(String[]). The check for the number of arguments should be if (args.length &lt;= 2), with a &lt;= instead of a &lt; operator (or, alternatively, if (args.length &lt; 3). Then, the check of the arguments themselves is faulty, because the condition you specify for an incorrect format will never evaluate to true. To check if none of two conditions hold true, you must either test


if (!(condition1 || condition2))


or


if (!condition1 &amp;&amp; !condition2).


The two are equivalent, but be aware of operator precedence: The unary operator ! will be evaluated before || or &amp;&amp;, so in the first case, you have to surround the expression condition1 || condition2 with parentheses.

Next, you are comparing Strings using ==. This is dangerous, because == only checks for reference equality but not the contents of the Strings. You should rather use String.equals(Object) to compare Strings. In your case, you could even take advantage of String.equalsIgnoreCase(String), halving the number of method invocations and making the code easier to read.

So much for the method checkUsage(String[]). On to the methods shuffle(ArrayList) and max(ArrayList). I don't really see a point in defining these methods, since they do nothing but pass the argument to the respective method in Collections, so you might as well call Collections.max(Collection) and Collections.shuffle(List) directly from main(String[]).

The method removeDuplicates(ArrayList) unnecessarily creates a HashSet in the first line that will never be used, because the expression new HashSet&lt;E&gt;(list) passed to the ArrayList constructor in the second line creates a new HashSet that has nothing to do with the HashSet created in the first line, so the first line is completely redundant. Also be aware that HashSet.iterator() returns the elements contained in the Set in no particular order, so your method removeDuplicates(ArrayList) might not only remove duplicates, but also change the order of the List in general. Furthermore, you might want to consider defining the method parameter as a List (which is an interface) instead of an ArrayList (which is a class), because the method doesn't depend on the specific implementation of the passed List but only on its behavior, which is defined by the interface. That way, the method can also accept other implementations of the List interface and not only ArrayLists. And lastly, the name "removeDuplicates" is a bit misleading, since, unlike shuffle(List), the method doesn't actually modify the passed List but returns a new List without duplicates, leaving the passed List unmodified. Maybe "withoutDuplicates" would be better, or something like that.

Now, about the generic type parameter E. There is no point in defining it in the class declaration because the class itself does not depend on it, only your methods that accept an ArrayList (or List) as a parameter do. I suggest you remove it, because defining the class Prog7a with a generic type parameter &lt;E&gt; and then declaring methods that define a generic type parameter &lt;E&gt; again introduces ambiguity:

public class Prog7a&lt;E&gt; { /* If we instantiate a Prog7a&lt;String&gt;,
                            this E would be String */

    /* Now comes a method declaration that defines a generic
    type parameter &lt;E&gt; again. This newly defined parameter
    hides the parameter defined in the class declaration and is
    completely independent of it, so this method might
    very well accept a List&lt;Integer&gt;, even though the type
    of this class is Prog7a&lt;String&gt; */
    public &lt;E&gt; ArrayList&lt;E&gt; removeDuplicates(List&lt;E&gt; list) {
        // ...
    }
}


Regarding the actual problem at hand, which is the code repetition in your original code, the other two suggested solutions both seem appropriate to me, but I don't get the organization of slowy's code at all. Not only that the execution logic of the program is put inside a constructor, but the constructor actually doesn't serve any other purpose at all except establishing a type parameter, and the generic type parameter of the Prog7a class is unnecessary, as I already explained. This also means that your static methods can, indeed, remain static.