suvi suvi - 3 months ago 9
Java Question

I dont understand what is missing in my code

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

Answer
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