A list of values of unknown datatypes - Generic wildcard example in Java

The Generics feature of the Java language allows programmers write reusable code.

Imagine that you want to build a generic function to manipulate a list of values. The manipulation can be a type of calculation or a way of organizing values.  By using Generics without wildcard, the datatype of the list can be broadly defined; but with wildcard, the datatype(s) of the list can be undefined when the function is built. That allows even more flexibility.

With wildcard, a function can be built in a way to generically accept a list of Strings, a list of Integers, a list of Doubles or a list of Longs etc.  In addition, the function can accept a list of values of various datatypes.  To build such a function, we need to use the generic wildcard syntax: <?>

Here is a small program of demonstrating the use of generic wildcard:

import java.util.*;

class MyInventoryOfUnknownDemo {
    
    // This function accepts a collection of unknown
    static void printCollection(Collection<?> c) {
        for (Object o : c)
            System.out.print (o + " ");
        System.out.print ("\n");
    }
    
    // This function accepts an abstract map of unknown
    static void printAbstractMap(AbstractMap<?, ?> am, String key) {
        System.out.println (key + ": " + am.get(key));
    }
    
    public static void main(String args[]) {
        
        // Define a linked list of String
        LinkedList <String> lls = new LinkedList<> ();
        lls.add("This"); lls.add("is"); lls.add("a"); lls.add("linked"); lls.add("list"); lls.add("of"); lls.add("String"); 

        printCollection (lls);
        
        // Define a linked list that contains different number types
        LinkedList <Number> lln = new LinkedList<> ();
        lln.add(10); lln.add(30f); lln.add(50d); lln.add(70L);

        printCollection (lln);
        
        // Define a tree map that contains key-value pairs where key is String and value is String
        TreeMap <String, String> tmss = new TreeMap<> ();
        tmss.put("Name", "Calmalgo"); tmss.put("Language", "Java"); tmss.put("OS", "All");
        
        printAbstractMap (tmss, "Name");
        printAbstractMap (tmss, "Language");
        printAbstractMap (tmss, "OS");    
    }
}

The function printCollection will accept a Collection of values of unknown datatype(s).  There can be one or more datatypes within this Collection.

The function printAbstractMap will accept an AbstractMap of values of unknown datatype(s).  By definition, AbstractMap manages a list of Key-value Pairs.  The Key can be of any datatype like String, Integer, Double, Long, or a Class defined by yourself etc.  Similarly, the Value can be of any datatype.  Given the nature of Key-value Pairs, printAbstractMap takes in the parameter AbstractMap<?, ?> -- first question mark for the Key's datatype, second for the Value's datatype.

The above program produces this result:
This is a linked list of String
10 30.0 50.0 70
Name: Calmalgo
Language: Java
OS: All

Comments

Popular posts from this blog

Finding Median by Stream

Factorial by Different Styles in Java

Reduction by Java Stream