Docsity

Java Lists: Implementation and Comparison between Java and GJ - Prof. Alan L. Sussman, Study notes of Programming Languages

Partial preview of the text

Download Java Lists: Implementation and Comparison between Java and GJ - Prof. Alan L. Sussman and more Study notes Programming Languages in PDF only on Docsity!

GJ:

Making

Java

easier

to

t

yp

e,

and

easier

to

t

yp

e

Gilad

Bracha

JavaSoft,

Sun

Microsystems

Ma

rtin

Odersky

Universit

y

of

South

Australia

David

Stoutamire,

JavaSoft,

Sun

Microsystems

Philip

W

adler,

Bell

Labs,

Lucent

T

echnologies

Lists

in

Java

and

GJ

in

Java

in

GJ

integer

list

List
List

string

list

List
List

string

list

list

List
List>

Collection

classes

in

Java



Collection



Set

(HashSet,

T

reeSet)



List

(Arra

yList,

Link

edList)



Map

(HashMap,

T

reeMap)



Iterato

r,

ListIterato

r



Compa

rato

r,

Compa

rable

The

basics:

Lists

in

Java

and

GJ

Lists

in

GJ

interface List { public void add (A x); public Iterator iterator (); interface} Iterator { public A next (); public boolean hasNext ();

T

ranslating

lists

from

GJ

to

Java

interface List { public void add (Object x); public Iterator iterator (); interface} Iterator { public Object next (); public boolean hasNext ();

Accessing

lists

in

GJ

integer list List xs = new LinkedList(); xs.add(new Integer(0)); Integer x = xs.iterator().next(); // space string list list List ys = new LinkedList(); ys.add("zero"); List> yss = new LinkedList>(); yss.add(ys); String y = yss.iterator().next().iterator().next(); // space string list treated as integer list Integer z = ys.iterator().next(); // compile-time error

Implementing

lists

in

Java

class LinkedList implements List { protected class Node { Object elt; Node next; Node (Object e) { elt=e; next=null; } protected} Node h, t; public LinkedList () { h=new Node(null); t=h; } public void add (Object elt) { t.next=new Node(elt); t=t.next; } public Iterator iterator () { return new Iterator () { protected Node p=h.next; public boolean hasNext () { return p!=null; } public Object next () { Object e=p.elt; p=p.next; return e; }

Generic

metho

ds,

Bridges,

and

Bounds

Generic

metho

ds:

First

in

Java

class Lists { public static Object first (List xs) { return xs.iterator().next();

Listspace} ys = new LinkedList(); ys.add("zero"); List yss = new LinkedList>(); yss.add(ys); String y = (String)Lists.first((List)Lists.first(ys s));

Bridges:

Compa

ring

b

ytes

in

Java

interface Comparable { public int compareTo (Object that); class} Byte implements Comparable { private byte value; public Byte (byte value) { this.value = value; } public byte byteValue () { return value; } public int compareTo (Byte that) { // overloading return this.value

that.value; public} int compareTo (Object that) { // overriding

bridge return this.compareTo((Byte)that);

Bridges:

Compa

ring

b

ytes

in

GJ

interface Comparable { public int compareTo (A that); class} Byte implements Comparable { private byte value; public Byte (byte value) { this.value = value; } public byte byteValue () { return value; } public int compareTo (Byte that) { return this.value

that.value;

Bounds:

Maximum

in

GJ

class Lists { public static > A max (List xs) { Iterator xi = xs.iterator();

A

w

xi.next(); while (xi.hasNext()) {

A

x

xi.next(); if (w.compareTo(x) <

w

x; return} w;

List} xs = new LinkedList(); xs.add(new Byte(0)); Byte x = Lists.max(xs); List ys = new LinkedList(); ys.add(new Boolean(false)); Boolean y = Lists.max(ys); // compile-time error

Retro

tting

and

Ra

w

t

yp

es

(Evolution

vs.

Revolution)