Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 563
user2963241 Avatar asked Dec 28 '25 16:12

user2963241


1 Answers

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,

like image 75
Anders R. Bystrup Avatar answered Dec 31 '25 06:12

Anders R. Bystrup



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!