Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to initialize a two column arraylist?

I need to create a List that records two columns {int, String}. I think ArrayList is what I need but I cant get my head around it. I pulled the Strings from a database and the int is the index value which I need to identify the strings position for later.

List<List<String>> strArray = ArrayList<List<String>>;

then could I do something like strArray.add().add() for each row I pull from the database?

like image 755
tozhan Avatar asked Dec 06 '12 06:12

tozhan


4 Answers

Another approach would be to make a custom object:

Class CustomObject {
    int value1;
    String value2;

    CustomObject(int v1, String v2) {
        value1 = v1;
        value2 = v2;
    }
}

And then use it:

List<CustomObject> myList = new ArrayList<CustomObject>();
CustomObject o1 = new CustomObject(1, "one");
myList.add(o1);
// etc.

If the int values are unique and you want to consider them keys, then a Map would work as others have suggested.

like image 172
The111 Avatar answered Oct 09 '22 16:10

The111


I think you should use a HashMap with int as key and String as value if your int values are going to be unique.

Map<Integer,String> myMap = new HashMap<Integer,String>();
myMap.put(1,"ABC");

Note that as Map is a collections and java collections do not store primitive like int, they store objects so you have to use Integer wrapper class for your int values.

Refer this link Why can Java Collections not directly store Primitives types?

like image 43
Abubakkar Avatar answered Oct 09 '22 17:10

Abubakkar


If you need just two values you can use native Pair class

List<Pair> mPairs = new ArrayList<Pair>();

Pair pair = new Pair(123,"your string");
mPairs.add(pair);

This will be a good decision if you int values are not unique and so you can not use HashMap

like image 37
k_shil Avatar answered Oct 09 '22 16:10

k_shil


If your IDs are not unique, you still can use Map :

Map<Integer, String> map = new IdentityHashMap<Integer, String>();
map.put(new Integer(1), "string");

IdentityHashMap - use native hashCode implemetation for each OBJECT, so you don't need unique IDs, but you MUST create ALL Integers via operator 'new', and don't use autoboxing, because there is some cache mechanism.

Also there is JVM parameter, which controlls cache size '-XX:AutoBoxCacheMax='. But using this parameter you can't disable cache, if you set size to the zero, then cache will ignore it and use default: [-128; 127]. This parameter is only for Integers, there is no such kind of parameter for Long.

UPDATE Also for non unique keys you could use some sort of multimap: Map> map

And store in it your values with nonunique keys:

map.put(1, new ArrayList<String>());
map.get(1).add("value1");
map.get(1).add("value2");

You can use HashMap for that for example.

Also you can find MultiMap implementation in google-collections: 'guava'.

like image 44
kornero Avatar answered Oct 09 '22 17:10

kornero