Skip to content

Instantly share code, notes, and snippets.

@karanmalhi
Created June 13, 2011 15:22
Show Gist options
  • Save karanmalhi/1022975 to your computer and use it in GitHub Desktop.
Save karanmalhi/1022975 to your computer and use it in GitHub Desktop.
Explains bounded wildcards
package learnquest;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
public class BoxTest {
public static void main(String[] args) throws InstantiationException,
IllegalAccessException {
/*
* ArrayList<Box> b1 = new ArrayList<UpsBox>(); // will not work
* ArrayList<? extends Box> b2 = new ArrayList<Box>(); b2 = new
* ArrayList<UpsBox>(); b2 = new ArrayList<FedexBox>();
*/
ArrayList<Box> boxes = new ArrayList<Box>(3);
boxes.add(new Box());
boxes.add(new UpsBox());
boxes.add(new FedexBox());
print(boxes);
// list of UpsBox objects
ArrayList<UpsBox> upsBoxes = new ArrayList<UpsBox>(3);
upsBoxes.add(new UpsBox());
upsBoxes.add(new UpsBox());
upsBoxes.add(new UpsBox());
print(upsBoxes);
// generic methods
Box boxArray[] = { new Box(), new UpsBox(), new FedexBox() };
ArrayList<Box> boxList = new ArrayList<Box>(boxArray.length);
copy(boxArray, boxList);
print(boxList);
UpsBox upsBox = create(UpsBox.class);
// Box box = create(Box.class); wont work since Box is not Shippable
FedexBox fedexBox = create(FedexBox.class);
// String string = create(String.class); wont work because String not
// subclass of box
Box[] array = toArray(boxList);
// ***********************************************************
// Multiple elements in generic methods
}
private static <E> void copy(E[] source, List<E> dest) {
for (int i = 0; i < source.length; i++) {
dest.add(source[i]);
}
}
// you can only pass classes which subclass Box and implement Shippable
private static <E extends Box & Shippable> E create(Class<E> clazz)
throws InstantiationException, IllegalAccessException {
return clazz.newInstance();
}
// List<?> is equivalent to List<? extends Object>
private static void print(List<? super UpsBox> boxes) {
for (Iterator iterator = boxes.iterator(); iterator.hasNext();) {
Box box = (Box) iterator.next();
System.out.println(box);
}
Object object = boxes.get(0);
boxes.add(new UpsBox()); // can only add null
}
private static <E> E[] toArray(List<E> list) {
// if you are trying to create an array of E, that wont work
// E[] arr = new E[list.size()];
int index = 0;
Object[] arr = new Object[list.size()];
for (Iterator<E> iterator = list.iterator(); iterator.hasNext();) {
E e = iterator.next();
arr[index] = e;
index++;
}
return (E[]) arr;
}
private static void printMap(Map<Integer, String> map) {
Set<Entry<Integer, String>> entrySet = map.entrySet();
for (Iterator<Entry<Integer, String>> iterator = entrySet.iterator(); iterator
.hasNext();) {
Entry<Integer, String> entry = iterator.next();
Integer key = entry.getKey();
String value = entry.getValue();
}
// Map<Integer,Map<String,Set<Box>>> foo ;
}
}
interface Shippable {
}
class Box {
@Override
public String toString() {
return "Box";
}
}
class UpsBox extends Box implements Shippable {
@Override
public String toString() {
return "UpsBox";
}
}
class FedexBox extends Box implements Shippable {
@Override
public String toString() {
return "FedexBox";
}
}
class Foo<E> {
<T> void m2(T t) {
}
<T extends Number> void m1(T t) {
} // void m1(Number t)
}
class Bar extends Foo {
@Override
void m1(Number t) {
}
}
class Baz<E> extends Foo<E> {
}
class Bak extends Foo<String> {
}
class Fak<T> extends Bar {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment