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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:20:33+00:00 2026-05-28T17:20:33+00:00

I haven’t used generics before and I am wondering when I should use them

  • 0

I haven’t used generics before and I am wondering when I should use them and what the advantages are. I think it might be appropriate for a collection that I made since java always uses generics for collections as well but if I call the methods I created the type is already set in the function so it would give an error anyway. When should I use a generic class? Could you give an example because I am not sure how to use it. At the moment my code is as follows:

public class NodeList {
  private static final int MAX_AMOUNT_OF_NODES = 12;
  private HashMap<String, Node> nodeList;

  public NodeList(){
    nodeList    = new HashMap<String, Node>(MAX_AMOUNT_OF_NODES);
  }

  public Node get(String id){
    return nodeList.get(id);
  }

  public boolean add(Node node){
    if(nodeList.size() <= MAX_AMOUNT_OF_NODES){
      nodeList.put(node.id, node);
      return true;
    }
    return false;
  }
}
  • 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-28T17:20:34+00:00Added an answer on May 28, 2026 at 5:20 pm

    You can look at the existing API for guidance. For example, all the Collections are generic. That is because all collections contain elements of a type.

    From that, it makes sense that generic classes should be used when you would have to create the exact same code again and again for different types. If you have to do that, generics might offer you some benefit.

    As far as an example, the docs are a good place to start.

    From that link, the first code sample is

    public class Box<T> {
    
        // T stands for "Type"
        private T t;
    
        public void add(T t) {
            this.t = t;
        }
    
        public T get() {
            return t;
        }
    }
    

    Conceptually, there is a Box class that is going to contain something. What it contains does not matter, because the type is specific by the programmer. A Box instance can contain basically anything. When the programmer needs to create a box, he/she specifies the type.

    Box<SomeClass> myBox = new Box<SomeClass>();

    Think about it this way — if you wanted to create a general Box that could hold anything without generics, you would have to

    1) have the field f be an Object, or
    2) create a Box class for every type a box could contain.

    With generics, you only need one class, and you can specify the exact type. Maybe if you are doing something and your approach involved either 1 or 2 above, it’s better to use generics.

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

Sidebar

Related Questions

I haven't used the STL much before, but I started to on this huffman
Haven't seen this feature anywhere else. I know that the 32nd bit is used
I haven't used ARC yet other than to deal with it when it forces
I haven't used multithreading in Clojure at all so am unsure where to start.
I haven't done a lot of work with multi-level, pure CSS drop-down menus before,
I haven't found any documentation on this or seen this done before, but is
Haven't seen anything, but I have seen that you can create albums in the
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Haven't done as much of Javascript as I should have done. I am trying
haven't used regex replaces much and am not sure if how I have done

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.