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

  • Home
  • SEARCH
  • 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 7877413
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T03:21:49+00:00 2026-06-03T03:21:49+00:00

I want to create cache in java to store user sessions. It’s something like

  • 0

I want to create cache in java to store user sessions. It’s something like a cache which will store for example 5 elements for every user. I need some kind of java data structure which must be able to remember this data. So far I created this Java code:

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class SessionCache {

    public SessionCache() {
    }

    /* Create object to store sessions */
    private List<ActiveSessionsObj> dataList = new ArrayList<>();

    public static class ActiveSessionsObj {
        private int one;
        private int two;
        private int three;
        private int four;
        private int five;

        private ActiveSessionsObj(int one, int two, int three, int four, int five) {
            throw new UnsupportedOperationException("Not yet implemented");
        }
    }

    public List<ActiveSessionsObj> addCache(int one, int two, int three, int four, int five){

        dataList.add(new ActiveSessionsObj(
                        one,
                        two,
                        three,
                        four,
                        five));
          return dataList;    
    }   

}

I’m new to java and I need a help how I can add data to the structure and how I can remove data from the structure. I need to do this using a key. Is this possible? Or is there more appropriate data structure to store the data according to mu needs?

Best wishes

  • 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-06-03T03:21:50+00:00Added an answer on June 3, 2026 at 3:21 am

    Presumably each user has a unique id, so a Map implementation seems like a sensible choice where the key is the user id and the value is ActiveSessionsObj:

    Map<String, ActiveSessionsObj> cache =
        new HashMap<String, ActiveSessionsObj>();
    

    See Javadoc for adding (put()) and removing (remove()) elements from a Map:

    public void addCache(String user_id,int one,int two,int three,int four,int five)
    {
        // You may want to check if an entry already exists for a user,
        // depends on logic in your application. Otherwise, this will
        // replace any previous entry for 'user_id'.
        cache.put(user_id, new ActiveSessionsObj(one, two, three, four, five));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create a number on the page that says something like 1,299,282
I want to create .nomedia file in the internal cache directory where I will
I want to create a class which stores DataTables, this will prevent my application
I want create a excel with Apache POI in java and I must insert
I want create module which update list of usb devices automatically (not only mass
I want to do something with ehcache in Java that I think should be
I want create a cache so that when an item doesn't exist, only one
I want to create a temporary cache lookup to speed up a file lookup
I have a cache I want to periodically check and prune. In Java, I'd
I want to create a map-based web app for my town. It's small, like

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.