Question

//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>>...

//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

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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)

Add a comment
Know the answer?
Add Answer to:
//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>>...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT