迭代自定义对象的 Hashset 会抛出 ClassCastException
Iterating over a Hashset of custom objects throws ClassCastException
这是我的摘要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;
}
}
这是带有主要方法的启动器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");
}
}
}
}
这里是错误
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)
您的 keysAsSet
目前 returns 是 Set<Entry<K, V>>
,而不是应有的 Set<K>
。更改它:
@Override
public Set<K> keysAsSet() {
HashSet<K> hashSet = new HashSet<>();
for (Entry<K, V> entry: entries) {
hashSet.add(entry.getKey());
}
return hashSet;
}
这是我的摘要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;
}
}
这是带有主要方法的启动器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");
}
}
}
}
这里是错误
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)
您的 keysAsSet
目前 returns 是 Set<Entry<K, V>>
,而不是应有的 Set<K>
。更改它:
@Override
public Set<K> keysAsSet() {
HashSet<K> hashSet = new HashSet<>();
for (Entry<K, V> entry: entries) {
hashSet.add(entry.getKey());
}
return hashSet;
}