//MultiValuedTreeMap.java
import java.util.Iterator;
import java.util.LinkedList;
import java.util.TreeMap;
//import java.util.ArrayList;
public class MultiValuedTreeMap<K, V> extends TreeMap<K,
LinkedList<V>> implements Iterable<Pair<K, V>>
{
private static final long serialVersionUID =
-6229569372944782075L;
public void add(K k, V v) { // Problem 1 method
// empty linked list, with
key=k
if (!containsKey(k)) {
put(k, new LinkedList<V>());
}
// adding v to the linked list associated with key k
get(k).add(v);
}
public V removeFirst(K k) {
// Problem 2 method
// if key is not present, returning null
if
(!containsKey(k))
{
return null;
}
// removing the first value from linked
list associated with key=k
V value = get(k).removeFirst();
// if list just become empty, removing it from the map
if (get(k).isEmpty()) {
super.remove(k);
}
// returning the value
return value;
}
public Pair<K, V> removeFirst()
{ // Problem 3
// TODO complete this method
return null;
}
// Return iterator over keyvalue pairs contained
inMultiValuedTreeMap
//return an iterator over keyvalue pairs contained in
MultiValuedTreeMap
public
Iterator<Pair<K, V>>
iterator() { // Problem 6
// TODO complete this method
return null;
}
public static void main(String[] args) {
MultiValuedTreeMap<Integer,
String> m = new MultiValuedTreeMap<Integer,
String>();
m.add(1, "b");
m.add(1, "a");
m.add(0, "c");
System.out.println(m);
System.out.println(m.removeFirst(1));
System.out.println(m);
System.out.println(m.removeFirst());
System.out.println(m.removeFirst());
System.out.println(m.removeFirst());
m.add(1, "b");
m.add(1, "a");
m.add(0, "c");
System.out.println(m);
for (Pair<Integer, String> p
: m) {
System.out.println(p);
}
}
}
//Pair.java
public class Pair<F, S> {
protected F first;
protected S second;
public Pair(F first, S second) {
this.first = first;
this.second = second;
}
public F first() {
return first;
}
public S second() {
return second;
}
@Override
public String toString() {
return "(" + first + ", " + second
+ ")";
}
}
In MultiValuedTreeMap.java,
implement the removeFirst() method using the removeFirst(K k) method mentioned in Problem 2. The removeFirst() method must remove the first value associated with the first key and then return a Pair containing the first key and its first value. This method must return null if it is invoked on an empty MultiValuedTreeMap. If the removeFirst() method is implemented correctly and if the following code: System.out.println(m.removeFirst (); System.out.println(m.removeFirst O; System.out.println(m.removeFirst (); is executed right after the code in Problem 2, the standard output stream will display:
(0, c)
(1, a)
null
Here is the completed code for this problem including the iterator method. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
// MultiValuedTreeMap.java
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.TreeMap;
public class MultiValuedTreeMap<K, V> extends TreeMap<K, LinkedList<V>>
implements Iterable<Pair<K, V>> {
private static final long serialVersionUID = -6229569372944782075L;
public void add(K k, V v) { // Problem 1 method
// empty linked list, with key=k
if (!containsKey(k)) {
put(k, new LinkedList<V>());
}
// adding v to the linked list associated with key k
get(k).add(v);
}
public V removeFirst(K k) {
// Problem 2 method
// if key is not present, returning null
if (!containsKey(k)) {
return null;
}
// removing the first value from linked list associated with key=k
V value = get(k).removeFirst();
// if list just become empty, removing it from the map
if (get(k).isEmpty()) {
super.remove(k);
}
// returning the value
return value;
}
public Pair<K, V> removeFirst() { // Problem 3
if (!isEmpty()) {
// fetching first key
K key = firstKey();
// removing first value associated with key and storing in value
// variable
V value = removeFirst(key);
// creating a pair object with these key, value pair and returning
// it
Pair<K, V> pair = new Pair<K, V>(key, value);
return pair;
}
return null; // empty map
}
// Return iterator over keyvalue pairs contained inMultiValuedTreeMap
// return an iterator over keyvalue pairs contained in MultiValuedTreeMap
public Iterator<Pair<K, V>> iterator() { // Problem 6
// creating an array list of pair objects
ArrayList<Pair<K, V>> list = new ArrayList<Pair<K, V>>();
// looping through each key in this map
for (K key : keySet()) {
// looping through each value for current key
for (V value : get(key)) {
// creating a pair, adding to list
Pair p = new Pair<K, V>(key, value);
list.add(p);
}
}
// returning an iterator to the list
return list.iterator();
}
public static void main(String[] args) {
MultiValuedTreeMap<Integer, String> m = new MultiValuedTreeMap<Integer, String>();
m.add(1, "b");
m.add(1, "a");
m.add(0, "c");
System.out.println(m);
System.out.println(m.removeFirst(1));
System.out.println(m);
System.out.println(m.removeFirst());
System.out.println(m.removeFirst());
System.out.println(m.removeFirst());
m.add(1, "b");
m.add(1, "a");
m.add(0, "c");
System.out.println(m);
for (Pair<Integer, String> p : m) {
System.out.println(p);
}
}
}
/*OUTPUT*/
{0=[c], 1=[b, a]}
b
{0=[c], 1=[a]}
(0, c)
(1, a)
null
{0=[c], 1=[b, a]}
(0, c)
(1, b)
(1, a)
//MultiValuedTreeMap.java import java.util.Iterator; import java.util.LinkedList; import java.util.TreeMap; //import java.util.ArrayList; public class MultiValuedTreeMap<K, V> extends TreeMap<K, LinkedList<V>>...
import java.util.Iterator; import java.util.LinkedList; import java.util.TreeMap; import java.util.ArrayList; public class MultiValuedTreeMap<K, V> extends TreeMap<K, LinkedList<V>> implements Iterable<Pair<K, V>> { private static final long serialVersionUID = -6229569372944782075L; public void add(K k, V v) { if (!containsKey(k)) { put(k, new LinkedList<V>()); } get(k).add(v); } public V removeFirst(K k) { if (!containsKey(k)) { return null; } V value = get(k).removeFirst(); if (get(k).isEmpty()) { super.remove(k); } return value; }...
P1 is below package p6_linkedList; import java.util.*; public class LinkedList { public Node header; public LinkedList() { header = null; } public final Node Search(int key) { Node current = header; while (current != null && current.item != key) { current = current.link; } return current; } public final void Append(int newItem) { Node newNode = new Node(newItem); newNode.link = header; header = newNode; } public final Node Remove() { Node x = header; if (header != null) { header...
Implement the missing methods in java! Thanks! import java.util.Iterator; import java.util.NoSuchElementException; public class HashTableOpenAddressing<K, V> implements DictionaryInterface<K, V> { private int numEntries; private static final int DEFAULT_CAPACITY = 5; private static final int MAX_CAPACITY = 10000; private TableEntry<K, V>[] table; private double loadFactor; private static final double DEFAULT_LOAD_FACTOR = 0.75; public HashTableOpenAddressing() { this(DEFAULT_CAPACITY, DEFAULT_LOAD_FACTOR); } public HashTableOpenAddressing(int initialCapacity, double loadFactorIn) { numEntries = 0; if (loadFactorIn <= 0 || initialCapacity <= 0) { throw new IllegalArgumentException("Initial capacity and load...
import java.util.List; import java.util.ArrayList; import java.util.LinkedList; public class ListPractice { private static final int[] arr = new int[100000]; public static void main(String[] args) { for(int i=0; i<100000; i++) arr[i] = i; //TODO comment out this line LinkedList<Integer> list = new LinkedList<Integer>(); //TODO uncomment this line //List<Integer> list = new ArrayList<Integer>(); //TODO change the rest of the...
//LinkedList import java.util.Scanner; public class PoD { public static void main( String [] args ) { Scanner in = new Scanner( System.in ); LinkedList teamList = new LinkedList(); final int TEAM_SIZE = Integer.valueOf(in.nextLine()); for (int i=0; i<TEAM_SIZE; i++) { String newTeamMember = in.nextLine(); teamList.append(newTeamMember); } while (in.hasNext()) { String removeMember = in.nextLine(); teamList.remove(removeMember); } System.out.println("FINAL TEAM:"); System.out.println(teamList); in.close(); System.out.print("END OF OUTPUT"); } } =========================================================================================== //PoD import java.util.NoSuchElementException; /** * A listnked list is a sequence of nodes with...
Please implement MyMaxPriorityQueue below. Thanks import net.datastructures.Entry; public class MyMaxPriorityQueue<K, V> { @Override public int size() { // TODO Auto-generated method stub return 0; } @Override public Entry<K, V> insert(K key, V value) throws IllegalArgumentException { // TODO Auto-generated method stub return null; } @Override public Entry<K, V> max() { // TODO Auto-generated method stub return null; } ...
CAN SOMEONE PLEASE SOLVE THIS? THANK YOU. FINALTREE.JAVA: import java.util.LinkedList; public class FinalTree<E extends Comparable<? super E>> { private Node<E> root; private int size; public FinalTree() { root = null; size = 0; } public boolean isEmpty() { return size == 0; } public void add(E newItem) { if (isEmpty()) { root = new Node<E>(newItem); } else { Node<E> parent = null; Node<E> temp = root; int result = 0; while (temp != null) { parent = temp; result =...
import java.util.LinkedList; public class testprintOut { private static LinkedList[] array; public static void main(String[] args) { int nelems = 5; array = new LinkedList[nelems]; for (int i = 0; i < nelems; i++) { array[i] = new LinkedList<String>(); } array[0]=["ab"]; System.out.println(array[0]); } } //I want to create array of linked lists so how do I create them and print them out efficiently? //Syntax error on token "=", Expression expected after this token Also, is this how I can put them...
package week_4; import input.InputUtils; import java.lang.reflect.Array; import java.util.ArrayList; import java.util.Random; import static input.InputUtils.positiveIntInput; import static input.InputUtils.yesNoInput; /** Write a program to roll a set of dice. Generate a random number between 1 and 6 for each dice to be rolled, and save the values in an ArrayList. Display the total of all the dice rolled. In some games, rolling the same number on all dice has a special meaning. In your program, check if all dice have the same value,...
Complete the code: package hw4; import java.io.File; import java.io.IOException; import java.util.LinkedList; import java.util.Scanner; /* * This class is used by: * 1. FindSpacing.java * 2. FindSpacingDriver.java * 3. WordGame.java * 4. WordGameDriver.java */ public class WordGameHelperClass { /* * Returns true if an only the string s * is equal to one of the strings in dict. * Assumes dict is in alphabetical order. */ public static boolean inDictionary(String [] dict, String s) { // TODO Implement using binary search...