Is there a Java class like ArrayList that can do this?

I sometimes ran into this problem when programming.

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

I want to be able to store each row of a table in a dynamic array. Therefore, each element of the array must contain a string and an integer.

I used to do this by simply breaking 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 with the same index.

But is there a better way? I know that there are classes like HashMap, but they do not allow duplicate keys. I am looking for something that allows duplicate entries.

I know you can do something like this:

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

I really don't want that every time I get an element from an array, I should use String and Integer, but maybe this is the only way without creating my own? This seems more confusing to me, and I'd rather use two ArrayLists.

So, is there any Java object like ArrayList where it will work as follows:

 ArrayList<String, Integer> myArray = new ArrayList<String, Integer>(); myArray.add("string", 123); 
+4
source share
9 answers

Just create a simple POJO class to store string data. Do not forget about equals and hashCode and prefer an 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; } } 

Using:

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

Note: in other languages ​​there are built-in solutions for it, for example case-classes and tuples in Scala.

+10
source

Create a Row class containing 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)); } } 
+4
source

A map is an option if you are sure that any value from an integer or a string is unique. Then you can put this unique value as a key. If this is not the case for your case, creating a simple POJO is the best option for you. Infact, if in the future there is a chance to get more values ​​(columns) per row, then using POJO will be less time. You can define a POJO as;

 public class Data { private int intValue; private String strValue; public int getIntValue() { return intValue; } public void setIntValue(int newInt) { this.intValue = newInt; } public String getStrValue() { return strValue; } public void setStrValue(String newStr) { this.strValue = newStr; } 

And in the class, you can use it like;

 ArrayList<Data> dataList = new ArrayList<Data>(); Data data = new Data(); data.setIntValue(123); data.setStrValue("string"); dataList.add(data); 
+4
source

You can create a very simple object, for example:

 public class Row{ private String strVal; private Integer intVal; public Row(String s, Integer i){ strVal = s; intVal = i; } //getters and setters } 

Then use it as follows:

 ArrayList<Row> myArray = new ArrayList<Row>(); myArray.add(new Row("string", 123)); 
+3
source

You should create a class (e.g. Foo) that contains an int and a string.

Then you can create an ArrayList object from Foo objects.

List<Foo> fooList = new ArrayList<Foo>();

+2
source

This is called my friend’s card. It looks like a dictionary in .net

http://docs.oracle.com/javase/6/docs/api/java/util/Map.html

+1
source

HashMap my be the class you are looking for, assuming the "string" will be different for different values. Here is the documentation on HashMap

Example:

 HashMap<String, Integer> tempMap = new HashMap<String, Integer>(); tempMap.put("string", 124); 

If you need to add multiple values, you can create a HashMap<String, ArrayList> like this.

0
source

Use Map to solve this problem:

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

For instance:

 map.put("string", 123); 
0
source

you can use the google collection guava library , there is a map called multimap. This collection is similar to a map, but which can associate multiple values ​​with one key. If you call put (K, V) twice with the same key but different values, the multimap contains key mappings for both values.

0
source

Source: https://habr.com/ru/post/1438804/


All Articles