1. Rewrite the following GrowList class so that it is immutable. (Note: Keep the
ID: 3747264 • Letter: 1
Question
1. Rewrite the following GrowList class so that it is immutable. (Note: Keep the same representation):
import java.util.*;
// GrowList is a mutable list that only gets longer.
public class GrowList <E> {
private Map<Integer,E> values;
public GrowList() { values = new HashMap<Integer,E>();}
// add to the end of the list
public void add(E o) {
values.put(size(), o);
}
// number of values in list
public int size() { return values.size(); }
// get ith value in list
public E get(int i) {
if (! inRange(i)) throw new IndexOutOfBoundsException("GrowList.get");
return values.get(i);
}
// update ith value in list; return previous value or null if none
public E set(int i, E o) {
if (! inRange(i)) throw new IndexOutOfBoundsException("GrowList.set");
return values.put(i, o);
}
// private helper method
private boolean inRange(int i) { return (i >= 0) && (i < size()); }
public String toString() {
if (size() == 0) return "[]";
String result = "[";
for (int i = 0; i < size()-1; i++) {
result += values.get(i) + ",";
}
return result + values.get(size() - 1) + "]";
}
public static void main(String[] args) {
GrowList<String> list = new GrowList<String>();
System.out.println("list is:" + list);
list.add("cat");
System.out.println("list is:" + list);
list.add("dog");
System.out.println("list is:" + list);
list.set(1,"bat");
System.out.println("list is:" + list);
}
}
Explanation / Answer
import java.util.*;
// GrowList is an immutable list that remains same once created.
public final class GrowList <E> {
private final Map<Integer,E> values;
public GrowList(HashMap<Integer,E> paramMap) {
// Doing a deep copy of the passed hashmap to remove any references
HashMap<Integer,E> temp_Map = new HashMap<Integer,E>();
Integer key;
Iterator<Integer> it = paramMap.keySet().iterator();
while(it.hasNext()){
key=it.next();
temp_Map.put(key, paramMap.get(key));
}
// Assigning it to values hashmap
this.values = temp_Map;
}
// number of values in list
public int size() { return values.size(); }
// get ith value in list
public E get(int i) {
if (! inRange(i)) throw new IndexOutOfBoundsException("GrowList.get");
return values.get(i);
}
// private helper method
private boolean inRange(int i) { return (i >= 0) && (i < size()); }
public String toString() {
if (size() == 0) return "[]";
String result = "[";
for (int i = 0; i < size() - 1; i++) {
result += values.get(i) + ",";
}
return result + values.get(size() - 1) + "]";
}
public static void main(String[] args) {
// Creating a hashmap to pass it to GrowList constructor
HashMap<Integer, String> h1 = new HashMap<Integer,String>();
h1.put(0, "cat");
h1.put(1, "dog");
// Creating an immutable object
GrowList<String> list = new GrowList<String>(h1);
// Printing it
System.out.println("list is:" + list);
// Now modifying the original hashmap that we passed to GrowList constructor
h1.put(2, "bat");
// Since, the object is immutable, the value should not change
System.out.println("list is:" + list);
}
}
OUTPUT
list is:[cat,dog]
list is:[cat,dog]
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.