Ad

How Can I Identify If 2 HashMaps Contain Objects With The Same IDs

- 1 answer

At the moment Im making a Card Game, In which a Player has a List of Cards In their Inventory. My Objective now Is to create a Deck with those Cards, where the Cards that are inside the Deck doesnt show In the Inventory side, for that I've tryed the following code:

for (HashMap<String, String> cartaInventario : listaCartasInventario) {
                for (HashMap<String, String> cartaDeckInventario : listaCartasDeckInventario) {
                    if(cartaInventario.get(Config.TAG_ID_CARTA_INVENTARIO) != cartaDeckInventario.get(Config.TAG_ID_CARTA_DECKINVENTARIO))
                    {
                        cartasInventario.add(cartaInventario.get(Config.TAG_ID_CARTA_INVENTARIO));
                    }
                }
            }

This code still doesnt work, my question Is, what's the most efficient way of doing this verification, should I use lambda expressions instead of the for cycles?

Ad

Answer

for (HashMap<String, String> cartaInventario : listaCartasInventario) {
            cartasInventario.add(cartaInventario.get(Config.TAG_ID_CARTA_INVENTARIO));
            for (HashMap<String, String> cartaDeckInventario : listaCartasDeckInventario) {
                if(cartaInventario.get(Config.TAG_ID_CARTA_INVENTARIO).equals(cartaDeckInventario.get(Config.TAG_ID_CARTA_DECKINVENTARIO)))
                {
                    cartasInventario.remove(cartaInventario.get(Config.TAG_ID_CARTA_INVENTARIO));
                    break;
                }
            }
        }
Ad
source: stackoverflow.com
Ad