Creando una lista de cadenas donde cada cadena es una clave, a una lista de tuplas (como valor), en Java

How can I create a list of strings where each string points to a list of tuples; In other words each string is a key to a list of tuples (as value)?

Each tuple should be of the form :

List<String> pref, where each element of the list pref (say pref_i):
pref1 --> {(T1:10),(T2:13), T3:3),...}
pref2 --> {(T1:7), (T4:3), (T5:1),...}
pref3 --> ...

preguntado el 23 de abril de 13 a las 13:04

4 Respuestas

Seems to me you want a Map, depending on the type of your tuple (assuming a String here):

Map<String, List<String>> prefs = new HashMap<String, List<String>>;

You can have your tuples be of any type, I used a String for convenience.

If your tuples are better represented though a Set or Map, change it accordingly.

Respondido 23 Abr '13, 13:04

MultiMap to the rescue!

There is no such thing as touples in java, unless you simulate them. A multimap is like having a Map, where key is String and Value is an Array of elements

multimapa

Respondido 23 Abr '13, 13:04

Maybe you could use a HashMap of HashMap ? The first HashMap to use your string keys and the second one to your tuples.

HashMap<String, HashMap<String, String>> myHashMap = new HashMap<String, HashMap<String, String>>();

Espero que esto ayude ! Chau !

Respondido 23 Abr '13, 13:04

You cal also use List.

import java.util.ArrayList;
    import java.util.List;
    public class TestArrayList {
        public static void main(String[] args) {

            List<String> tempList=new ArrayList<String>();
            List<String> temp1=new ArrayList<String>();
            temp1.add(0, "a");
            temp1.add(1, "b");
            List<String> temp2=new ArrayList<String>();
            temp2.add(0, "c");
            temp2.add(1, "d");
            List<String> temp3=new ArrayList<String>();
            temp3.add(0, "e");
            temp3.add(1, "f");
            tempList.addAll(0, temp1);
            tempList.addAll(1, temp2);
            tempList.addAll(1, temp3);

            for(int i=0;i<tempList.size();i++){
                System.out.println(">>"+tempList.get(i));
            }
        }
    }

Respondido 23 Abr '13, 13:04

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.