CodesJava

Easy learning with example program codes

Lower bounded wildcard in generics


Lower bounded wildcard:

Java generics lower bounded wildcard : Lower bounded wildcard is used to restrict the unknown type to be a specific type or a super type of that type using ‘?’ with super keyword.

Syntax: Collectiontype <? super T>

Let us consider that we want to write a method which prints list items and work on List<Integer>, List<Number> and List<Object> or any type which can contain Integer type value. Normally we write it as:

public static void printListItems(List<Integer> list) {
    for (Integer num : list){
	        System.out.println(num);
    }
}

But this method will not work for List<Number> and List<Object>. We can use lower bounded wild card to resolve this problem. List<? super Integer> matches a list of Integer and any of its supertypes.

public static void printListItems(List<? super Integer> list) {
for (Object num : list){
        System.out.println(num);
    }
}

Lower bounded wildcard example:


GenericsTest.java

import java.util.ArrayList;
import java.util.List;
 
/**
 * This class is used to show the use of Lower bounded wildcard.
 * @author codesjava
 */
public class GenericsTest {
	//Only work for the list of Number type.
	static void printListItems1(List<Integer> list){
	    for (Integer num : list){
	        System.out.println(num);
	    }
	}
 
	//Work for Number and any of its sub types.
	static void printListItems2(List<? super Integer> list){
	    for (Object num : list){
	        System.out.println(num);
	    }
	}
 
	public static void main(String args[]){
		//Arraylist of Integer type.	
		List<Integer> list1 = new ArrayList<Integer>();
		list1.add(1);
		list1.add(2);
		list1.add(3);
 
 
		//Arraylist of Number type.	
		List<Number> list2 = new ArrayList<Number>();
		list2.add(1.4);
		list2.add(2.3);
		list2.add(3.4);	
 
		//Only accept Integer type list.
		System.out.println("Print of List of Integer type" +
				" using squareOfListItems1 method:");
		printListItems1(list1);
 
		//Accept Integer and any of its super types.
		System.out.println("Print of List of Integer type" +
				" using squareOfListItems2 method:");;
		printListItems2(list1);
		System.out.println("Print of List of Number type using" +
				" squareOfListItems2 method:");
		printListItems2(list2);
 
	}
}

Output:

Print of List of Integer type using squareOfListItems1 method:
1
2
3
Print of List of Integer type using squareOfListItems2 method:
1
2
3
Print of List of Number type using squareOfListItems2 method:
1.4
2.3
3.4

Download this example.
 
Next Topic: Generics class example.
Previous Topic: Upper bounded wildcard.

Related Topics:

How to build java project using ant in eclipse?
JAXB marshalling – convert java object to xml example.
How to create pdf file in java using iText jar?
Generics class example.
OGNL in struts 2.
Hibernate One-to-One Mapping using xml.
Send inline image in email using JavaMail API.
Quartz 2 JobListener example.

 

Sign Up/ Sign In
Ask a Question


Industrial Training

We offers Placement Oriented Training on Java, Spring, JSF, Hibernate, PHP, AngularJS, Angular 4, PLSQL, Oracle BI Publisher etc. We also provides Online training, please mail us at hr@codesjava.com.

Development

We also provides the Development services for Website Development , Java Development, PHP Development, Android App Development etc. You can contact us on hr@codesjava.com.

Copyright © 2018 CodesJava DMCA.com Protection Status SiteMap Reference: Java Wiki