Do you know the full power of Java? Have troubles writing great interfaces? Generics do help you with that. Check out how can you write powerful interfaces with Java generics. For more info and examples see http://uptech.team
8. public class Stack<E> {
public Stack();
public void push(E e);
public E pop();
public boolean isEmpty();
}
public void pushAll(Iterable<E> src) {
for (E e : src)
push(e);
}
Example
9. Stack<Number> numberStack = new Stack<Number>();
Iterable<Integer> integers = ... ;
numberStack.pushAll(integers); // Nooooooo
Push All
11. // popAll method without wildcard type - deficient!
public void popAll(Collection<E> dst) {
while (!isEmpty())
dst.add(pop());
}
Stack<Number> numberStack = new Stack<Number>();
Collection<Object> objects = ... ;
numberStack.popAll(objects); // Noooooooooo
Pop All
14. Max, come on ...
Object max(List src) {
// ...
}
15. Max, are you kidding me
public <T extends Comparable<? super T>> T max( List<? extends T> list) {
// ...
}
16. Swift Magic
protocol Foo {
typealias Key
typealias Element
}
protocol Bar {
typealias RawGeneratorType
}
func example<T : Foo, U, V where V : Foo, V : Bar, T.Key ==
V.RawGeneratorType, U == V.Element>
(arg1: T, arg2: U, arg3: V) -> U {
// ...
}
17. Swift insane Magic
// Methods in this extension are only available to Arrays whose elements are
// both hashable and comparable.
extension Array where Element : Hashable, Element : Comparable {
// ...
}