我遇到了麻烦,我知道哪里出了问题,但是无法将其放入代码中。我目前正在纸牌上工作,我想从LinkedHashMap
的纸牌等级中删除特定的纸牌套装。我到处都在搜索针对我的特定问题的特定解决方案,但是找不到。
我尝试使用entrySet,迭代器和逻辑解决方案,但是remove()
操作似乎删除了value
的所有记录,而无视key
。
下面的示例演示了我去除5颗心的尝试。
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;
public class Main {
public static void main(String[] args) {
Deck deck = new Deck();
System.out.println(deck.deck);
//ENTRYSET
for (Map.Entry<String, LinkedList<String>> entry : deck.deck.entrySet()) {
System.out.println(entry.getValue());
System.out.println(entry.getKey());
if (entry.getKey().contains("5")) {
if (entry.getValue().contains("Hearts")) {
entry.getValue().remove("Hearts");
break;
}
}
}
System.out.println(deck.deck);
//LOGIC SOLUTION
if (! deck.deck.get("5").isEmpty()) {
deck.deck.get("King").remove("Hearts");
}
//ITERATOR
for (Iterator<Map.Entry<String, LinkedList<String>>> it = deck.deck.entrySet().iterator(); it.hasNext(); ) {
Map.Entry<String, LinkedList<String>> entry = it.next();
LinkedList<String> list = entry.getValue();
System.out.println(list);
if (entry.getKey().equals("5")) {
for (int i = 0; i < list.size(); i++) {
if (list.get(i).equals("Hearts")) {
list.remove(i);
break;
}
}
if (list.isEmpty())
it.remove();
}
}
System.out.println(deck.deck);
}
}
输出entrySet:
{2=[Clubs, Diamonds, Hearts, Spades], 3=[Clubs, Diamonds, Hearts, Spades], 4=[Clubs, Diamonds, Hearts, Spades], 5=[Clubs, Diamonds, Hearts, Spades], 6=[Clubs, Diamonds, Hearts, Spades], 7=[Clubs, Diamonds, Hearts, Spades], 8=[Clubs, Diamonds, Hearts, Spades], 9=[Clubs, Diamonds, Hearts, Spades], 10=[Clubs, Diamonds, Hearts, Spades], Jack=[Clubs, Diamonds, Hearts, Spades], Queen=[Clubs, Diamonds, Hearts, Spades], King=[Clubs, Diamonds, Hearts, Spades], Ace=[Clubs, Diamonds, Hearts, Spades]}
[Clubs, Diamonds, Hearts, Spades]
2
[Clubs, Diamonds, Hearts, Spades]
3
[Clubs, Diamonds, Hearts, Spades]
4
[Clubs, Diamonds, Hearts, Spades]
5
{2=[Clubs, Diamonds, Spades], 3=[Clubs, Diamonds, Spades], 4=[Clubs, Diamonds, Spades], 5=[Clubs, Diamonds, Spades], 6=[Clubs, Diamonds, Spades], 7=[Clubs, Diamonds, Spades], 8=[Clubs, Diamonds, Spades], 9=[Clubs, Diamonds, Spades], 10=[Clubs, Diamonds, Spades], Jack=[Clubs, Diamonds, Spades], Queen=[Clubs, Diamonds, Spades], King=[Clubs, Diamonds, Spades], Ace=[Clubs, Diamonds, Spades]}
输出逻辑解决方案:
{2=[Clubs, Diamonds, Hearts, Spades], 3=[Clubs, Diamonds, Hearts, Spades], 4=[Clubs, Diamonds, Hearts, Spades], 5=[Clubs, Diamonds, Hearts, Spades], 6=[Clubs, Diamonds, Hearts, Spades], 7=[Clubs, Diamonds, Hearts, Spades], 8=[Clubs, Diamonds, Hearts, Spades], 9=[Clubs, Diamonds, Hearts, Spades], 10=[Clubs, Diamonds, Hearts, Spades], Jack=[Clubs, Diamonds, Hearts, Spades], Queen=[Clubs, Diamonds, Hearts, Spades], King=[Clubs, Diamonds, Hearts, Spades], Ace=[Clubs, Diamonds, Hearts, Spades]}
{2=[Clubs, Diamonds, Spades], 3=[Clubs, Diamonds, Spades], 4=[Clubs, Diamonds, Spades], 5=[Clubs, Diamonds, Spades], 6=[Clubs, Diamonds, Spades], 7=[Clubs, Diamonds, Spades], 8=[Clubs, Diamonds, Spades], 9=[Clubs, Diamonds, Spades], 10=[Clubs, Diamonds, Spades], Jack=[Clubs, Diamonds, Spades], Queen=[Clubs, Diamonds, Spades], King=[Clubs, Diamonds, Spades], Ace=[Clubs, Diamonds, Spades]}
输出迭代器:
{2=[Clubs, Diamonds, Hearts, Spades], 3=[Clubs, Diamonds, Hearts, Spades], 4=[Clubs, Diamonds, Hearts, Spades], 5=[Clubs, Diamonds, Hearts, Spades], 6=[Clubs, Diamonds, Hearts, Spades], 7=[Clubs, Diamonds, Hearts, Spades], 8=[Clubs, Diamonds, Hearts, Spades], 9=[Clubs, Diamonds, Hearts, Spades], 10=[Clubs, Diamonds, Hearts, Spades], Jack=[Clubs, Diamonds, Hearts, Spades], Queen=[Clubs, Diamonds, Hearts, Spades], King=[Clubs, Diamonds, Hearts, Spades], Ace=[Clubs, Diamonds, Hearts, Spades]}
[Clubs, Diamonds, Hearts, Spades]
[Clubs, Diamonds, Hearts, Spades]
[Clubs, Diamonds, Hearts, Spades]
[Clubs, Diamonds, Hearts, Spades]
[Clubs, Diamonds, Spades]
[Clubs, Diamonds, Spades]
[Clubs, Diamonds, Spades]
[Clubs, Diamonds, Spades]
[Clubs, Diamonds, Spades]
[Clubs, Diamonds, Spades]
[Clubs, Diamonds, Spades]
[Clubs, Diamonds, Spades]
[Clubs, Diamonds, Spades]
{2=[Clubs, Diamonds, Spades], 3=[Clubs, Diamonds, Spades], 4=[Clubs, Diamonds, Spades], 5=[Clubs, Diamonds, Spades], 6=[Clubs, Diamonds, Spades], 7=[Clubs, Diamonds, Spades], 8=[Clubs, Diamonds, Spades], 9=[Clubs, Diamonds, Spades], 10=[Clubs, Diamonds, Spades], Jack=[Clubs, Diamonds, Spades], Queen=[Clubs, Diamonds, Spades], King=[Clubs, Diamonds, Spades], Ace=[Clubs, Diamonds, Spades]}
我希望地图包含以下内容:
{2=[Clubs, Diamonds, Hearts, Spades], 3=[Clubs, Diamonds, Hearts, Spades], 4=[Clubs, Diamonds, Hearts, Spades], 5=[Clubs, Diamonds, Spades], 6=[Clubs, Diamonds, Hearts, Spades], 7=[Clubs, Diamonds, Hearts, Spades], 8=[Clubs, Diamonds, Hearts, Spades], 9=[Clubs, Diamonds, Hearts, Spades], 10=[Clubs, Diamonds, Hearts, Spades], Jack=[Clubs, Diamonds, Hearts, Spades], Queen=[Clubs, Diamonds, Hearts, Spades], King=[Clubs, Diamonds, Hearts, Spades], Ace=[Clubs, Diamonds, Hearts, Spades]}
那么,我将如何指定去除诉讼的钥匙?我不希望删除“心脏”的所有实例(如本例所示)。帮助和线索非常感谢!
编辑:
填充西装使关键点仅指向地图内存中的一个列表。
之前:
public Deck() {
for (int i = 2; i <= 10; i++) {
deck.put(String.valueOf(i), suits));
}
for (int i = 0; i <= 3; i++) {
deck.put(highRank.get(i), suits));
}
}
之后:
public Deck() {
for (int i = 2; i <= 10; i++) {
deck.put(String.valueOf(i), new LinkedList<String>(Arrays.asList("Clubs", "Diamonds", "Hearts", "Spades")));
}
for (int i = 0; i <= 3; i++) {
deck.put(highRank.get(i), new LinkedList<String>(Arrays.asList("Clubs", "Diamonds", "Hearts", "Spades")));
}
}
答案 0 :(得分:1)
问题出在地图上,而不是删除时:
如果您具有以下条件:
Map<String, List> myMap = ...
List<String> myList = ...
myMap.put("1", myList);
myMap.put("2", myList);
然后,当您将其删除时,您将同时从两个地图条目中删除:
myMap.get("1").remove("Hearts"); // removes both from myMap.get("1") and MyMap.get("2")