Skip to content

Commit 4d75022

Browse files
committed
code examples from Java Generics and Collections book
1 parent edf0a09 commit 4d75022

File tree

8 files changed

+226
-0
lines changed

8 files changed

+226
-0
lines changed

.classpath

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<classpath>
33
<classpathentry kind="src" path="src"/>
4+
<classpathentry kind="src" path="resources"/>
45
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
56
<classpathentry kind="lib" path="libs/commons-codec-1.7.jar"/>
67
<classpathentry kind="output" path="bin"/>

resources/MultipleBounds.file.in.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello World

resources/MultipleBounds.file.out.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello World

src/benblack86/java/Bridge.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package benblack86.java;
2+
3+
import java.lang.reflect.Method;
4+
5+
public class Bridge {
6+
public static void main(String[] args) {
7+
for(Method m : Point.class.getMethods()) {
8+
if(m.getName().equals("clone") || m.getName().equals("compareTo")) {
9+
System.out.println(m.toGenericString());
10+
}
11+
}
12+
}
13+
14+
}
15+
16+
class Point implements Comparable<Point> {
17+
18+
@Override
19+
public int compareTo(Point point) {
20+
return 0;
21+
}
22+
23+
@Override
24+
public Point clone() {
25+
return new Point();
26+
}
27+
28+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package benblack86.java;
2+
3+
import java.io.Closeable;
4+
import java.io.FileReader;
5+
import java.io.FileWriter;
6+
import java.io.IOException;
7+
import java.nio.CharBuffer;
8+
9+
10+
public class MultipleBounds {
11+
12+
public static void main(String[] args) {
13+
try {
14+
FileReader r = new FileReader("resources/MultipleBounds.file.in.txt");
15+
FileWriter w = new FileWriter("resources/MultipleBounds.file.out.txt");
16+
17+
copy(r, w);
18+
19+
} catch(Throwable t) {
20+
t.printStackTrace();
21+
}
22+
}
23+
24+
public static <S extends Readable & Closeable, T extends Appendable & Closeable> void copy(S src, T trg) throws IOException {
25+
try {
26+
CharBuffer buf = CharBuffer.allocate(32);
27+
28+
while(src.read(buf) >= 0) {
29+
buf.flip(); // prepare buffer for writing
30+
trg.append(buf);
31+
buf.clear(); // prepare buffer for reading
32+
}
33+
} finally {
34+
// not good design as resources should be open and closed in the same block
35+
src.close();
36+
trg.close();
37+
}
38+
}
39+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package benblack86.java;
2+
3+
import java.util.AbstractCollection;
4+
import java.util.Collection;
5+
import java.util.Iterator;
6+
import java.util.NoSuchElementException;
7+
8+
public class StaticNestedClass {
9+
/*
10+
* Nested classes: way of logically grouping classes that are only used in one
11+
* place, increase encapsulation, lead to more readable and maintainable code
12+
* Static: cannot refer directly to instance variables or methods defined in
13+
* its enclosing class, behave like top level classes, more efficient
14+
*/
15+
16+
public static void main(String args[]) {
17+
Collection<String> items = new LinkedCollection<String>();
18+
items.add("hello");
19+
items.add("world");
20+
21+
for(String item : items) {
22+
System.out.println(item);
23+
}
24+
}
25+
}
26+
27+
28+
class LinkedCollection<E> extends AbstractCollection<E> {
29+
private static class Node<T> {
30+
private T element;
31+
private Node<T> next = null;
32+
33+
private Node(T element) {
34+
this.element = element;
35+
}
36+
}
37+
38+
private Node<E> first = new Node<E>(null);
39+
private Node<E> last = first;
40+
private int size = 0;
41+
42+
public LinkedCollection() {}
43+
44+
public LinkedCollection(Collection<? extends E> collection) {
45+
// extends E means we can get elements out of the collection
46+
this.addAll(collection);
47+
}
48+
49+
@Override
50+
public int size() {
51+
return this.size;
52+
}
53+
54+
@Override
55+
public boolean add(E element) {
56+
this.last.next = new Node<E>(element);
57+
this.last = this.last.next;
58+
this.size++;
59+
return true;
60+
}
61+
62+
private static class LinkedIterator<T> implements Iterator<T> {
63+
private Node<T> current;
64+
65+
public LinkedIterator(Node<T> first) {
66+
this.current = first;
67+
}
68+
69+
@Override
70+
public boolean hasNext() {
71+
return current.next != null;
72+
}
73+
74+
@Override
75+
public T next() {
76+
if (hasNext()) {
77+
current = current.next;
78+
return current.element;
79+
} else {
80+
throw new NoSuchElementException();
81+
}
82+
}
83+
84+
@Override
85+
public void remove() {
86+
throw new UnsupportedOperationException();
87+
}
88+
89+
}
90+
91+
@Override
92+
public Iterator<E> iterator() {
93+
return new LinkedIterator<E>(first);
94+
}
95+
96+
}

src/benblack86/java/Varargs.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package benblack86.java;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class Varargs {
7+
public static void main(String[] args) {
8+
System.out.println(Varargs.toList(new String[]{"one", "two"}));
9+
// System.out.println(Varargs.toList(null)); runtime exception
10+
System.out.println(Varargs.toList(new Object[]{null}));
11+
System.out.println(Varargs.toList(new Object[]{}));
12+
}
13+
14+
public static <T> List<T> toList(T... array) {
15+
List<T> list = new ArrayList<T>();
16+
17+
for(T element : array) {
18+
list.add(element);
19+
}
20+
21+
return list;
22+
}
23+
}

src/benblack86/java/Wildcards.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package benblack86.java;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
7+
public class Wildcards {
8+
9+
/*
10+
* The Get and Put principle: use an extends wildcard when you only get values
11+
* out of a structure, use a super wildcard when you only put values into a
12+
* structure, and don't use a wildcard when you both get and put.
13+
*
14+
*/
15+
public static void main(String[] args) {
16+
List<Number> items = new ArrayList<Number>();
17+
items.add(3);
18+
items.add(8.0);
19+
items.add(989898979879879879l);
20+
21+
System.out.println(items);
22+
23+
List<Double> more = Arrays.asList(3.0, 3.9, 8.0);
24+
25+
Wildcards.copy(items, more);
26+
27+
System.out.println(items);
28+
}
29+
30+
31+
public static <T> void copy(List<? super T> dest, List<? extends T> src) {
32+
for(T element : src) {
33+
dest.add(element);
34+
}
35+
}
36+
37+
}

0 commit comments

Comments
 (0)