Results 1 to 9 of 9
Like Tree4Likes
  • 1 Post By SurfMan
  • 1 Post By SurfMan
  • 1 Post By SurfMan
  • 1 Post By SurfMan

Thread: Unable to add data to my nested collection (TreeMap/Set).

  1. #1
    javacakes is offline Member
    Join Date
    May 2013
    Posts
    11
    Rep Power
    0

    Default Unable to add data to my nested collection (TreeMap/Set).

    Hi all,

    I have been having major problems for the past 2 days with an exercise and finally gave up.
    I need to mimic an index of a book, showing specific words and what pages they occur on.

    It needs to have a zero-argument constructor that initializes an empty collection to hold this data.
    It must implement atleast 2 collection classes.

    Here is what I did:

    Java Code:
    // instance variables 
    public Map<String, Set<Integer>> bookMap;
    
    //Constructor
    public BookIndex()
    {
        this.bookMap = new TreeMap<String, Set<Integer>>();
        Set<Integer> pageNumbers = new TreeSet<>();
        bookMap.put("", pageNumbers);
    }
    I setup an instance variable first for the Map, which contains a String as the Keys (words), and a Set of Integers for the values (pages).
    In the constructor I then initialized a new map to "bookMap", and the set of integers to "pageNumbers".
    I then added an empty string and the set to the bookMap in the last line.


    The problem is when I make my new method, that should add data (such as a word, and the pages it is on) to the Map.
    I just can't get it to work, I have tried about 10 different versions and every time it fails, either at compile time or run time.

    Java Code:
    public void addData(String aWord, Integer aPage)
      {
           
           pageNumbers.add(aPage);
           bookMap.put(aWord,pageNumbers);
            
    }
    Can anyone tell me what I am doing wrong as I am going crazy trying to figure this out.

    Thanks

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,087
    Rep Power
    4

    Default Re: Unable to add data to my nested collection (TreeMap/Set).

    In your second example, pageNumber does not exist as a field. You declared it in the constructor, so the scope is limited to the constructor. The trick here is that you don't need to declare the pageNumbers as a field. Also, the bookMap.put("", pagNumbers) doesn't make sense.

    What you should do in addData(...) is check if there is a Set available for aWord, if it is, add the aPage to it. If not, create a new Set, add it to the bookMap and then add the aPage. Bit similar to this:
    Java Code:
    public void addData( String aWord, Integer aPage) {
       Set pageNumbers = bookMap.get(aWord);
       if ( pageNumbers == null ) { //if the get on a Map returns null, the key (or value) does not exist
          pageNumbers = new TreeSet();
          bookMap.put(aWord, pageNumbers);
       }
       pageNumbers.add(aPage);
    javacakes likes this.

  3. #3
    javacakes is offline Member
    Join Date
    May 2013
    Posts
    11
    Rep Power
    0

    Default Re: Unable to add data to my nested collection (TreeMap/Set).

    Hi SurfMan,

    I used this and it worked great but I have a few questions if you don't mind.

    why did the bookMap.put("", pagNumbers) not make any sense? That is some of the examples I seen in my text book but it didn't seem to work this time.


    So basically your method checks to see if a set is available for aWord, if there is it means that the word is already in the Map right?
    If not, it will create a new set for the new word, then add it to the map?

    I am also getting an error with the If statement, about unchecked errors. Any idea?

  4. #4
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,087
    Rep Power
    4

    Default Re: Unable to add data to my nested collection (TreeMap/Set).

    Quote Originally Posted by javacakes View Post
    Hi SurfMan,

    I used this and it worked great but I have a few questions if you don't mind.

    why did the bookMap.put("", pagNumbers) not make any sense? That is some of the examples I seen in my text book but it didn't seem to work this time.
    Because first, there is no word "". Second, it looks like you're trying to initialize the bookMap and pageNumbers Set in some way which is not necessary. If you visualize your bookMap it should look like this:
    Java Code:
    Key -> Value
    ==========
    "A" ->  {1, 4, 6, 9}
    "The -> {1,2,3,4,5,7,8,9}
    "Car" -> {4,10}
    "House" -> {3}
    Quote Originally Posted by javacakes View Post
    So basically your method checks to see if a set is available for aWord, if there is it means that the word is already in the Map right?
    If not, it will create a new set for the new word, then add it to the map?
    You are correct. A word only appears once in the map. For every single word, there is one Set with pagenumbers. So, if that word does not exist yet in the bookMap, create a new Set and put the word and Set in the bookMap. After that, you have to reuse the Set because it already exists.

    Quote Originally Posted by javacakes View Post
    I am also getting an error with the If statement, about unchecked errors. Any idea?
    That is not really an error, but a warning. It's about generics, because I haven't "generified" the method like Set<Integer>. You could ignore those, or you could add <Integer> to the Set. Adding @SuppressWarnings("unchcked") before the method will ignore it as well.
    javacakes likes this.

  5. #5
    javacakes is offline Member
    Join Date
    May 2013
    Posts
    11
    Rep Power
    0

    Default Re: Unable to add data to my nested collection (TreeMap/Set).

    Quote Originally Posted by SurfMan View Post
    In your second example, pageNumber does not exist as a field. You declared it in the constructor, so the scope is limited to the constructor. The trick here is that you don't need to declare the pageNumbers as a field. Also, the bookMap.put("", pagNumbers) doesn't make sense.

    What you should do in addData(...) is check if there is a Set available for aWord, if it is, add the aPage to it. If not, create a new Set, add it to the bookMap and then add the aPage. Bit similar to this:
    Java Code:
    public void addData( String aWord, Integer aPage) {
       Set pageNumbers = bookMap.get(aWord);
       if ( pageNumbers == null ) { //if the get on a Map returns null, the key (or value) does not exist
          pageNumbers = new TreeSet();
          bookMap.put(aWord, pageNumbers);
       }
       pageNumbers.add(aPage);
    Hi,

    I have this working but I am just looking over the code and I am getting very confused by the last line.

    Lets say that (pageNumbers == null) is false, and the word already exists.
    It will skip the IF statement and goto the last line pageNumbers.add(aPage);

    How does the method know to add this page value to the correct corresponding key in the map?

    The last line will add the page value to a set called pageNumbers.
    How is this set then added to the correct key-value pair in the map?

    Shouldn't we have bookMap.put(aWord, pageNumbers); under it again?

  6. #6
    javacakes is offline Member
    Join Date
    May 2013
    Posts
    11
    Rep Power
    0

    Default Re: Unable to add data to my nested collection (TreeMap/Set).

    Ok I thought it through, can someone tell me If I am right:

    Java Code:
    //public Map<String, Set<Integer>> bookMap;
    
    public void addData( String aWord, Integer aPage) 
       {
       Set pageNumbers = bookMap.get(aWord); // pageNumbers will now reference the set of values associated with key aWord in the map.
       if ( pageNumbers == null ) {                   // if pageNumbers set is empty 
          pageNumbers = new TreeSet();            // make pageNumbers reference a new set
          bookMap.put(aWord, pageNumbers);     // add aWord to map as the key, add pageNumbers as value <set> which is currently empty
       }
       pageNumbers.add(aPage);                      // any thing we add to this set will now be reflected in the map automatically, as it references the value set in the map.
    Is that right?

    I thought pageNumbers just copied the contents of the map values for keyset aWord.
    I didn't think we could change the values of the map without having to invoke bookMap.put(aWord, pageNumbers) everytime we added something to pageNumbers set?
    Last edited by javacakes; 05-08-2013 at 10:56 PM.

  7. #7
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,087
    Rep Power
    4

    Default Re: Unable to add data to my nested collection (TreeMap/Set).

    Yup, you nailed it. Think of a map as a drawer where you put stuff in. On the front of the drawer is a label (the key) and inside the drawer is some content (the value). When you say Map.get(key) you get the value, but the value stays in the drawer. If that drawer (read: key) does not exist, you get null in return. That's the Map way of telling you: this key does not exist. So now you know that it doesn't exist, you put a new value (the newly created Set) in a drawer. Every time you need a Set, you get a reference to it, so you work directly on the Set while it lies in the drawer. There is no need to put it back in, since you never removed it.

    Finally, a nitpick: an object being empty is different than it being null. So if you comment something, don't make it confusing for yourself:
    Java Code:
       Set pageNumbers = bookMap.get(aWord); // pageNumbers will now reference the set of values associated with key aWord in the map. 
       //It will be null if it there is no Set associated with this word
    
       if ( pageNumbers == null ) {                   // if pageNumbers set does not exist (is null)
    Edit: added some clarifying comments.
    Edit 2: extra explanation.
    Last edited by SurfMan; 05-08-2013 at 11:06 PM.
    javacakes likes this.

  8. #8
    javacakes is offline Member
    Join Date
    May 2013
    Posts
    11
    Rep Power
    0

    Default Re: Unable to add data to my nested collection (TreeMap/Set).

    Really nice way to put it, awesome!

    so you work directly on the Set while it lies in the drawer.
    That's what I needed to hear!
    I wish I was told this before as it would have made everything much more clear.

    Thank you for the help man I really appreciate it, saved me hours.

  9. #9
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,087
    Rep Power
    4

    Default Re: Unable to add data to my nested collection (TreeMap/Set).

    Glad it was helpful. Keep up the good work. It looks like you got what it takes to be a programmer. Trust me, read the posts in the "new" section where they ask for "teh Java codez cos they r in doubt" (read: do my homework for me). They will never make it... They need to practice one thing: "Do you like fries with that?" :D
    javacakes likes this.

Similar Threads

  1. Unable to fetch data from other class
    By buggedup in forum New To Java
    Replies: 9
    Last Post: 02-06-2013, 10:45 AM
  2. Replies: 0
    Last Post: 11-29-2011, 11:56 PM
  3. Replies: 8
    Last Post: 11-22-2011, 11:45 AM
  4. Unable to read data from inputstream
    By renuka_anil in forum Java Servlet
    Replies: 0
    Last Post: 01-29-2009, 04:20 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •