Я столкнулся с хедшкратом, где я знаю, что не так, но не могу написать это в коде.В настоящее время я работаю над карточной колодой, в которой я хотел бы убрать конкретную масть из карточного ранга в 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]}
Итак, как я могу указать ключ, из которого снимать костюм?Я не хочу удалять все экземпляры "Hearts" (как в этом примере).Помощь и подсказки очень ценятся!
РЕДАКТИРОВАТЬ:
Заполнение ключей, изготовленных из мастей, указывает только на один список в памяти на карте.
До:
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")));
}
}