Java - Iterate through Mapset in descending order, return required output

Malkeir :

I have to following to complete:

Requirements:

In a package coll.MapSet, implement a new class MapSet that extends AbstractMap> and implements Iterable where K represents a generic key, and V represents a generic value:

public class MapSet<K, V> extends AbstractMap<K, HashSet<V>> implements Iterable<V>

The purpose of this class is to store a dictionary of keys K to HashSet objects. Methods

Only three methods are required, but you may choose to add additional helper methods.

addValue

Implement addValue such that calling this method adds the given value to the HashSet associated with the given key. This method must have the following signature:

public void addValue(K, V)

iterator

Implement the iterator such that only values V are traversed. Values are traversed first in descending order of the size of HashSet objects associated with keys, and then in the iterator order for the HashSet.

entrySet

This method must be implemented and overridden from AbstractMap. It should simply return a Set> of the MapSet. Output

Example output is shown below. For the given main:

public static void main(String[] args) {

    MapSet<String, Integer> map = new MapSet<>();

    map.addValue("B", 4);
    map.addValue("A", 0);
    map.addValue("A", 1);
    map.addValue("B", 3);
    map.addValue("A", 2);

    for (Integer value : map) {
        System.out.println(value);
    }
}

The expected output is:

0

1

2

3

4

Values in key "A" are traversed first because it has the largest quantity of elements associated with it. Values within "A" are then traversed in the order of the iterator of its associated HashSet. Next, the traversal is repeated for "B".

I've done the below but am really unsure on where to start with the iterator.

package coll.MapSet;

import java.util.*;
import java.lang.Iterable;

public class MapSet<K, V> extends AbstractMap<K, HashSet<V>> implements Iterable<V> {

    private Map<K, HashSet<V>> contents = new HashMap<>();

    public void addValue(K key, V value) {
            if(contents.containsKey(key)){
                    contents.get(key).add(value);
            }
            else{
                    HashSet<V> set = new HashSet<>();
                    set.add(value);
                    contents.put(key, set);
            }
    }

    @Override
    public Iterator<V> iterator(){
            return new Iterator<>() {
                    private HashMap<K, Integer> sizeMap = new HashMap<>();
                    private List<V> orderedValueList = new ArrayList<>();

//I am really unsure what to do with the iterator to get the required output. Any help would be appreciated. 

                    @Override
                    public boolean hasNext() {
                            return null;
                    }

                    @Override
                    public V next() {
                            if (this.hasNext()) {
                                    return null;
                            } else {
                                    return null;
                            }
                    }
            };

    @Override
    public Set<Entry<K, HashSet<V>>> entrySet(){
            return contents.entrySet();
    }
}
Hülya :

First you should get the collection of values in descending order of the size of HashSet objects associated with keys:

contents.values().stream().sorted(Comparator.comparingInt(Set<?>::size).reversed())

Then flatten the elements and create iterator to get the desired output:

flatMap(Collection::stream).iterator();

Try this:

@Override
public Iterator<V> iterator() {
    return contents.values().stream()
            .sorted(Comparator.comparingInt(Set<?>::size).reversed()).flatMap(Collection::stream).iterator();
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=95510&siteId=1