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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T00:44:37+00:00 2026-06-03T00:44:37+00:00

The following code is working import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import

  • 0

The following code is working

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.gson.Gson;

class DataObject<T> {

    private int data1 = 100;
    private String data2 = "hello";
DataObject child;
private List<String> list = new ArrayList<String>() {
  {
    add("String 1");
    add("String 2");
    add("String 3");
      }
    };
    private Map<String, DataObject> data=null;

    public DataObject(int i){
        this.data1 = i;
        this.data = new HashMap<String, DataObject>();
    }

    //getter and setter methods

    @Override
    public String toString() {
       return "DataObject [data1=" + data1 + ", data2=" + data2 + ", list="
    + list + "]";
    }
    public Map<String, DataObject> getData() {
        return data;
    }

    public void addData(final String key, DataObject value, Class<T> t) {
        data.put(key, value);
    }
}

public class test {



    /**
 * @param args
 */
public static void main(String[] args) {    
    DataObject obj = new DataObject(12);
    obj.child = new DataObject(25);
    obj.addData("myOtherData", new DataObject(32), DataObject.class);
        Gson gson = new Gson();
        System.out.println(gson.toJson(obj));
    }

}

output:

{"data1":12,"data2":"hello","child":{"data1":25,"data2":"hello","list":["String 1","String 2","String 3"],"data":{}},"list":["String 1","String 2","String 3"],"data":{"myOtherData":{"data1":32,"data2":"hello","list":["String 1","String 2","String 3"],"data":{}}}}

but I actually need to make the work with a generic

Map<String, Object> data

So this code:

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.gson.Gson;

class DataObject<T> {

    private int data1 = 100;
    private String data2 = "hello";
DataObject child;
private List<String> list = new ArrayList<String>() {
  {
    add("String 1");
    add("String 2");
    add("String 3");
      }
    };
    private Map<String, Object> data=null;

    public DataObject(int i){
        this.data1 = i;
        this.data = new HashMap<String, Object>();
    }

    //getter and setter methods

    @Override
    public String toString() {
       return "DataObject [data1=" + data1 + ", data2=" + data2 + ", list="
    + list + "]";
    }
    public Map<String, Object> getData() {
        return data;
    }

    public void addData(final String key, Object value, Class<T> t) {
        data.put(key, value);
    }
}

public class test {



    /**
 * @param args
 */
public static void main(String[] args) {    
    DataObject obj = new DataObject(12);
    obj.child = new DataObject(25);
    obj.addData("myOtherData", new DataObject(32), DataObject.class);
        Gson gson = new Gson();
        System.out.println(gson.toJson(obj));
    }

}

which is not working:
output:

{"data1":12,"data2":"hello","child":{"data1":25,"data2":"hello","list":["String 1","String 2","String 3"],"data":{}},"list":["String 1","String 2","String 3"],"data":{"myOtherData":{}}}

myOtherData object is missing, because Gson cannot work with general Object

That’s why I started putting the class as third argument of the addData method, I would need to put

Map<String, <T>> data;

I don’t know the way to declare a configurable type in the map

thanks for anyone who will make that work


edit : it’s almost working like that

I’m getting the error
Exception in thread “main” java.lang.ClassCastException: DataObject cannot be cast to java.lang.Class
at DataObject.addData(test.java:41)

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.gson.Gson;

class DataObject<T> {

    private int data1 = 100;
    private String data2 = "hello";
DataObject child;
private List<String> list = new ArrayList<String>() {
  {
    add("String 1");
    add("String 2");
    add("String 3");
      }
    };
    private Map<String, Class<T>> data=null;

    public DataObject(int i){
        this.data1 = i;
        this.data = new HashMap<String, Class<T>>();
    }

    //getter and setter methods

    @Override
    public String toString() {
       return "DataObject [data1=" + data1 + ", data2=" + data2 + ", list="
    + list + "]";
    }
    public Map<String, Class<T>> getData() {
        return data;
    }

    public void addData(final String key, Object value, Class<T> t) {
        data.put(key, (Class<T>) value);
    }
}

public class test {



    /**
 * @param args
 */
public static void main(String[] args) {

    DataObject obj = new DataObject(12);
    obj.child = new DataObject(25);
    obj.addData("myOtherData", new DataObject(32), DataObject.class);
        Gson gson = new Gson();
        System.out.println(gson.toJson(obj));
    }

}
  • 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-03T00:44:38+00:00Added an answer on June 3, 2026 at 12:44 am

    EDIT:
    Remove the class parameter of the addData() method, it is not needed. Simply type the value with T directly and type datawith Map<String, T>. Initialize the map with: this.data = new HashMap<String, T>();


    This works for me (I typed your Map value with T and removed the unncesary Class parameter of addData). But your code actually works without those modifications. I ran it on my machine and I had the same output as the one you mark as “working”.

    What is a little bit weird, is your typing of DataObject. It is confusing because you have also an inside member (child) which is of the same class. I don’t know if it should be of the same type DataObject<T> or not. You should try to type properly your generics as much as possible. If you are unable to do so, it may be the sign of some incorrect use of generics.

    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    import com.google.gson.Gson;
    
    class DataObject<T> {
    
        private List<String> list = new ArrayList<String>() {
            {
                add("String 1");
                add("String 2");
                add("String 3");
            }
        };
        private int data1 = 100;
        private String data2 = "hello";
        DataObject child;
        private Map<String, T> data = null;
    
        public DataObject(int i) {
            this.data1 = i;
            this.data = new HashMap<String, T>();
        }
    
        // getter and setter methods
    
        @Override
        public String toString() {
            return "DataObject [data1=" + data1 + ", data2=" + data2 + ", list=" + list + "]";
        }
    
        public Map<String, T> getData() {
            return data;
        }
    
        public void addData(final String key, T value) {
            data.put(key, value);
        }
    }
    
    public class test {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            DataObject obj = new DataObject(12);
            obj.child = new DataObject(25);
            obj.addData("myOtherData", new DataObject(32));
            Gson gson = new Gson();
            System.out.println(gson.toJson(obj));
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on the following code: import java.io.*; import javax.xml.parsers.*; import javax.xml.transform.*; import
I have the following code: package example; import java.awt.BorderLayout; import java.awt.FlowLayout; import javax.swing.JFrame; import
We have the following code working for a complex rails form with checkboxes. I'm
I'm trying to get the following code working: string url = String.Format(@SOMEURL); string user
Right now I have the following code working: @UiHandler(usernameTextBox) void onUsernameTextBoxKeyPress(KeyPressEvent event) { keyPress(event);
The following code is working great in a normal console application: private void button1_Click(object
Why is the following code not working? if(EventLog.Exists(Foo)) { EventLog.Delete(Foo); } if(EventLog.Exists(Foo) == false)
can anybody tell me why is the following code not working? <script type=text/javascript src=../../Scripts/jquery.js></script>
I am using the following code for my form validation. The code is working
i am using python 2.5.2 . The following code not working. def findValue(self, text,

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.