Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I iterate over an Iterable object when the type is not known?

For a homework assignment, I need to implement my own PriorityQueue and PriorityQueueSort. I used generics to get it working without the sort function, but now I'm stuck here..

public static void PriorityQueueSort(Iterable<?> list, 
    PriorityQueue<?,?> pq) {
  if (!pq.isEmpty()) {
    throw new IllegalArgumentException("Non-Empty PriorityQueue");
  }

  for (Object obj : list) {

  }
}

I need to pass in a list and an empty PriorityQueue, so my best guess at how to do this is just above. How should I attack this so that I can iterate through the list with unknown type, and add each element in that list with the proper type into the priority queue?


Edit:

Here are a few more details since it was determined that I didn't include enough information.

I have a custom PriorityQueue class, and a custom Entry class that holds a key of type K, and a value of type V.

I need to be able to take any iterable list with any type T and iterate through it, taking each item and add it to an initially empty PriorityQueue as a key with null value. I then need to continuously call removeMin() on my PriorityQueue and add it in order back into the same list object.

public class PriorityQueue<K extends Comparable<? super K>,V> {

  private Entry<K,V> _head;
  private Entry<K,V> _tail;
  private int _size;

  public PriorityQueue() {
    this._head = null;
    this._tail = null;
    this._size = 0;
  }

  public int size() {
    return _size;
  }

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

  public Entry<K,V> min() {
    if (_head == null) {
      return null;
    }
    Entry<K,V> current = _head;
    Entry<K,V> min = _head;;

    while (current != null) {
      if (current.compareTo(min) < 0) {
        min = current;
      }
      current = current.getNext();
    }
    return min;
  }

  public Entry<K,V> insert(K k, V x) {
    Entry<K,V> temp = new Entry<K,V>(k,x);
    if (_tail == null) {
      _tail = temp;
      _head = temp;
    }
    else {
      _tail.setNext(temp);
      temp.setPrev(_tail);
      _tail = temp;
    }
    return temp;
  }

  public Entry<K,V> removeMin() {
    Entry<K,V> smallest = min();
    smallest.getPrev().setNext(smallest.getNext());
    smallest.getNext().setPrev(smallest.getPrev());

    return smallest;
  }

  public String toString() {
    return null;
  }

  public static <K> void PriorityQueueSort(Iterable<? extends K> list,
        PriorityQueue<? super K, ?> queue) {

      for (K item : list) {
          queue.insert(item, null);
      }

      list.clear();
  }

  public static void main(String[] args) {
    PriorityQueue<Integer, Integer> pq = 
        new PriorityQueue<Integer, Integer>();

    pq.insert(4, 2);
    pq.insert(5, 1);


    System.out.println(pq.min().toString());
  }
}
like image 270
Mirrana Avatar asked Mar 08 '13 13:03

Mirrana


1 Answers

What you've got at the moment doesn't make sense in terms of the method signature - it would let you pass in a List<Button> and a PriorityQueue<String> for example.

I suspect you actually want something like:

public static <T> void prioritySortQueue(Iterable<? extends T> iterable,
    PriorityQueue<? super T> queue) {

    for (T item : iterable) {
        queue.add(item);
    }
}

Note that the variance here just gives more flexibility - you could have a List<Circle> but a PriorityQueue<Shape> for example, and it's still type-safe.

EDIT: Now that we have more details, I think you want something like this:

public static <K> void prioritySortQueue(Iterable<? extends K> iterable,
    PriorityQueue<? super K, ?> queue) {

    for (T item : iterable) {
        queue.put(item, null);
    }
}

(Assuming you have a put method. We still don't know what your PriorityQueue class looks like.)

like image 118
Jon Skeet Avatar answered Sep 18 '22 19:09

Jon Skeet