Skip to content

Commit 8604c05

Browse files
committed
complete linked list and printable linked list
1 parent ae30ead commit 8604c05

4 files changed

Lines changed: 86 additions & 3 deletions

File tree

src/section21/MapperFn.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package section21;
22

33
/**
4-
* This interface encapsulates a function that maps type T to U, and it can be used for funciton-as-value as in
4+
* This interface encapsulates a function that maps type T to U, and it can be used for function-as-value as in
55
* functional programming.
66
*
7-
* Java 8 now includes Function<T,U> under java.util.function package.
7+
* Java 8 now includes Function<T,U> under java.util.function package for exactly this.
88
*/
99
public interface MapperFn<T,U> {
1010

src/section21/MyLinkedList.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
/**
66
* This class implements a generic doubly-linked list with common operations.
77
*/
8-
public class MyLinkedList<T> implements Iterable<T> {
8+
public class MyLinkedList<T> implements MyList<T> {
99

1010
/**
1111
* Static nested class that presents a node in the linked list; also generic.
@@ -147,6 +147,15 @@ public T getTailValue() {
147147
return tail.value;
148148
}
149149

150+
/* Creates a new linked list by mapping each of its elements */
151+
public <U> MyList<U> map(MapperFn<T,U> fn) {
152+
MyLinkedList<U> newList = new MyLinkedList<>();
153+
for (T elem : this) {
154+
newList.append(fn.call(elem));
155+
}
156+
return newList;
157+
}
158+
150159
/* Get an iterator for the current state of the linked list */
151160
public Iterator<T> iterator() {
152161
return new Iterator<T>() {

src/section21/MyPrintableList.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package section21;
2+
3+
import java.io.PrintWriter;
4+
5+
/**
6+
* A generic printable list that extends MyLinkedList<T> and adds a print() method to make it printable.
7+
* Type argument constraint (`T extends PrintableObj`) makes sure that each element is printable.
8+
*/
9+
public class MyPrintableList<T extends PrintableObj> extends MyLinkedList<T> implements PrintableObj, MyList<T> {
10+
11+
/* This method prints all contents of the linked list to the PrintWriter */
12+
public void print(PrintWriter fs) {
13+
for (T elem : this) { // this syntax is possible since MyLinkedList implements MyList<T> (hence Iterable<T>)
14+
fs.print(elem);
15+
}
16+
}
17+
18+
}
19+
20+
/**
21+
* This interface represents an object that has print method that prints itself to the PrintWriter as provided.
22+
*/
23+
interface PrintableObj {
24+
25+
/**
26+
* Prints itself to the provided PrintWriter.
27+
* @param fs target PrintWriter
28+
*/
29+
void print(PrintWriter fs);
30+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package section21;
2+
3+
import java.io.PrintWriter;
4+
5+
/**
6+
* This example uses MyPrintableList declared earlier.
7+
*/
8+
public class MyPrintableListExample {
9+
10+
public static void main(String[] argv) {
11+
MyPrintableList<PrintableInteger> myList = new MyPrintableList<>();
12+
myList.append(new PrintableInteger(2));
13+
myList.append(new PrintableInteger(3));
14+
myList.append(new PrintableInteger(5));
15+
myList.append(new PrintableInteger(7));
16+
17+
PrintWriter stdout = new PrintWriter(System.out);
18+
PrintWriter stderr = new PrintWriter(System.err);
19+
myList.print(stdout);
20+
myList.print(stderr);
21+
stdout.flush();
22+
stderr.flush();
23+
}
24+
}
25+
26+
/**
27+
* PrintableInteger simply holds an immutable integer, and defines a toString() method to be used by print(PrintWriter).
28+
*/
29+
class PrintableInteger implements PrintableObj {
30+
31+
public final int val; // intentionally immutable
32+
33+
public PrintableInteger(int val) {
34+
this.val = val;
35+
}
36+
37+
public void print(PrintWriter fs) {
38+
fs.write(val); // what's written to `fs` is determined by its toString method
39+
}
40+
41+
public String toString() {
42+
return String.format("PrintableInteger(val=%d)", val);
43+
}
44+
}

0 commit comments

Comments
 (0)