Using bounded wildcards in Java Generics

Categories: Java March 3rd, 2015 22:29

Code:

package com.liguoliang.java.core.lang.generics;

import org.junit.Before;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

import static org.junit.Assert.*;

public class GenericsWildCardTest {

private GenericsWildCard genericsWildCard;

private List<Integer> listOfIntegers;
private List<Number> listOfNumbers;
private List<Long> listOfLong;
private List<Object> listOfObjects;

@Before
public void before() {
genericsWildCard = new GenericsWildCard();

listOfIntegers = new ArrayList<Integer>();
listOfIntegers.add(1);
listOfIntegers.add(2);

listOfNumbers = new ArrayList<Number>();
listOfNumbers.add(1.1);

listOfLong = new ArrayList<Long>();
listOfLong.add(100L);

listOfObjects = new ArrayList<Object>();
}

@Test
public void testMergeList_() throws Exception {
List<Integer> listOfIntegers = new ArrayList<Integer>();
List<Number> listOfNumbers = new ArrayList<Number>();

int sizeExpected = listOfIntegers.size() + listOfNumbers.size();
genericsWildCard.mergeList_(listOfIntegers, listOfNumbers);
assertEquals(sizeExpected, listOfNumbers.size());
}

@Test
public void testMergeListWithWildCard() {
int sizeExpected = listOfIntegers.size() + listOfNumbers.size();
genericsWildCard.mergeListWithBoundedWildCard(listOfIntegers, listOfNumbers);
assertEquals(sizeExpected, listOfNumbers.size());

sizeExpected = listOfLong.size()+ listOfObjects.size();
genericsWildCard.mergeListWithBoundedWildCard(listOfLong, listOfObjects);
assertEquals(sizeExpected, listOfObjects.size());
}
}

 
package com.liguoliang.java.core.lang.generics;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
* Created by Guoliang, Li(liguoiang.com) on 3/3/2015.
*/
public class GenericsWildCard {

protected void mergeList_ (List<Integer> subList, List<Number> superList) {
}

protected void mergeListWithBoundedWildCard(List<? extends Number> subList, List<? super Number> superList) {
for (Number number : subList) {
superList.add(number);
}

Number firstItem = subList.get(0);

// subList.add(firstItem); // cannot be applied to Number;
// subList.add(Integer.valueOf(1)); // cannot be applied to Interger;

// superList.add(firstItem);
// superList.add(Integer.valueOf(1));
// superList.add(new Object()); // cannot be applied to Object
}


private static void mergeList(List<? extends Number> subList, List<? super Number> superList) {
Integer i = (Integer) subList.get(0);
Number n = i;

//subList.add(n); // cannot be applied
Number n1 = 0;
// subList.add(0);
// subList.add(n1);
// subList.add(0);

superList.add(i);
superList.add(n);
}

public static void main(String[] args) {
List<Number> listOfNumbers = new ArrayList<Number>();
listOfNumbers.add(0.9);
listOfNumbers.add(2.1);

List<Integer> listOfIntegers = new ArrayList<Integer>();
listOfIntegers.add(1);
listOfIntegers.add(3);

List<Object> listOfObjects = new ArrayList<Object>();

mergeList(listOfNumbers, listOfNumbers);
// mergeList(listOfNumbers, listOfIntegers);
// mergeList(listOfIntegers, listOfObjects);
}
}
 

Notes:

List<Integer> is not List<Number>
List<Integer> is List<? extends Number>

PECS – producer-extends, consumer-super;

Cannot add any item to List<? extends Object>

<->



// Proudly powered by Apache, PHP, MySQL, WordPress, Bootstrap, etc,.