Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a Java Class similar to ArrayList that can do this?

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);
like image 593
Altherat Avatar asked Nov 28 '22 08:11

Altherat


2 Answers

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.

like image 146
Sergey Passichenko Avatar answered Dec 05 '22 03:12

Sergey Passichenko


Create a Row class that holds the data.

package com.stackoverflow;

import java.util.ArrayList;
import java.util.List;

/**
 * @author maba, 2012-10-10
 */
public class Row {
    private int intValue;
    private String stringValue;

    public Row(String stringValue, int intValue) {
        this.intValue = intValue;
        this.stringValue = stringValue;
    }

    public int getIntValue() {
        return intValue;
    }

    public String getStringValue() {
        return stringValue;
    }

    public static void main(String[] args) {
        List<Row> rows = new ArrayList<Row>();
        rows.add(new Row("string", 123));
    }
}
like image 26
maba Avatar answered Dec 05 '22 01:12

maba