-3
import java.util.*;

public class CarProduct{

    String color;
    String modelname; 
    String price;   
    public CarProduct(String c, String m, String p){
        color = c;
        modelname = m;
        price = p;
    }
}

class HashMapApplication{

    public static void main(String []ar){

        ArrayList<CarProduct> arraylist1 = new ArrayList<CarProduct>(); 
        ArrayList<CarProduct> arraylist2 = new ArrayList<CarProduct>(); 
        ArrayList<CarProduct> arraylist3 = new ArrayList<CarProduct>(); 

        HashMap<String,ArrayList> hashmap = new HashMap<String,ArrayList>();

        CarProduct Tata = new CarProduct("black","12 Lakhs","Aria");
        arraylist1.add(Tata);
        hashmap.put("Tata",arraylist1);

        CarProduct WolksWagen = new CarProduct("off white","10 Lakhs","Passat");
        arraylist2.add(WolksWagen);
        hashmap.put("WolksWagen",arraylist2);

        CarProduct Mahindra = new CarProduct("white","15 Lakhs","XUV");
        arraylist3.add(Mahindra);
        hashmap.put("Mahindra",arraylist3);

        //get(int index)
        //map.get(id).add(value);

        //hashmap.get("")   
        // this contains error i dont know how iterate it because the class is there and i need access each and every field in it
        for (Entry<String, ArrayList<CarProduct>> entry : hashmap.entrySet()) {
            System.out.print(entry.getKey()+" | ");
            for(String property : entry.getValue()){
                System.out.print(property+" ");
            }
            System.out.println();
        }   

    }   

}

I want to extract the values from hashmap. Please help, the key is given and the value will be arraylist. Please help to convert the object thing in string and in displaying each and every value using get method

suvi
  • 3
  • 3
  • Your `CarProduct` should override `toString()`. – Eran Sep 01 '16 at 10:17
  • 1
    You forgot to describe problem you are facing. Please [edit] your question and include part which shows what you expect and what you get instead (error/exception/incorrect result). – Pshemo Sep 01 '16 at 10:18
  • [Don't use raw types](http://stackoverflow.com/questions/2770321/what-is-a-raw-type-and-why-shouldnt-we-use-it); `HashMap hashmap = new HashMap();` => `Map> hashmap = new HashMap<>();` – Jorn Vernee Sep 01 '16 at 10:20
  • then what should I include? – suvi Sep 01 '16 at 10:27

2 Answers2

0
import java.util.*;

public class CarProduct{

    String color;
    String modelname;
    String price;
    public CarProduct(String c, String m, String p){
        color = c;
        modelname = m;
        price = p;
    }

    @Override
    public String toString()
    {
        return "Model: " + modelname + " Colour:" + color + " Price:" + price ;
    }
}

class HashMapApplication{

    public static void main(String []ar){

        List<CarProduct> arraylist1 = new ArrayList<CarProduct>();
        List<CarProduct> arraylist2 = new ArrayList<CarProduct>();
        List<CarProduct> arraylist3 = new ArrayList<CarProduct>();

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

        CarProduct Tata = new CarProduct("black","12 Lakhs","Aria");
        arraylist1.add(Tata);
        hashmap.put("Tata",arraylist1);

        CarProduct WolksWagen = new CarProduct("off white","10 Lakhs","Passat");
        arraylist2.add(WolksWagen);
        hashmap.put("WolksWagen",arraylist2);

        CarProduct Mahindra = new CarProduct("white","15 Lakhs","XUV");
        arraylist3.add(Mahindra);
        hashmap.put("Mahindra",arraylist3);

        for (Map.Entry<String, List<CarProduct>> entry : hashmap.entrySet()) {
            System.out.print(entry.getKey()+" | ");
            for(CarProduct property : entry.getValue()){
                System.out.print(property+" ");
            }
            System.out.println();
        }

    }

}

So there's a couple of things to correct.

Firstly the hashmap, we need to make sure the hashmap is properly typed so we've specified the type of the ArrayList otherwise we'll only be able to get Objects.

Secondly the for loop. Here we need to change the inner for loop so that it loops on CarProduct's and not Strings.

Lastly for printing the property we need to override the toString() method in CarProduct this will allow you to get the car product and put it directly in a System.out.print() as you have done.

One thing I should add is that currently you are putting the inputs into your initializer in the wrong order. Your constructor specifies color, model, price but you're using your initializer as color, price, model.

EDIT: Made the lists and maps more generic to reflect the comments on the original question

superdyoll
  • 47
  • 6
  • For future reference, I don't know if you're using an IDE such as eclipse, netbeans or IntelliJ but when I copied your code across your non logic errors such as the the for loop were highlighted immediately. – superdyoll Sep 01 '16 at 11:02
0
  for (Map.Entry<String, ArrayList> entry : hashmap.entrySet()) {
                System.out.print(entry.getKey()+" | ");
                //get the arraylist first.
                ArrayList<CarProduct> arrayList = entry.getValue();
            for(CarProduct x: arrayList){
                   //display the carProduct
     }
       System.out.println();
   }

You are basically trying to print ArrayList.toString() which will not give proper response. Try to first get the arraylist and then iterate over its contents.

thedarkpassenger
  • 7,158
  • 3
  • 37
  • 61
  • for(CarProduct x: arrayList) still there is an error in this, like both are different – suvi Sep 01 '16 at 10:39