0

I have two stacks:

public static Stack<String> listOfObjects = new Stack<String>();

public static Stack<Integer> objectCounts = new Stack<Integer>();

The stack listOfObjects contains things like:

  • chairs
  • tables
  • couches
  • ...

And the stack objectCounts contains the counts for these variables at the same corresponding location in the stack

  • 4
  • 1
  • 3
  • ...

If I want to sort both stacks by the counts in the stack objectCounts, what would be the fastest way of sorting the stacks?

Thanks a lot!

1
  • 3
    Either use an object that stores the type of object and count of that object, or use a different structure like a map where the keys are object type and the values are the count. It'll be easier than having to related stacks that must match in order. Commented Jan 29, 2014 at 17:09

3 Answers 3

2

I would instead create a Pair Object hold String name and Integer count.

private static final class Pair<K, V> {
    private final K key;
    private final V value;

    private Pair(final K key, final V value) {
        this.key = key;
        this.value = value;
    }

    @Override
    public String toString() {
        return "P: " + key + ", " + value;
    }
}

public static void main(final String[] args) {
    final Stack<Pair<String, Integer>> stack = new Stack<>();
    stack.add(new Pair<String, Integer>("Chair", 2));
    stack.add(new Pair<String, Integer>("Table", 2));
    stack.add(new Pair<String, Integer>("Bed", 44));
    Collections.sort(stack, new Comparator<Pair<String, Integer>>() {
        @Override
        public int compare(final Pair<String, Integer> o1, final Pair<String, Integer> o2) {
            return o2.value.compareTo(o1.value);
        }
    });
    System.out.println(stack);
}

Which returns:

[P: Bed, 44, P: Chair, 2, P: Table, 2]

You could iterate through your Stacks to create a Collections of pairs to sort quite easily to:

final Stack<Pair<String, Integer>> stack = new Stack<>();
for (int i = 0; i < stringStack.size(); i++) {
    stack.push(new Pair<String, Integer>(stringStack.get(0), countStack.get(0)); 
}
Sign up to request clarification or add additional context in comments.

1 Comment

Why not a simple TreeSet which does the ordering for you, given this approach ends up using more space (duplicate count variables in the pair)
0

1) I'd suggest you to save that information in a class (name and count).

2) You would have to create a Comparator, where you compare it by the count of each object.

3) Do you really need to use Stack?. If you can implement it with Array or something, there's a built-in method to sort it, using a Comparator, that you've defined.

If you really need to use Stack, I would copy the elements to an Array, sort it, and then copy to a Stack again.

Also, consider using a PriorityQueue.

Comments

0

"Stack" isn't the right structure to have the elements in sorted order.

But you can still try this approach,
1. Have a TreeMap < int,List < String > > //This will map to count -> list of words with the count
2. You can get the "set" of keys from the SortedMap
3. iterate through each element in set (which inherently is sorted) and push the entire list of words on to stack

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.