user1896796
user1896796

Reputation: 749

Mapping all values to one key

      Map<Object,String> mp=new HashMap<Object, String>();
      Scanner sc=new Scanner(System.in);
      System.out.println("Enter the instrument name");
      String name=sc.next();


        mp.put(name, "Control Valve");
        mp.put(name, "BDV");
        mp.put(name, "SDV");
        mp.put(name, "ON-OFF VALVE");
        mp.put(name,"Analyser");


        Set s=mp.entrySet();

        Iterator it=s.iterator();

        while(it.hasNext())
        {

            Map.Entry m =(Map.Entry)it.next();

            String key=(String)m.getKey();

            String value=(String)m.getValue();

            System.out.println("Instrument name :"+key+"  fields:"+value);
        }
    }
}

In this only last value is mapped to the key .i.e analyser to key .

How to map all the values to one key entered by the user .And also it has to ask user to enter values for each value field.

updated code -:It asks for instrument name but then shows exception "java.util.ArrayList cannot be cast to java.lang.String"

 Map<String,List<String>> mp=new HashMap<String,List<String>>();
      Scanner sc=new Scanner(System.in);
      System.out.println("Enter the instrument name");
      String name=sc.next();
      List<String> valList = new ArrayList<String>();
      valList.add("Control Valve");
      valList.add("BDV");
      valList.add("SDV");
      valList.add("ON-OFF VALVE");
      valList.add("Analyser");
      mp.put(name, valList);




        Set s=mp.entrySet();

        Iterator it=s.iterator();

        while(it.hasNext())
        {

            Map.Entry m =(Map.Entry)it.next();

            String key=(String)m.getKey();

            String value=(String)m.getValue();

            System.out.println("Instrument name :"+key+" fields:"+value);
        }
    }
}

Upvotes: 0

Views: 425

Answers (5)

someone
someone

Reputation: 6572

I would suggest to use

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

So that you can maintain set of values to a given key.

if a list available for given key , get the list and add the new value to list.

UPDATED

Map<String,List<String>> mp=new HashMap<String,List<String>>();
      Scanner sc=new Scanner(System.in);
      System.out.println("Enter the instrument name");
      String name=sc.next();
      List<String> valList = new ArrayList<String>();
      valList.add("Control Valve");
      valList.add("BDV");
      valList.add("SDV");
      valList.add("ON-OFF VALVE");
      valList.add("Analyser");
      mp.put(name,valList);

      for(String key : mp.keySet()){
          System.out.print("Instrument name :"+key+" Values : ");
          for(String val : mp.get(key)){
              System.out.print(val+",");
          }


      }

Upvotes: 3

Peter Lawrey
Peter Lawrey

Reputation: 533870

I suspect you should be using a custom class with a field for each piece of information you need. This can be added once into the map for each name

public static void main(String... args) {
    Map<String, MyType> mp = new LinkedHashMap<String, MyType>();
    Scanner sc = new Scanner(System.in);

    System.out.println("Enter the following comma separated with a blank line to stop.");
    System.out.println("instrument name, Control Value, BDV, SDV, ON-OFF VALVE, Analyser");
    for (String line; (line = sc.nextLine()).length() > 0; ) {
        MyType mt = new MyType(line);
        mp.put(mt.getName(), mt);
    }
    System.out.println("you entered");
    for (MyType myType : mp.values()) {
        System.out.println(myType);
    }
}

static class MyType {
    private final String name, controlValue, bdv, sdv, onOffValue, analyser;

    MyType(String line) {
        String[] parts = line.trim().split(" *, *");
        name = parts[0];
        controlValue = parts[1];
        bdv = parts[2];
        sdv = parts[3];
        onOffValue = parts[4];
        analyser = parts[5];
    }

    public String getName() {
        return name;
    }

    public String getControlValue() {
        return controlValue;
    }

    public String getBdv() {
        return bdv;
    }

    public String getSdv() {
        return sdv;
    }

    public String getOnOffValue() {
        return onOffValue;
    }

    public String getAnalyser() {
        return analyser;
    }

    @Override
    public String toString() {
        return "MyType{" +
                "name='" + name + '\'' +
                ", controlValue='" + controlValue + '\'' +
                ", bdv='" + bdv + '\'' +
                ", sdv='" + sdv + '\'' +
                ", onOffValue='" + onOffValue + '\'' +
                ", analyser='" + analyser + '\'' +
                '}';
    }
}

if given the following input prints

Enter the following comma separated with a blank line to stop.
instrument name, Control Value, BDV, SDV, ON-OFF VALVE, Analyser
a,b,c,d,e,f
1,2,3,4,5,6
q,w,e,r,t,y

you entered
MyType{name='a', controlValue='b', bdv='c', sdv='d', onOffValue='e', analyser='f'}
MyType{name='1', controlValue='2', bdv='3', sdv='4', onOffValue='5', analyser='6'}
MyType{name='q', controlValue='w', bdv='e', sdv='r', onOffValue='t', analyser='y'}

Upvotes: 1

hmatar
hmatar

Reputation: 2429

You may have a map of string array. Then you should retrive the array list and add new value.

  Map<Object,ArrayList<String>> mp=new HashMap<Object, ArrayList<String>>();

Upvotes: 0

Brian Agnew
Brian Agnew

Reputation: 272417

I would suggest looking at a Guava MultiMap

A collection similar to a Map, but which may associate multiple values with a single key. If you call put(K, V) twice, with the same key but different values, the multimap contains mappings from the key to both values.

Otherwise you will have to implement a Map of Strings to some Java collection. That will work, but it's painful since you have to initialise the collection every time you enter a new key, and add to the collection if the empty collection exists for a key.

Upvotes: 0

kosa
kosa

Reputation: 66667

You may need to use google guava multimap to achieve this.

A collection similar to a Map, but which may associate multiple values with a single key. If you call put(K, V) twice, with the same key but different values, the multimap contains mappings from the key to both values.

(or)

Change value as List and add all values which have same key to that list

Example:

List<String> valList = new ArrayList<String>();
valList.add(val1);
valList.add(val2);
mp.put(name, valList);

Upvotes: 0

Related Questions