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 3961774
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T03:01:00+00:00 2026-05-20T03:01:00+00:00

So yeah, I’m just trying to see if there is a more elegant way

  • 0

So yeah, I’m just trying to see if there is a more elegant way of doing what I’m about to do below (please keep in mind that I want POJO (Plain-Old Java Objects) related answers, since this issue is J2ME related, so no Generics and modern data structures only found in Java 1.5 and above):

Suppose I have an object, MyImage, which just a simple bean object that gets populated with data from a network call to my server. All it contains is metadata about said image related to my app, more importantly, it contains a unique identifier that is used to construct a URL in order to fetch the image from my server for that object. I receive a new set of those objects every so often when I make a request for them, some of which are the same as previous requests.

Now, even though I am able to download the image, the problem arises in how to cache the image data in a way that when I receive a new set of MyImage objects, I cross-reference them against my cache and only retain the image for that MyImage object if it was already downloaded. In other words, when I save my downloaded image to my Hashtable cache, I key the image data with the constructed URL (MY_IMAGE_SERVER+myImageUniqueId). When I get a new set of MyImage objects, currently I do the following:

 Hashtable imgs = getImages();

 //If we have cached images, we should see which ones to carry over.
 if(imgs.size() > 0){       
    Hashtable newImgs = new Hashtable();
    for(int i = 0; i < myImages.length; i++){
        MyImage mi = myImages[i];
        if(mi != null && mi.hasImage()){
            //Check if we have the MD5 URL
            if(imgs.containsKey(IMG_URL_PATH + mi.getUniqueId())){
                //Place in new hashtable
                newImgs.put(IMG_URL_PATH + mi.getUniqueId(), imgs.get(IMG_URL_PATH + mi.getUniqueId()));
            }
        }
    }
   _bannerImgs = newImgs;
 }

I am wondering if this sounds legit, or can it be done in a better more efficient way?

  • 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-20T03:01:01+00:00Added an answer on May 20, 2026 at 3:01 am

    FOLLOWUP

    Based on the assumed purpose of the code in the comment (below), the way you are doing it seems reasonable, but there are a couple of minor optimization you could make. Change the relevant part of the code to this:

        // Check if we have the image in our cache
        String key = IMG_URL_PATH + mi.getUniqueId();
        Object image = imgs.get(key);
        if (image != null) {
            // Carry over to new cache
            newImgs.put(key, image);
        }
    

    Note:

    1. Creating / using a local variable avoids creating the key string 3 times.
    2. Using get instead of contains eliminates one hashtable lookup.

    However, it is doubtful that this will make a significant difference to your system’s performance … unless the getUniqueId() method does something daft, like calculate an MD5 sum each time you call it. (And apparently it doesn’t.)

    Notwithstanding the performance, I’d make this change because it makes the code easier to read … IMO.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I don't know anything about jquery so yeah. I just want so that when
Yeah, I was wondering if there's a way to make a link the size
Yeah... just thinking about it... Should I store credit card numbers that have been
So yeah, like I said, I've spent a few hours trying to fix this
This shows what I have and want: So yeah as you can see I
Yeah, there's this cool myLabel.adjustsFontSizeToFitWidth = YES; property. But as soon as the label
Hello yeah I'm asking this question a second time, sorry about that but I
What is the most elegant way to return a string from a List ok,
Yeah some people on the web have the same problem.. but there was no
Yeah I know there are couple of questions which are related to syncing with

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.