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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T16:44:45+00:00 2026-06-09T16:44:45+00:00

I write a simple class (the merit of StringPools themselves is not my question;

  • 0

I write a simple class (the merit of StringPools themselves is not my question; I have specific reasons for wanting this class):

import java.lang.ref.WeakReference;
import java.util.WeakHashMap;

public final class StringPool {

    private WeakHashMap m_pool = new WeakHashMap();

    public String intern(String s) {
        WeakReference reference = (WeakReference)m_pool.get(s);
        if(reference != null) {
            String interned = (String)reference.get();
            if(interned != null)
                return interned;
        }
        m_pool.put(s,new WeakReference(s));
        return s;
    }
};

When I compile it, the compiler notes it uses unchecked or unsafe operations:

warning: [unchecked] unchecked call to WeakReference(T) as a member of the raw type WeakReference       
    m_pool.put(s,new WeakReference(s));
    ^   where T is a type-variable:
    T extends Object declared in class WeakReference 

warning: [unchecked] unchecked call to put(K,V) as a member of the raw type WeakHashMap 
    m_pool.put(s,new WeakReference(s));
    ^   where K,V are type-variables:
    K extends Object declared in class WeakHashMap
    V extends Object declared in class WeakHashMap

2 warnings

How should I fix these warnings so the code compiles clean and is correct?

  • 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-09T16:44:46+00:00Added an answer on June 9, 2026 at 4:44 pm

    Add generics:

    WeakHashMap<String, WeakReference<String>>
    

    remove casting from:

    (WeakReference)m_pool.get(s);
    

    and:

    (String)reference.get()
    

    to look like:

    m_pool.get(s);
    

    and

    reference.get()
    

    respective.

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

Sidebar

Related Questions

Say we have this hyper simple class hierchy: public class SomeMath { public int
I have to write a simple log class, that would write the output to
I have write an simple python application on GAE. class Upload(webapp2.RequestHandler): def post(self): self.response.out.write('HelloWorld')
I'm using Quartz to write a simple server monitor in Java: public class ServerMonitorJob
I want to write a simple class (PHP5) that can 'run' an unknown amount
I've been trying to write a simple Static-class State Machine for my application to
I'm trying to write a simple unit containing a class TMainWindow to improve my
How to write a simple fair lock analog to new ReentrantLock(true) ? public class
I want to write a simple class to process Strings (may be very long
I have a simple class representing an object. It has 5 properties (a date,

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.