Java Tree Map always gets initialised but shouldn't, why is that?

In summary: CODE]In summary, the code creates a Position object and adds skills to it if they don't exist. If a position exists, it updates the list of skills and positions.
  • #1
DottZakapa
239
17
TL;DR Summary
The TreeMap in a class, every time the class is accessed the tree map content gets deleted
here is the portion of code

[Code tags added by the Mentors]
Java:
public void addPosition(String name, String skillNames) throws ApplicationException {
    Position ps=new Position (name);   //creating object position
    if(listaPosizioni.get(name)==null) {   //checking if position already present in the list
           listaPosizioni.put(name, ps);   //adding position to the list
     } else throw new ApplicationException();
    for (String s :  skillNames) {
          if (elencoSkills.get(s)==null) {
                  throw new ApplicationException();
          }
          Skill skil= new Skill(s);    // At first cycle of the for  data is inserted properly , at second cycle the TreeMap in the corresponding classes are reset
          ps.addSkill(skil);
          skil.addPosition(ps);
    }
...

class Position{
    private String name;

    private List<Skill> listSkills=new ArrayList<>(); //This list every time is accessed during the for cycle gets initialised again

    public Position(String name) {
        this.name=name;
    }

    public void addSkill(Skill skill) { //method to add skill required for the position
        listSkills.add(skill);
    }

public class Skill {
    private String name;
    private Map<String, Position> positions = new TreeMap<>();
    //private List<Capability> capabilities = new ArrayList<>();
    Skill(String name) {this.name = name;}

    public String getName() {
        return name;
        }

    public List<Position> getPositions() {
        return new ArrayList<>(positions.values());}

    void addPosition(Position position) {
       positions.put(position.getName(), position);
        }
 
Last edited by a moderator:
Technology news on Phys.org
  • #2
You question (and code) is a bit hazy to me, but it sounds like you are puzzled that the map inside the skil variable is empty after line 10 for all loop iterations? If so, then that follows directly from the map being a member field of the Skill class of which you allocate a new instance on every iteration of the loop.

(My Java is a bit rusty, but you seem to loop over the characters of the skillNames string. Perhaps you meant that parameter type to be some kind of a collection or a whitespace-separated list?)
 
  • #3
Java:
public void addPosition(String name, String... skillNames) throws ApplicationException {

          

        if(listaPosizioni.get(name)!=null)
        throw new ApplicationException();   //checking if position already present in the list
        Position ps=new Position (name);   
        listaPosizioni.put(name, ps);   //adding position to the list

        for (String s:skillNames) {
            
            if (elencoSkills.get(s)==null) {
                throw new ApplicationException();
                }
            Skill skil= new Skill(s);
            ps.addSkill(skil);
            skil.addPosition(ps);
        }
    
    ...//following the class skill
        
    public class Skill {

    private String name;
    private Map<String, Position> positions = new TreeMap<>();
    //private List<Capability> capabilities = new ArrayList<>();
    
    Skill(String name) {this.name = name;}
    
    public String getName() {
        return name;
        }
    public List<Position> getPositions() {
        return new ArrayList<>(positions.values());}
    
    
    void addPosition(Position position) {
        positions.put(position.getName(), position);
        }
    //void addCapability(Capability c) {capabilities.add(c);}
    //List<Capability> getCapabilities() {return capabilities;}
    
    public String toString() {
        return this.name;
    }
}   
    
    ...//here is the class position
        
        public class Position {
    
    private String name;
    
    private List<Skill> listSkills=new ArrayList<>();
    
    public Position(String name) {
        this.name=name;
    }
    
    public void addSkill(Skill skill) { //method to add skill required for the position
        
        listSkills.add(skill);
        
    }

    
    public String getName() {
        return this.name;
        }
    
    public List<String> getApplicants() {
        return null;
    }
    
    public String getWinner() {
        return null;
    }
    
    public String toString() {
        return this.name;
    }
}

now i understood how to insert code in here... should look more clear

So in the for loop it should loop an array of skillNames because the argument is vararg. the thing i don't understand is why when the cycle goes in the class skill, the Map is always reinitialised, doesn't keep the prevues entry.

If i use the following code in the method addPosition all works fine :
Java:
if (listaPosizioni.containsKey(name)) throw new ApplicationException();
        Position position = new Position(name);
        listaPosizioni.put(name, position);
        for (String skillName: skillNames) {
            Skill skill = elencoSkills.get(skillName);
            if (skill == null) throw new ApplicationException();
            position.addSkill(skill);
            skill.addPosition(position);
        }

what the previous doesn't work properly and this does?
 
Last edited:
  • #4
You have a "scope" problem.
When you do this:
Skill skil; /* creates a name locally in this procedure */
When you do this:

skil = (something); /* aims the variable "skil" at some object or value */
If you want "skil" to retain its value over multiple calls to the procedure, you must declare it OUTSIDE the procedure.
Alternatively, you can start with a new version of "skil" eash time and set its value appropriately using some kind of method (I think you used get(skillName)).

NOTE: Last night when I was trying to edit this, my browser went crazy and I got interrupted; Firefox had updated itself. It seems to have its brains back now. But, I can't remember if I was going to say anything else or not. Sigh.
 
Last edited:
  • Like
Likes jim mcnamara
  • #5
harborsparrow said:
If you want "skil" to retain its value over multiple calls to the procedure, you must declare it OUTSIDE the procedure.

Or the skill and other references created in a local scope can be stored in a data structure (or in general an object graph) referenced by a variable in an outer scope. For instance, I suppose from the code snippets shown that the skill object is supposed to be linked into the position data which again is linked into the listaPosizioni map.

Edit: spelling
 
Last edited:
  • Like
Likes harborsparrow

What is a Java Tree Map and why does it always get initialised?

A Java Tree Map is a data structure that stores key-value pairs in a sorted order based on the natural ordering of the keys. It always gets initialised because it is a part of the Java Collections Framework and follows the default initialization rules for objects in Java.

Can a Java Tree Map be initialised without any elements?

Yes, a Java Tree Map can be initialised without any elements. This is because it follows the default initialization rules for objects in Java, where all instance variables are automatically assigned a default value. For a Tree Map, the default value for the root node is null.

Why is it not recommended to initialise a Java Tree Map without any elements?

Initialising a Java Tree Map without any elements can lead to unexpected behavior, as the Tree Map relies on the natural ordering of keys to properly maintain its structure. Without any elements, the Tree Map will have an empty root node, which can cause issues when attempting to add elements to the map.

What is the best way to initialise a Java Tree Map with elements?

The recommended way to initialise a Java Tree Map with elements is to use the constructor that takes in a Comparator or use the put() method to add elements to an empty Tree Map. This ensures that the elements are added in a proper sorted order and avoids any unexpected behavior.

Can a Java Tree Map be initialised with custom sorting logic?

Yes, a Java Tree Map can be initialised with custom sorting logic by providing a Comparator in the constructor. This Comparator can be used to define the sorting logic for the keys in the Tree Map, allowing for customization of the map's ordering.

Similar threads

  • Programming and Computer Science
Replies
3
Views
3K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
9
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
1
Views
3K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
2
Views
2K
Back
Top