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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T21:46:57+00:00 2026-06-11T21:46:57+00:00

I am trying to understand how Generics work and I have this piece of

  • 0

I am trying to understand how Generics work and I have this piece of code to display the entries of newDetails a class with these kind of parameters Map. This is my code.

public class Map<Key,Value> {

    private Map<Key, Value> entry;

    public Map(){
        entry = new HashMap<Key,Value>();
    }

    public void putEntry(Key k, Value id){
        entry.put(k, id);
    }

    public void printMe(){
        System.out.println();
        for (Map.Entry<Key, Value> x: entry.entrySet()){
            System.out.print(x+" ");
        }
    }
}

Chocolates Class

    public class Chocolates<T> {

    private List<T> listOfChocolate = new ArrayList<T>(); 

    public Chocolates(T choc){
        listOfChocolate.add(choc);
    }

    public void addChocolate(T getChoc){
        listOfChocolate.add(getChoc);
    }

    public List<T> getLists(){
        return listOfChocolate;
    }
}

Main method

public static void main(String[] arg){

        Map<Chocolates<String>, Integer> newDetails = new Map<Chocolates<String>, Integer>();
        Chocolates<String> choco = new Chocolates<String>();

        newDetails.putEntry((new Chocolates<String>("Cadbury")), 1);
        newDetails.putEntry((new Chocolates<String>("Hersheys")), 2);
        newDetails.printMe();
    }

Whenever I try to ran the program it just display this one

generics.Chocolates@9931f5=1 generics.Chocolates@19ee1ac=2 

How do I display the actual entries like Cadbury 1 then Hersheys 2?

Also is there any way I can improve the main method or some parts of the program because I always create a new object whenever I add a new entry. Originally what I wanted to do is create a Generics version of this code:

Map<List<String>, Integer> newDetails = new HashMap<List<String>, Integer>();

As someone told me that its alot better to implement that line of code in a Generics Class. Any suggestions? Thanks

EDIT 2

I edited Maps Class printMe Method as Nambari have requested

public void printMe(){
        System.out.println();
        for (Map.Entry<Key, Value> x: entry.entrySet()){
            System.out.print(x.getKey()+" "+x.getValue());
        }
    }

The output is:

generics.Chocolates@9931f5 1generics.Chocolates@19ee1ac 2

But if I add this line as suggested by Joao

public String toString(){
        return listOfChocolate.toString();
    }

output is :

[Hersheys] 2[Cadbury] 1

Thanks everyone for your help!

  • 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-11T21:46:58+00:00Added an answer on June 11, 2026 at 9:46 pm

    Override Object#toString in the Chocolate class:

    public class Chocolates<T> {
    
      // ...
    
      @Override
      public String toString() {
        return listOfChocolate.toString();
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im trying to understand how class generics work and this bit just doesnt make
Trying to understand Ruby a bit better, I ran into this code surfing the
Trying to understand why this doesn't work. I keep getting the following errors: left
I am trying to understand why this program doesn't work Expected output: numbers 0-19
I'm trying to understand why this cast doesn't work: using System; using System.Collections.Generic; using
I'm a C# guy learning Java and I'm trying to understand how generics work
I was looking at the Java Generics documentation and found this piece of code,
I am just trying to understand the extends keyword in Java Generics. List<? extends
I'm trying to understand the constraints on generic type parameters in C#. What is
I am trying understand ViewModels deeper and I have read many articles and blogs

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.