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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T08:40:02+00:00 2026-05-20T08:40:02+00:00

I am using an Arraylist to hold heterogeneous data…a mix of String , int

  • 0

I am using an Arraylist to hold heterogeneous data…a mix of String, int and class objects (These objects could be instances of any class I’ve defined.). I’ve initialised the ArrayList as ArrayList<Object> = new ArrayList<Object>.

But I think I’ve heard ppl saying that directly referring to the Objects is sth. bad/undesirable? I’m referring to the objects because I can’t find any suitable generics to use in my case. Is it ok to use or are there any suitable generics? (I’m relatively new to Java)

  • 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-20T08:40:02+00:00Added an answer on May 20, 2026 at 8:40 am

    Use different list for each type and keep those lists in a map and you can use the class object as the key so it wont mix up and easy to access.

     Map<Class<?>,List<?>> map = new HashMap<Class<?>,List<?>>();
     map.put(String.class, new ArrayList<String>());
     map.put(Integer.class, new ArrayList<Integer>());
     map.put(Class.class, new ArrayList<Class<?>>());
    

    EDIT:

    P.S: You will still get an unchecked warning when you retrieve the list from the map if you want to add new object into the list.

    public static void main(String[] args) {
            Map<Class<?>,List<?>> map = new HashMap<Class<?>,List<?>>();
            map.put(String.class, new ArrayList<String>());
            map.put(Integer.class, new ArrayList<Integer>());
            map.put(Class.class, new ArrayList<Class<?>>());
    
            String str = "deneme";
    
            @SuppressWarnings("unchecked")
            List<String> strList =  (List<String>) map.get(String.class);
            strList.add(str);
            strList.add("str2");
            strList.add("str2");
    
            @SuppressWarnings("unchecked")
            List<Class<?>> classList =  (List<Class<?>>) map.get(Class.class);
            classList.add(String.class);
            classList.add(Integer.class);
            classList.add(Double.class);
    
    
            for(String currentStr:strList){
                System.out.println(currentStr);
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm building a thesaurus using a HashMap <String,ArrayList<String>> to hold words and their synonyms
I have created an ArrayList<HashMap<String, String>> collection to hold my data for ListView .
I have an ArrayList that I want to use to hold RaceCar objects that
I was wondering what the performance impact would be when using ArrayList.ListIterator(int index -
If I'm using an ArrayList in C#.NET, is the order guaranteed to stay the
How would I get the length of an ArrayList using a JSF EL expression?
Using C# and System.Data.SqlClient, is there a way to retrieve a list of parameters
at the moment I'm using a List<short> as a buffer to hold things for
It's obvious that a parent class's object can hold a reference to a child,
I have a timeseries class that, over the course of a day will hold

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.