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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T16:07:36+00:00 2026-06-02T16:07:36+00:00

On occasion, as much as I try to avoid it, I do generate a

  • 0

On occasion, as much as I try to avoid it, I do generate a fair amount of boilerplate code in my programming. I’ve been told that the Java Class object can be used to get around boilerplate code, but I don’t currently see how.

When I say boilerplate code, I’m referring to the term used to refer to text that is reused again and again, with slight modification.

public Map<String, Boolean> loadBooleanTags(File in)
{
// Code that extracts boolean tags
}

Now, suppose, you then want to load int tags, where the files are in the exact same format, but you want the data structure to be a Map<String, Integer>. The only way I can think of handling this is like so:

public Map<String, Integer> loadIntegerTags(File in)
{
// Code that extracts integer tags
}

Essentially, I copy and past the boolean method, but I make it parse an Integer instead. What are some better ways to handle this? Ideally, I’d like to have one method that outputs a map with the correct generics.

  • 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-02T16:07:37+00:00Added an answer on June 2, 2026 at 4:07 pm

    Interesting question. First of all, Class has been around since Java 1. Second, I think this is the pattern you’re looking for:

    public abstract class GenericMapFactory<T>
    {
        public Map<String,T> makeMap(File in) throws InstantiationException, IllegalAccessException, IOException
        {
            Map<String,T> result = new HashMap<String,T>();
            BufferedReader rdr = new BufferedReader(new FileReader(in));
            String line = null;
            while ((line=rdr.readLine()) != null)
            {
                String key  = "" /* something meaningful for your application */; 
                T      item = parse(line);
                result.put(key, item);
            }
            return result;
        }
    
        protected abstract T parse(String line);
    }
    

    For every variation you need you provide a specialization, for example:

    public static class IntMapFactory extends GenericMapFactory<Integer>
    {
        @Override
        protected Integer parse(String line)
        {
            Integer result = null; 
            // parse the line, setting the value of result
            return result;
        }
    }
    

    All the ‘boilerplate’ code is factored out into a generic superclass and only the type-specific code needs to be written. You use it as follows:

    File in = ...
    Map<String,Integer> msi = new IntMapFactory().makeMap(in);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

on more than one occasion I felt the need to define class methods that
I've got a Customer class that has a List <string > Roles property. Much
I have occasion to need to print code (Horrors!!! ;) ), and i was
I am using svn and on occasion I need to revert some changes that
I've been using StackOverflow since the beginning, and have on occasion been tempted to
On occasion, the following code works, which probably means good concept, but poor execution.
On my busiest production installation, on occasion I get a single thread that seems
I've noticed on occasion that that using * to search for the word under
This is a problem that I come to on occasion and have yet to
On occasion, I'll end up checking in a file to our TFS db that

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.