Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Java code question

  • 20-07-2018 03:33PM
    #1
    Closed Accounts Posts: 1,752 ✭✭✭


    Can someone perhaps explain the following to me? My confusion is mainly around the else statement, in that it initialises an empty HashSet and adds this HashSet to the Map, but calling values.add(sToAdd) after the fact seems to updated the entries map anyway?
    import java.util.Collections;
    import java.util.HashSet;
    import java.util.Map;
    import java.util.Set;
    import java.util.TreeMap;
    
    public class Entries {
    	
    	private Map<Integer, Set<String>> entries = null;
    	
    	Entries(){ 
    		this.entries = Collections.synchronizedMap(new TreeMap<Integer, Set<String>>());
    		}
    	
    	final void add(String sToAdd){
    		
    		Set<String> values;
    		if (this.entries.containsKey(1)) {
    			values = this.entries.get(1);
    		} else {
    			values = new HashSet<String>();
    			this.entries.put(1, values);
    		}
    		values.add(sToAdd);
    	}
    	
    	public static void main(String[] args) {
    		
    		Entries e = new Entries();
    
    		e.add("First entry");
    		
    		System.out.println(e.entries.toString());
    	}
    
    }
    


Comments

  • Registered Users, Registered Users 2 Posts: 1,127 ✭✭✭Rulmeq


    They have a Map that contains sets that use an integer as the key.

    To add a value to the set, they first need to check if the set already exist for that key, if it does they update that (they could do this inline instaed of declaring the set outside the block, but each to their own style). If it does not exist, then they create a new set, put the value into that, and then add that new set to the map using the key.

    Not sure why they are doing any of this though, because they only ever use one key 1, so there's no benefit to having a map at all.


  • Closed Accounts Posts: 1,752 ✭✭✭Pelvis


    I wrote the code myself but it's based on a class I was reviewing that was confusing me, so just wanted my own version to run through it. I was using 1 as a key just to see if it would overwrite an existing value with the new value. (it doesn't).
    Rulmeq wrote: »
    If it does not exist, then they create a new set, put the value into that, and then add that new set to the map using the key.

    This is the bit that confuses me, as from my reading of the code, it adds the newly created set to the entries map, before putting the value into it. The value is added after, but yet entries is still updated. It doesn't make sense to me.
    if (this.entries.containsKey(1)) {
    	values = this.entries.get(1);
    } else {
    	values = new HashSet<String>();
    	this.entries.put(1, values); [B]//values is empty???[/B]
    }
    values.add(sToAdd);[B] //string is added to the new set here, AFTER calling entries.put(). [/B]
    


  • Registered Users, Registered Users 2 Posts: 1,127 ✭✭✭Rulmeq


    Pelvis wrote: »
    This is the bit that confuses me, as from my reading of the code, it adds the newly created set to the entries map, before putting the value into it. The value is added after, but yet entries is still updated. It doesn't make sense to me.
    Ah, so you need to back up the horse a little bit here. You need to understand how java references objects.


    Maybe start here (although this isn't quite what you're asking, if you grok it, it should give you the basis of your answer)

    https://javaranch.com/campfire/StoryCups.jsp
    https://javaranch.com/campfire/StoryPassBy.jsp


  • Closed Accounts Posts: 1,752 ✭✭✭Pelvis


    I'm an idiot! Thanks :)


Advertisement