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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T14:48:50+00:00 2026-06-12T14:48:50+00:00

I have been running into this problem sometimes when programming. Imagine I have a

  • 0

I have been running into this problem sometimes when programming.

Imagine I have a table of data with two columns. The first column has strings, the second column has integers.

I want to be able to store each row of the table into a dynamic array. So each element of the array needs to hold a string and an integer.

Previously, I have been accomplishing this by just splitting each column of the table into two separate ArrayLists and then when I want to add a row, I would call the add() method once on each ArrayList. To remove, I would call the remove(index) method once on each ArrayList at the same index.

But isn’t there a better way? I know there are classes like HashMap but they don’t allow duplicate keys. I am looking for something that allows duplicate entries.

I know that it’s possible to do something like this:

ArrayList<Object[]> myArray = new ArrayList<Object[]>();
myArray.add(new Object[]{"string", 123});

I don’t really want to have to cast into String and Integer every time I get an element out of the array but maybe this is the only way without creating my own? This looks more confusing to me and I’d prefer using two ArrayLists.

So is there any Java object like ArrayList where it would work like this:

ArrayList<String, Integer> myArray = new ArrayList<String, Integer>();
myArray.add("string", 123);
  • 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-12T14:48:51+00:00Added an answer on June 12, 2026 at 2:48 pm

    Just create simple POJO class to hold row data. Don’t forget about equals and hashCode and prefer immutable solution (without setters):

    public class Pair {
        private String key;
        private Integer value;
    
        public Pair(String key, Integer value) {
            this.key = key;
            this.value = value;
        }
    
        public String getKey() {
            return key;
        }
    
        public Integer getValue() {
            return value;
        }
    
        // autogenerated
    
        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (!(o instanceof Pair)) return false;
    
            Pair pair = (Pair) o;
    
            if (key != null ? !key.equals(pair.key) : pair.key != null) return false;
            if (value != null ? !value.equals(pair.value) : pair.value != null) return false;
    
            return true;
        }
    
        @Override
        public int hashCode() {
            int result = key != null ? key.hashCode() : 0;
            result = 31 * result + (value != null ? value.hashCode() : 0);
            return result;
        }
    }
    

    Usage:

        List<Pair> list = new ArrayList<Pair>();
        list.add(new Pair("string", 123));
    

    Note: in other languages there are build-in solutions for it like case-classes and tuples in Scala.

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

Sidebar

Related Questions

I've been running into this problem a lot lately. I have an NSMutableArray called
I have been running into this problem several times. Basically, at work we use
I have been running into a problem with python's re module. This is the
I have been using this code, but have been running into some memory issues:
I have been running into this error for the last 3 or 4 weeks
I've been running into a problem where using objects from two datacontexts that are
So I have been running into all kinds of interesting problems in VisualStudio 2008
I have been trying to learn Erlang and have been running into some problems
I'm exploring package building on Windows 7 but have been running into trouble after
I have been attempting to learn Grails, however I am running into the following

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.