Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions EclipseChapterProjects/Ch1/.classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="lib" path="lib/hamcrest-core-1.3.jar"/>
<classpathentry kind="lib" path="lib/hamcrest-library-1.3.jar"/>
<classpathentry kind="lib" path="lib/junit-4.12.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
17 changes: 17 additions & 0 deletions EclipseChapterProjects/Ch1/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>Ch1</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
11 changes: 11 additions & 0 deletions EclipseChapterProjects/Ch1/.settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.8
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.allendowney.thinkdast;

import java.util.LinkedList;
import java.util.List;

public class ListClientExample {
@SuppressWarnings("rawtypes")
private List list;

@SuppressWarnings("rawtypes")
public ListClientExample() {
list = new LinkedList();
}

@SuppressWarnings("rawtypes")
public List getList() {
return list;
}

public static void main(String[] args) {
ListClientExample lce = new ListClientExample();
@SuppressWarnings("rawtypes")
List list = lce.getList();
System.out.println(list);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.allendowney.thinkdast;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.CoreMatchers.*;

import java.util.ArrayList;
import java.util.List;

import org.junit.Test;

/**
* @author downey
*
*/
public class ListClientExampleTest {

/**
* Test method for {@link ListClientExample}.
*/
@Test
public void testListClientExample() {
ListClientExample lce = new ListClientExample();
@SuppressWarnings("rawtypes")
List list = lce.getList();
assertThat(list, instanceOf(ArrayList.class) );
}

}
12 changes: 12 additions & 0 deletions EclipseChapterProjects/Ch10/.classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="lib" path="lib/commons-math3-3.6.jar"/>
<classpathentry kind="lib" path="lib/hamcrest-core-1.3.jar"/>
<classpathentry kind="lib" path="lib/hamcrest-library-1.3.jar"/>
<classpathentry kind="lib" path="lib/jcommon-1.0.23.jar"/>
<classpathentry kind="lib" path="lib/jfreechart-1.0.19.jar"/>
<classpathentry kind="lib" path="lib/junit-4.12.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
17 changes: 17 additions & 0 deletions EclipseChapterProjects/Ch10/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>Ch10</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
12 changes: 12 additions & 0 deletions EclipseChapterProjects/Ch10/.settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.release=disabled
org.eclipse.jdt.core.compiler.source=1.8
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
package com.allendowney.thinkdast;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
* Implementation of a Map using a collection of MyLinearMap, and
* using `hashCode` to determine which map each key should go in.
*
* @author downey
* @param <K>
* @param <V>
*
*/
public class MyBetterMap<K, V> implements Map<K, V> {

// MyBetterMap uses a collection of MyLinearMap
protected List<MyLinearMap<K, V>> maps;

/**
* Initialize the map with 2 sub-maps.
*
*/
public MyBetterMap() {
makeMaps(2);
}

/**
* Makes a collection of `k` MyLinearMap
*
* @param k
*/
protected void makeMaps(int k) {
maps = new ArrayList<MyLinearMap<K, V>>(k);
for (int i=0; i<k; i++) {
maps.add(new MyLinearMap<K, V>());
}
}

@Override
public void clear() {
// clear the sub-maps
for (int i=0; i<maps.size(); i++) {
maps.get(i).clear();
}
}

/**
* Uses the hashCode to find the map that would/should contain the given key.
*
* @param key
* @return
*/
protected MyLinearMap<K, V> chooseMap(Object key) {
int index = key==null ? 0 : Math.abs(key.hashCode()) % maps.size();
return maps.get(index);
}

@Override
public boolean containsKey(Object target) {
// to find a key, we only have to search one map
// TODO: FILL THIS IN!
return false;
}

@Override
public boolean containsValue(Object target) {
// to find a value, we have to search all map
// TODO: FILL THIS IN!
return false;
}

@Override
public Set<Map.Entry<K, V>> entrySet() {
throw new UnsupportedOperationException();
}

@Override
public V get(Object key) {
MyLinearMap<K, V> map = chooseMap(key);
return map.get(key);
}

@Override
public boolean isEmpty() {
return size() == 0;
}

@Override
public Set<K> keySet() {
// add up the keySets from the sub-maps
Set<K> set = new HashSet<K>();
for (MyLinearMap<K, V> map: maps) {
set.addAll(map.keySet());
}
return set;
}

@Override
public V put(K key, V value) {
MyLinearMap<K, V> map = chooseMap(key);
return map.put(key, value);
}

@Override
public void putAll(Map<? extends K, ? extends V> map) {
for (Map.Entry<? extends K, ? extends V> entry: map.entrySet()) {
put(entry.getKey(), entry.getValue());
}
}

@Override
public V remove(Object key) {
MyLinearMap<K, V> map = chooseMap(key);
return map.remove(key);
}

@Override
public int size() {
// add up the sizes of the sub-maps
int total = 0;
for (MyLinearMap<K, V> map: maps) {
total += map.size();
}
return total;
}

@Override
public Collection<V> values() {
// add up the valueSets from the sub-maps
Set<V> set = new HashSet<V>();
for (MyLinearMap<K, V> map: maps) {
set.addAll(map.values());
}
return set;
}

/**
* @param args
*/
public static void main(String[] args) {
Map<String, Integer> map = new MyBetterMap<String, Integer>();
map.put("Word1", 1);
map.put("Word2", 2);
Integer value = map.get("Word1");
System.out.println(value);

for (String key: map.keySet()) {
System.out.println(key + ", " + map.get(key));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.allendowney.thinkdast;
/**
*
*/


import org.junit.Before;

/**
* @author downey
*
*/
public class MyBetterMapTest extends MyLinearMapTest {

/**
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception {
map = new MyBetterMap<String, Integer>();
map.put("One", 1);
map.put("Two", 2);
map.put("Three", 3);
map.put(null, 0);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.allendowney.thinkdast;
/**
*
*/


import java.util.List;
import java.util.Map;

/**
* Implementation of a HashMap using a collection of MyLinearMap and
* resizing when there are too many entries.
*
* @author downey
* @param <K>
* @param <V>
*
*/
public class MyHashMap<K, V> extends MyBetterMap<K, V> implements Map<K, V> {

// average number of entries per map before we rehash
protected static final double FACTOR = 1.0;

@Override
public V put(K key, V value) {
V oldValue = super.put(key, value);

//System.out.println("Put " + key + " in " + map + " size now " + map.size());

// check if the number of elements per map exceeds the threshold
if (size() > maps.size() * FACTOR) {
rehash();
}
return oldValue;
}

/**
* Doubles the number of maps and rehashes the existing entries.
*/
/**
*
*/
protected void rehash() {
// TODO: FILL THIS IN!
}

/**
* @param args
*/
public static void main(String[] args) {
Map<String, Integer> map = new MyHashMap<String, Integer>();
for (int i=0; i<10; i++) {
map.put(new Integer(i).toString(), i);
}
Integer value = map.get("3");
System.out.println(value);
}
}
Loading