user2963241
user2963241

Reputation: 45

storing multiple values in a map against single key

I have the following file named ght.txt in my c: and it contains the following data

Id|ytr|yts
1|W|T
2|W|T
3|W|T

Now the thing is that positions of this columns (Id|ytr|yts) is also not in order means they can be reshuffled also..for ex

Id|ytr|dgfj|fhfjk|fgrt|yts

or they can be as ..

Id|wer|ytr|weg|yts

so I have done the following way and read them in java as shown below

String[] headers = firstLine.split("|");
int id, Ix, Ixt, count = 0;

for(String header : headers) {
    if(header.equals("Id")) {
        idIx = count;
    }elseif (header.equals("Ix")) {
        Ixt = count;
    } elseif (header.equals("Ixt")) {
        Ixt = count;
    }
    count++;
}

Now I need to store them in a map in such a way that against id I will get the value of column ytr and yts so in map there should be single key but against that key value could be multiple please advise how to store in map in such a way

Upvotes: 3

Views: 3224

Answers (6)

Alekya
Alekya

Reputation: 243

 public static void main(String[] args) {

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

List<String> valSetOne = new ArrayList<String>();
valSetOne.add("ABC");
valSetOne.add("BCD");
valSetOne.add("DEF");

List<String> valSetTwo = new ArrayList<String>();
valSetTwo.add("CBA");
valSetTwo.add("DCB");

map.put("FirstKey", valSetOne);
map.put("SecondKey", valSetTwo);

for (Map.Entry<String, List<String>> entry : map.entrySet()) {

    String key = entry.getKey();

    List<String> values = entry.getValue();

    System.out.println("Value of " + key + " is " + values);

}

}

You can use Set or List based on your requirement i.e you need elements in ordered or unordered collection.This is a simple method of having single key with multiple values.

Upvotes: 0

Anders R. Bystrup
Anders R. Bystrup

Reputation: 16050

Using a Map<Integer,List<String>> sounds like a viable first approach.

As it sound like your value is structured, it might be even better to create a value class to hold this, eg. Map<Integer, YourValueClass> where

class YourValueClass
{
    String ix;
    String ixt;

    // constructor, getters and setters
}

Basically, you should think in terms of classes/objects - don't be in object denial :-)

Cheers,

Upvotes: 6

Nitin Dandriyal
Nitin Dandriyal

Reputation: 1607

Value Class

class TextValues {
    final int id;
    final String ix;
    final String ixt;

    private TextValues(final int id, final String ix, final String ixt){
        this.id = id;
        this.ix = ix;
        this.ixt = ixt;
    }    

    public static TextValues createTextValues(int id, String ix, String ixt) {
        return new TextValues(id, ix, ixt);
    }    
}

Usage:

Map<Integer, TextValues> map = new HashMap<Integer, TextValues>();
map.put(1, TextValues.createTextValues(1, "ix value ", "ixt value"));

Upvotes: 1

Pradeep Simha
Pradeep Simha

Reputation: 18123

You can use MultiMap from Guava Library:

MultiMap<String,String> map = ArrayListMultimap.create();
map.put("key","value1");
map.put("key","value2");

By using:

System.out.println(map.get("key");

Prints:

["value1","value2"]

Upvotes: 1

I'm not quite sure what you mean, but if I get it right, you are looking for a multimap.

You can roll one yourself, as @Anders R. Bystrup suggests.
Or you can use an existing implementation like the Google Collections Multimap.

Upvotes: 3

Bennet
Bennet

Reputation: 387

Don't store one key and multiple values. Instead, you can store a Key and Values as a List.

Upvotes: 2

Related Questions