#var = shopsByItem.computeIfAbsent, Assigns shopsByItem(?)
1 messages · Page 1 of 1 (latest)
<@&987246399047479336> please have a look, thanks.
While you are waiting for getting help, here are some tips to improve your experience:
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.
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);
}
}
}
Detected code, here are some useful tools:
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);
}
}
}
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?
Detected code, here are some useful tools:
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);
}
}
Yes, the shops variable is a pointer to a list that is inside the shopsByItem map.
In java all* non-primitive variables are actually pointers to objects.
* Note: Does not apply for value-based classes when they are added to java.
If your question has been answered, use /help-thread close to close this thread.
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?
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
thanks alot! appreciated