@Override
    public Iterator<E> iterator() {
        final Iterator<Entry<E, Integer>> i = map.entrySet().iterator();
        return new Iterator<E>() {
            private E e;
            private int n = 0;

            public boolean hasNext() {
                return n > 0 || i.hasNext();
            }

            public E next() {
                if (n == 0) { // finished with current element
                    Entry<E, Integer> entry = i.next();
                    e = entry.getKey();
                    n = entry.getValue();
                }
                n--;
                return e;
            }

            public void remove() {
                throw new UnsupportedOperationException();
            }
        };
    }