简体   繁体   English

迭代自定义对象的 Hashset 会引发 ClassCastException

[英]Iterating over a Hashset of custom objects throws ClassCastException

This is my abstract class which contains the keysAsSet() method implemented from the interface Readablemap这是我的抽象 class 它包含从接口 Readablemap 实现的 keysAsSet() 方法

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这是发射器 class 的主要方法

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)

Your keysAsSet currently returns a Set<Entry<K, V>> , not a Set<K> like it should.您的keysAsSet当前返回Set<Entry<K, V>> ,而不是应有的Set<K> Change it:更改:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM