Solved How to point multiple variables to one variable.

Discussion in 'Spigot Plugin Development' started by Xuho, Jan 22, 2020.

  1. I have a map with this structure.
    Code (Text):
    public Map<String, Map<String, List<CustomObject>>> map = new HashMap<>();
    And the second Map contains a list of a custom object asociated to a string.
    My problem is that I want to make the objects of that list the same so if two list contains the same object, if I modify that object in one list, it will be modified in the other list.
     
  2. Could you explain this better?

    Why are you mapping a custom object to a string which is mapped to another string. Couldn't you just map the original string to the List of custom objects, or use a multimap to achieve the same outcome?
     
  3. The first string reffers to the name of the worlds of the server, the second string is just like an identifier of some information.
    (I nested the maps becouse a server could have "infinite" worlds)
     
  4. Use custom object classes, not some weird hashmap inside hashmap with a list of string inside of it
     
    • Funny Funny x 1
  5. For optimization reasons, this is the best way of storing my CustomObject, I'm just asking how I can point multiple variables to one variable.
     
  6. And what exactly is not working? This:
    Code (Java):
    CustomObject customObject = new CustomObject("Hello");
    final List<CustomObject> list = new ArrayList<>();
    final List<CustomObject> otherList = new ArrayList<>();

    list.add(customObject);
    otherList.add(customObject);

    System.out.println(list);
    System.out.println(otherList);
    Prints out "[Hello]", "[Hello]"
    If you want to assign a new object, You have to make a Wrapper-class (a class that basically contains only your object as field).
    That being said, like @Maxx_Qc said, don't use that ugly Map<Map<>> stuff and use a custom class
     
    • Like Like x 1
  7. As I said, I must use that Map structure to store my objects but I dont want to create multiple copies of the same object and update all of them each time I want to change something so I was wondering if there is any way of pointing multiple variables to an unique one.
     
  8. If you pass the same instanace of the object to two (or more) different entries of the map and change a field (or do whatever) to an object from one entry of the map, the objects in all entries will all take effect (since it's the same instance)

    Please learn OOP
     
    • Like Like x 1
    • Agree Agree x 1
  9. This
    Code (Java):
    list.add(customObject);
    otherList.add(customObject);
    does not create any copies. It simply adds apointer to the same object to the list. The object stays the same
     
    • Like Like x 1
  10. So if I modify that object from one list and then I try to retrieve that object from another list the object will have the new changes?
     
  11. Yes.
     
  12. The problem was that I added the object to the lists like .add(new CustomObject(...)) I should have realized:rolleyes:
     
  13. Code (Java):
    CustomObject customObject = new CustomObject("Hello");
    final List<CustomObject> list = new ArrayList<>();
    final List<CustomObject> otherList = new ArrayList<>();

    list.add(customObject);
    otherList.add(customObject);

    System.out.println(list);
    System.out.println(otherList);
           
    customObject.setString("World");

    System.out.println(list);
    System.out.println(otherList);
    prints out "[Hello]", "[Hello]" and "[World]", "[World]".
    Hope, this example helps
     
  14. That's the point of custom object class. Just like the cass Player for example, if you change their health it's gonna change from everywhere
     
  15. But as I didnt realize that I created a new object each time I add them to a list, I thought it didnt work like that.