Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7159221
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:11:42+00:00 2026-05-28T13:11:42+00:00

Given data such as : user, library ID, book ID, year 1,10, 100,2000 2,10,

  • 0

Given data such as :
user, library ID, book ID, year

1,10, 100,2000
2,10, 100,2000
3,10, 100,2000

2,10, 200,2000
3,10, 200,2000
1,10, 200,2000

I tried storing them using TreeMap but the second group overwrite the first , any suggestions ?

    TreeMap<Integer, Integer> bookMap = new TreeMap<Integer, Integer>();
    TreeMap<Integer, TreeMap<Integer, Integer>> libraryMap = new TreeMap<Integer, TreeMap<Integer, Integer>>();
    TreeMap<Integer, TreeMap<Integer, TreeMap<Integer, Integer>>> UserMap = new TreeMap<Integer, TreeMap<Integer, TreeMap<Integer, Integer>>>();

    bookMap.put(100, 2000);
    libraryMap.put(10,bookMap);     
    UserMap.put(1,libraryMap);
    UserMap.put(2,libraryMap);
    UserMap.put(3,libraryMap);

    bookMap = new TreeMap<Integer, Integer>(); 
    bookMap.put(200, 2000);
    libraryMap = new TreeMap<Integer, TreeMap<Integer, Integer>>();
    libraryMap.put(10,bookMap);
    UserMap.put(1,libraryMap);
    UserMap.put(2,libraryMap);
    UserMap.put(3,libraryMap);

Solution #1
still using maps only, but I think this solution will take long time and consume more memory ?

TreeMap<Integer, Integer> bookMap = new TreeMap<Integer, Integer>();
        TreeMap<Integer, TreeMap<Integer, Integer>> libraryMap = new TreeMap<Integer, TreeMap<Integer, Integer>>();
        TreeMap<Integer, TreeMap<Integer, TreeMap<Integer, Integer>>> UserMap = new TreeMap<Integer, TreeMap<Integer, TreeMap<Integer, Integer>>>();

        bookMap.put(100, 2000);
        bookMap.put(300, 2003);
        libraryMap.put(10,bookMap);     
        UserMap.put(1,libraryMap);
        UserMap.put(2,libraryMap);
        UserMap.put(3,libraryMap);

        int userID = 1;
        int libraryID =10;
        if (UserMap.containsKey(userID))
        {           
             if ( UserMap.get(userID).containsKey(libraryID))
             {
                 TreeMap<Integer, Integer> storedBookMap = UserMap.get(userID).get(libraryID);
                 bookMap= new TreeMap<Integer, Integer>();

                 bookMap.putAll(storedBookMap);
                 bookMap.put(200, 2000);

                 libraryMap = new TreeMap<Integer, TreeMap<Integer, Integer>>();
                 libraryMap.put(libraryID,bookMap); 

                 UserMap.put(userID,libraryMap);
             }
        }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T13:11:42+00:00Added an answer on May 28, 2026 at 1:11 pm

    every user can have more than one book, but when you do:

    UserMap.put(1,libraryMap);
    

    you overwrite whatever was in the UserMap under the key 1. so you should make the UserMap of type TreeMap<Integer,ArrayList<whatever>> and add data to the list.

    also, there’s no sense in nesting the maps. each entity in you program (user/book/library) has an id, so i would suggest keeping a single map for each type of entity and using ids for reference. i.e:

    TreeMap<Integer, Integer> bookMap = new TreeMap<Integer, Integer>();    
    TreeMap<Integer, ArrayList<Integer>> userMap = new TreeMap<Integer, Integer>();
    
    // for each line
      // read line and assign appropriate values to uid, libid, bid, year
    
      if (bookMap.get(bid) == null)
        bookMap.put(bid,year);
    
      if (userMap.get(uid) == null))
        userMap.put(uid, new ArrayList<Integer>());
      userMap.get(uid).add(bid);
    

    now if you want to know which books user 1 has, you do something like:

    for (Integer i: userMap.get(1))
      System.out.println(i+","+bookMap.get(i))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given such data UserID, MovieType , year 1, 2, 2000 1, 3, 2000 1,
Hey guys, given a data set in plain text such as the following: ==Events==
Given an InputStream called in which contains audio data in a compressed format (such
I am using the excellent jTemplates plugin to generate content. Given a data object
Given this data set: ID Name City Birthyear 1 Egon Spengler New York 1957
Given a data structure (e.g. a hash of hashes), what's the clean/recommended way to
Given a data set like this; +-----+---------------------+--------+ | id | date | result |
Given a data stream of continuously arriving items containing a timestamp and text (e.g.
I'm being given a data source weekly that I'm going to parse and put
I know you could make a helper pretty easily given the data. So, if

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.