Search code examples
javasethashsetclasscastexception

Iterating over a Hashset of custom objects throws ClassCastException


This is my abstract class which contains the keysAsSet() method implemented from the interface Readablemap

public abstract class AbstractReadableMap<K, V> implements ReadableMap {
    protected Entry<K, V>[] entries;

    public AbstractReadableMap(Entry<K, V>[] entries) {
        this.entries = GenericArrayHelper.copyArray(entries);
    }

    public AbstractReadableMap() {
        this.entries = GenericArrayHelper.newEntryArrayOfSize(10);
    }

    @Override
    public Object getOrThrow(Object key) throws UnknownKeyException {
        for(Entry<K, V> entry :entries)
        {
            if(entry!=null && entry.getKey().equals(key))
                return entry.getValue();
            throw new UnknownKeyException();
        }
        return null;
    }

    @Override
    public ImmutableMap asImmutableMap() {
        return new ImmutableMap<>(entries);
    }

    @Override
    public Set keysAsSet() {
        HashSet<Entry<K, V>> hashSet = new HashSet<>();
        for(Entry<K, V> entry : entries)

        {
            if(entry!=null)
                hashSet.add(entry);
        }
        return hashSet;
    }

}

This is the launcher class with the main method

public class Launcher {

    public static void main(String[] args) {
        MutableMap<String, Integer> map = new MutableMap<>();
        putEntries(map);
        printEntries(map);
        ImmutableMap<String, Integer> immutableMap = map.asImmutableMap();
        printEntries(immutableMap);



    }

    private static void putEntries(WritableMap<String, Integer> writableMap) {
        writableMap.put("sizeInMB", 42);
        writableMap.put("version", 4);
        writableMap.put("yearOfrelease", 2015);
    }

    private static void printEntries(ReadableMap<String, Integer> readableMap) {

        StringBuilder stringBuilder = new StringBuilder();
        for (String keyString : readableMap.keysAsSet()) {
            stringBuilder.setLength(0);
            stringBuilder = new StringBuilder();
            stringBuilder.append(keyString);
            stringBuilder.append(": ");
            try {
                stringBuilder.append(readableMap.getOrThrow(keyString));
                System.out.println(stringBuilder.toString());
            } catch (UnknownKeyException e) {
                System.out.println("Error rip");
            }
        }
    }

}

Here is the error

Exception in thread "main" java.lang.ClassCastException: class Entry cannot be cast to class java.lang.String (Entry is in unnamed module of loader 'app'; java.lang.String is in module java.base of loader 'bootstrap')
    at Launcher.printEntries(Launcher.java:23)
    at Launcher.main(Launcher.java:6)

Solution

  • Your keysAsSet currently returns a Set<Entry<K, V>>, not a Set<K> like it should. Change it:

    @Override
    public Set<K> keysAsSet() {
        HashSet<K> hashSet = new HashSet<>();
        for (Entry<K, V> entry: entries) {
            hashSet.add(entry.getKey());
        }
        return hashSet;
    }