JmanxC
JmanxC

Reputation: 387

Returning a linked list containing keys from another Linked List

I have the following problem. I have a linked list or positional list containing objects called Entry. Each entry stores a key and a value pair. I want to make another linked list to grab from that list just the keys. I have come up with a method to do so,however for some reason they order in which I added them is not represented when I print the key list. I have the following two methods:

public PositionalList1<K> keySet() //I prnted position by position while adding and it worked. I also tested addAfter with positionalList1 class and it worked
{
    PositionInterface<Entry> iterator = map.first(); //gets first position in the Entry List
    PositionInterface<K> first = keyList.addFirst((K)iterator.getData().getKey()); //adds to list containing just keys the key from the Entry list(getData is just a method that returns whatever object is stored at the node, in this case a Entry Object)
    iterator = map.after(iterator); //go to next node in Entry list

    for(int i=0;i<size-1;i++) //get rest of keys
    {
        PositionInterface<K> p = keyList.addAfter(first,(K)iterator.getData().getKey());
        iterator = map.after(iterator);

    }
    return keyList;
}

public void printKeySet(PositionalList1 list) //print key list
{
    PositionInterface p = list.first();
    for(int i=0; i<list.size();i++)
    {
        System.out.println("Key : " + p.getData());
        p = list.after(p);
    }
}

The keySet() method returns the list containing only keys, while the printKeySet takes the result of KeySet() and prints the entire key list. I have tested this using the following main program:

OrderedMapL<Integer,String> map2 = new OrderedMapL<Integer,String>();
    map2.put(2,"A");//adds (2,A)
    map2.put(5,"B");//adds(5,B)
    map2.put(1,"C");//adds(1,C)
    map2.put(4,"D");//adds(4,D)
    map2.put(3,"E");//adds(3,E)
    map2.remove(2); //removes (2,A)

This resulted in a ordered list of (1,C) (2,A) .. etc and the entries themselves print fine in this order. The problem arises when calling the following:

PositionalList1<Integer> keyList = map2.keySet();
map2.printKeySet(keyList);

For some reason the keys are printing in the order: 1,5,4,3 instead of 1,3,4,5 and I have no idea why. Any help would be appreciated.

Upvotes: 1

Views: 960

Answers (1)

Rafał Spryszyński
Rafał Spryszyński

Reputation: 684

The problem is with this line:

PositionInterface<K> p = keyList.addAfter(first,(K)iterator.getData().getKey());

You shouldn't add after first. That is why You get wrong order.

If the keys are 1, 3, 4, 5 then You are adding it like this:

  1. Add 1 as first element 1
  2. Add 3 after first element 1 3
  3. Add 4 after first element 1 4 3
  4. Add 5 after first element 1 5 4 3

It is because You are adding it after the first element.

If I understand Your code correctly You should change it like this:

// I suspect that #addFirst method returns added list element
PositionInterface<K> last = keyList.addFirst((K) iterator.getData().getKey());
iterator = map.after(iterator);

for(int i = 0; i < size - 1; i++)
{
    // I suspect that #addAfter method returns added list element
    PositionInterface<K> last = keyList.addAfter(last, (K) iterator.getData().getKey());
    iterator = map.after(iterator);

}

Upvotes: 1

Related Questions