我不明白我的代码中缺少了什么 import java.util.*; 公共类汽车产品{ 字符串颜色; 字符串模型名; 串价; 公共CarProduct(字符串c、字符串m、字符串p){ 颜色=c; modelname=m; 价格=p; } } 类HashMapApplication{ 公共静态void main(字符串[]ar){ ArrayList ArrayList 1=新的ArrayList(); ArrayList ArrayList 2=新的ArrayList(); ArrayList ArrayList 3=新的ArrayList(); HashMap HashMap=新的HashMap(); CarProduct Tata=新的CarProduct(“黑色”、“120万”、“咏叹调”); arraylist1.add(塔塔); hashmap.put(“塔塔”,ArrayList 1); CarProduct WolksWagen=新的CarProduct(“灰白色”、“100万”、“帕萨特”); arraylist2.add(WolksWagen); hashmap.put(“WolksWagen”,arraylist2); CarProduct Mahindra=新的CarProduct(“白色”、“150万”、“XUV”); arraylist3.add(Mahindra); hashmap.put(“Mahindra”,ArrayList 3); //获取(整数索引) //map.get(id).add(value); //hashmap.get(“”) //这包含错误我不知道如何迭代它,因为类在那里,我需要访问其中的每个字段 for(条目:hashmap.entrySet()){ System.out.print(entry.getKey()+“|”); for(字符串属性:entry.getValue()){ 系统输出打印(属性+“”); } System.out.println(); } } }
我想从hashmap中提取值。 请帮助,键已给出,值将为arraylist。 请帮助转换字符串中的对象内容,并使用get方法显示每个值我不明白我的代码中缺少了什么 import java.util.*; 公共类汽车产品{ 字符串颜色; 字符串模型名; 串价; 公共CarProduct(字符串c、字符串m、字符串p){ 颜色=c; modelname=m; 价格=p; } } 类HashMapApplication{ 公共静态void main(字符串[]ar){ ArrayList ArrayList 1=新的ArrayList(); ArrayList ArrayList 2=新的ArrayList(); ArrayList ArrayList 3=新的ArrayList(); HashMap HashMap=新的HashMap(); CarProduct Tata=新的CarProduct(“黑色”、“120万”、“咏叹调”); arraylist1.add(塔塔); hashmap.put(“塔塔”,ArrayList 1); CarProduct WolksWagen=新的CarProduct(“灰白色”、“100万”、“帕萨特”); arraylist2.add(WolksWagen); hashmap.put(“WolksWagen”,arraylist2); CarProduct Mahindra=新的CarProduct(“白色”、“150万”、“XUV”); arraylist3.add(Mahindra); hashmap.put(“Mahindra”,ArrayList 3); //获取(整数索引) //map.get(id).add(value); //hashmap.get(“”) //这包含错误我不知道如何迭代它,因为类在那里,我需要访问其中的每个字段 for(条目:hashmap.entrySet()){ System.out.print(entry.getKey()+“|”); for(字符串属性:entry.getValue()){ 系统输出打印(属性+“”); } System.out.println(); } } },java,collections,Java,Collections,我想从hashmap中提取值。 请帮助,键已给出,值将为arraylist。 请帮助转换字符串中的对象内容,并使用get方法显示每个值import java.util.*; import java.util.*; public class CarProduct{ String color; String modelname; String price; public CarProduct(String c, String m, String p){
import java.util.*;
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();
}
}
}
公共类汽车产品{
字符串颜色;
字符串模型名;
串价;
公共CarProduct(字符串c、字符串m、字符串p){
颜色=c;
modelname=m;
价格=p;
}
@凌驾
公共字符串toString()
{
返回“型号:”+modelname+“颜色:”+color+“价格:”+Price;
}
}
类HashMapApplication{
公共静态void main(字符串[]ar){
List ArrayList 1=新建ArrayList();
List ArrayList 2=新建ArrayList();
List ArrayList 3=新建ArrayList();
Map hashmap=新hashmap();
CarProduct Tata=新的CarProduct(“黑色”、“120万”、“咏叹调”);
arraylist1.add(塔塔);
hashmap.put(“塔塔”,ArrayList 1);
CarProduct WolksWagen=新的CarProduct(“灰白色”、“100万”、“帕萨特”);
arraylist2.add(WolksWagen);
hashmap.put(“WolksWagen”,arraylist2);
CarProduct Mahindra=新的CarProduct(“白色”、“150万”、“XUV”);
arraylist3.add(Mahindra);
hashmap.put(“Mahindra”,ArrayList 3);
for(Map.Entry:hashmap.entrySet()){
System.out.print(entry.getKey()+“|”);
for(CarProduct属性:entry.getValue()){
系统输出打印(属性+“”);
}
System.out.println();
}
}
}
所以有几件事需要纠正
首先是hashmap,我们需要确保hashmap的类型正确,因此我们已经指定了ArrayList的类型,否则我们只能获取对象
其次是for循环。这里我们需要更改内部for循环,以便它在CarProduct而不是字符串上循环
最后,为了打印属性,我们需要覆盖CarProduct中的toString()方法,这将允许您获取汽车产品并将其直接放入System.out.print()中,就像您所做的那样
我要补充的一点是,目前您将输入以错误的顺序放入初始值设定项。构造函数指定了颜色、型号和价格,但您使用的初始值设定项是颜色、型号和价格
编辑:使列表和地图更通用,以反映对原始问题的评论(Map.Entry:hashmap.entrySet()){
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();
}
}
}
System.out.print(entry.getKey()+“|”);
//首先获取arraylist。
ArrayList ArrayList=entry.getValue();
for(CarProduct x:arrayList){
//显示carProduct
}
System.out.println();
}
基本上,您正在尝试打印ArrayList.toString(),但这不会给出正确的响应。尝试首先获取arraylist,然后迭代其内容。您的
CarProduct
应该覆盖toString()
。您忘记描述您面临的问题。请回答您的问题,包括显示您期望的内容和您得到的内容的部分(错误/异常/错误结果)<代码>HashMap HashMap=新建HashMap()代码>=>Map hashmap=new hashmap()代码>那么我应该包括什么呢?对于(CarProduct x:arrayList)仍然有一个错误,就像两者都是不同的。对于将来的参考,我不知道您是否正在使用诸如eclipse、netbeans或IntelliJ之类的IDE,但是当我跨您的非逻辑错误(如for循环)复制代码时,会立即突出显示错误。
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();
}