gpt4 book ai didi

java - 扩展 AbstractMap 让我看到 put 方法的 UnsupportedOperationException

转载 作者:太空宇宙 更新时间:2023-11-04 10:52:48 24 4
gpt4 key购买 nike

我正在尝试实现一个个人的abstractMap。我的项目中有两种类型的 map ,一种 <String , List<Box>>和一个<String , Box> (盒子是一个类,用于将元素及其数量封装起来)。但是当我尝试 map 的 put 方法时,它向我显示“UnsupportedOperationException - 如果此 map 不支持 put 操作”

这是 map 的抽象类

public abstract class TamagotchiMap<X> extends AbstractMap<String, X> {

@Override
public abstract Set<Entry<String, X>> entrySet();


public abstract void attachCategories(Set<String> categories);


public abstract void addItemForCategory(String category, Box box);


public abstract String getCategory(Box box);


public Collection<String> getAllCategories() {
return this.keySet();
}



}

还有两个类扩展了这个类

1)

public class InventoryMainMap extends TamagotchiMap<Box> {

private final Set<Entry<String, Box>> entry = new HashSet<>();

@Override
public Set<Entry<String, Box>> entrySet() {
return entry;
}


@Override
public void attachCategories(final Set<String> categories) {
for (String category: categories) {
this.put(category, null);
}
}



/**
*
* @return main item for this category
* @param category is the category of item
*/
public Box getMainItem(final String category) {
return this.get(category);
}

@Override
public String getCategory(final Box box) {
for (String category: this.getAllCategories()) {
if (this.get(category).containsItem(box.getItem())) {
return category;
}
}
return null;
}

@Override
public void addItemForCategory(final String category, final Box box) {
if (this.containsKey(category)) {
this.replace(category, box);
}
}
}

2)

public class ItemContainerMap extends TamagotchiMap<List<Box>> {

private final Set<Entry<String, List<Box>>> entry = new HashSet<>();

@Override
public Set<Entry<String, List<Box>>> entrySet() {
return entry;
}

@Override
public String getCategory(final Box box) {
for (String category: this.getAllCategories()) {
for (Box boxIterator: this.get(category)) {
if (boxIterator.containsItem(box.getItem())) {
return category;
}
}
}
return null;
}

@Override
public void addItemForCategory(final String category, final Box box) {
if (this.containsKey(category)) {
if (!this.get(category).contains(box)) {
System.out.println("nuovo item");
this.get(category).add(box);
} else {
System.out.println("item esistente");
this.get(category).stream().filter(iterBox -> iterBox.equals(box)).forEach(iterBox -> iterBox.increaseQuantity());
}
} else {
throw new NoSuchElementException();
}

}

@Override
public void attachCategories(final Set<String> categories) {
for (String category: categories) {
this.put(category, new LinkedList<Box>());
}
}

}

当我这样做的时候

class Test {

TamagotchiMap map = new ItemContainerMap();
map.put(string, box);

}

它让我看到了我已经说过的异常。好像看跌期权无法使用,你知道我错在哪里吗?

最佳答案

抛出 UnsupportedOperationException 是从 AbstractMap 继承的 put(K, V) 的默认实现。您可以自己实现它,或者让您的 TamagotchiMap 扩展一个具体的 Map 类,例如 HashMap 而不是 AbstractMap

关于java - 扩展 AbstractMap 让我看到 put 方法的 UnsupportedOperationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47622882/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com