#var = shopsByItem.computeIfAbsent, Assigns shopsByItem(?)

1 messages · Page 1 of 1 (latest)

shadow plume
#

I'm lost.

olive craterBOT
#

<@&987246399047479336> please have a look, thanks.

olive craterBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

shadow plume
#
    public void addShop(PlayerShopContainer shop) {
        PlayerShopContainer selectedShop = shopsByName.get(shop.getPlayer().getUsername());
        if (selectedShop == null) {
            shopsByName.put(shop.getPlayer().getUsername(), shop);
        }

        for (Item item : shop.getItems()) {
            String name = item.getDefinition().getName();
            List<PlayerShopContainer> shops = shopsByItem.computeIfAbsent(name, k -> new ArrayList<>());
            if (!shops.contains(shop)) {
                shops.add(shop);
            }
        }
    }
olive craterBOT
# shadow plume ``` public void addShop(PlayerShopContainer shop) { PlayerShopContai...

Detected code, here are some useful tools:

Formatted code
public void addShop(PlayerShopContainer shop) {
  PlayerShopContainer selectedShop = shopsByName.get(shop.getPlayer().getUsername());
  if (selectedShop == null ) {
    shopsByName.put(shop.getPlayer().getUsername(), shop);
  }
  for (Item item : shop.getItems()) {
    String name = item.getDefinition().getName();
    List<PlayerShopContainer> shops = shopsByItem.computeIfAbsent(name, k -> new ArrayList<>());
    if (!shops.contains(shop)) {
      shops.add(shop);
    }
  }
}
shadow plume
#

in this function

#

method*

#

i'm assigning a local variable named shops

#

however, it is really assigning to a instance member:
private static final TreeMap<String, List<PlayerShopContainer>> shopsByItem = new TreeMap<>();

#

why, and how

#

specifically this part:

    String name = item.getDefinition().getName();
    List<PlayerShopContainer> shops = shopsByItem.computeIfAbsent(name, k -> new ArrayList<>());
    if (!shops.contains(shop)) {
      shops.add(shop);
    }
  }```
How is it even possible that its doing shops.add(shop)? is this because shops in this scenario is a pointer to the List assigned to the key?
olive craterBOT
winged storm
shadow plume
#

aha! appreciate your reply

#

thanks alot

winged storm
shadow plume
#

ow excuse me my connection was lost

#

i asked

#

if you would possible be able to elaborate on what you meant by value-based classes?

#

and how those differ from objects?

winged storm
#

Sorry, i meant value objects. I only know a littebit about them myself and there are currently only a preview feature, so you probably don't have to know this.
If you are generally interested in learning about them, here is the jep: https://openjdk.org/jeps/8277163

shadow plume
#

thanks alot! appreciated