Java 哈希映射。使用自定义类型作为值

Java 哈希映射。使用自定义类型作为值,java,hashmap,Java,Hashmap,我有类计算机,想使用计算机作为值的类型,而不是字符串。但我有一个错误: 错误:(22,36)java:不兼容的类型:java.lang.String不能为 转换为com.example.lesson18.Computer 公共类主{ 私有静态地图目录; 公共静态void main(字符串[]args){ catalogComputersMap=新HashMap(); ComputerStore cs=新的ComputerStore(); 目录计算机映射输出(1,“2x1.6GHz,4GB,512

我有类计算机,想使用计算机作为值的类型,而不是字符串。但我有一个错误:

错误:(22,36)java:不兼容的类型:java.lang.String不能为 转换为com.example.lesson18.Computer

公共类主{
私有静态地图目录;
公共静态void main(字符串[]args){
catalogComputersMap=新HashMap();
ComputerStore cs=新的ComputerStore();
目录计算机映射输出(1,“2x1.6GHz,4GB,512GB,GeForce 210350瓦”);
目录计算机映射输出(2,“2 x 3.2 GHZ,8 GB,1 TB,Radeon R7 370550瓦”);
我在计算机类中生成了equals()和hashCode()(这是我需要的吗?),但结果保持不变。工作代码必须与
一起使用,而不是其他任何东西。

但在这一行:

catalogComputersMap.put(1, "2 x 1.6 GHz, 4 GB, 512 GB, GeForce 210, 350 Watt");

您正在传递字符串作为第二个参数,而不是计算机对象。您应该传递一台计算机来完成此操作。

您正在将字符串作为值放入映射中

你必须用你想要的值创建一个计算机对象。我不知道你的计算机对象是什么样子的,但是如果它有一个带字符串的构造函数,代码会是这样的

Computer computer = new Computer("2 x 1.6 GHz, 4 GB, 512 GB, GeForce 210, 350 Watt");
catalogComputersMap.put(1, computer);

您正在尝试使用(int,String)参数调用方法,而该方法需要(int,Computer)参数。如果您的计算机类具有具有String参数的构造函数,则可以传递给put方法

catalogComputersMap = new HashMap<Integer, Computer>();
ComputerStore cs = new ComputerStore();
catalogComputersMap.put(1, new Computer("2 x 1.6 GHz, 4 GB, 512 GB, GeForce 210, 350 Watt"));
catalogComputersMap.put(2, new Computer("2 x 3.2 GHZ, 8 GB, 1 TB, Radeon R7 370, 550 Watt"));
catalogComputersMap=newhashmap();
ComputerStore cs=新的ComputerStore();
catalogComputersMap.put(1台新计算机(“2x1.6GHz,4GB,512GB,GeForce 210350瓦”);
目录计算机映射输出(2台新计算机(“2 x 3.2 GHZ,8 GB,1 TB,Radeon R7 370,550瓦”);
您尝试将字符串“2 x 1.6 GHz,4 GB,512 GB,GeForce 210,350瓦”作为值而不是计算机传递。您的计算机类必须如下所示:

public class Computer {

private String info;

public void setInfo(String info){
this.info = info;
}

public String getInfo (){
return this.info;
}

public Computer (String info){
this.info = info;
}
}
然后:

    public static void main(String[] args) {
       Map<Integer, Computer> catalogComputersMap = new HashMap<Integer, Computer>();

        catalogComputersMap.put(1, new Computer("2 x 1.6 GHz, 4 GB, 512 GB, GeForce 210, 350 Watt"));
        catalogComputersMap.put(2, new Computer("2 x 3.2 GHZ, 8 GB, 1 TB, Radeon R7 370, 550 Watt"));
}
publicstaticvoidmain(字符串[]args){
Map catalogComputersMap=新HashMap();
catalogComputersMap.put(1台新计算机(“2x1.6GHz,4GB,512GB,GeForce 210350瓦”);
目录计算机映射输出(2台新计算机(“2 x 3.2 GHZ,8 GB,1 TB,Radeon R7 370,550瓦”);
}

计算机对象是`公用计算机(字符串处理器、字符串ram、字符串hdd、字符串显卡、字符串电源){this.processor=processor;this.ram=ram;this.hdd=hdd;this.videoCard=videoCard;this.powerSupply=powerSupply;}`我只是把所有的东西简化成一个字符串来寻找解决方案。谢谢大家。最终的解决方案是
catalogComputersMap.put(1,新计算机(“2 x 1.6 GHz”、“4 GB”、“512 GB”、“GeForce 210”、“350瓦”);
在计算机类中我的对象
公共计算机(字符串处理器、字符串ram、字符串hdd、字符串显卡、字符串电源){this.processor=processor;this.ram=ram;this.hdd=hdd;this.videoCard=videoCard;this.powerSupply=powerSupply;}
    public static void main(String[] args) {
       Map<Integer, Computer> catalogComputersMap = new HashMap<Integer, Computer>();

        catalogComputersMap.put(1, new Computer("2 x 1.6 GHz, 4 GB, 512 GB, GeForce 210, 350 Watt"));
        catalogComputersMap.put(2, new Computer("2 x 3.2 GHZ, 8 GB, 1 TB, Radeon R7 370, 550 Watt"));
}