Java - 哈希图可以有 4 个通用参数而不是 2 个吗?

发布于 2024-12-02 14:56:04 字数 332 浏览 0 评论 0原文

这可能很难解释,但这里是:

我想将 3 个整数和一个字符串存储到 Hashmap,这样我就可以从映射中检索数据,但事实证明,Hashmap 只允许 2 个通用参数,而不是 4 个。

例如: HashMap; <整数> <整数> (我想做的),

但你只能使用 2 个参数,如下所示: HashMap; <整数>

我最好的猜测是我的想法无法实现,如果是这样,请列出处理此类问题的替代方案。

This may be difficult to explain, but here goes:

I want to store 3 integers and a String to a Hashmap, so I can retrieve data from the map, but it turns out that hashmaps only allow 2 generic parameters instead of 4.

For example: HashMap <String> <Integer> <Integer> <Integer> (what I want to do)

but you can only use 2 parameters, as it seems: HashMap <String> <Integer>.

My best guess is that my idea cannot be done, if so please list the alternatives to handling something like this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

岁吢 2024-12-09 14:56:05

您不需要哈希图来存储 4 个值。存储 3 个整数和 1 个字符串:

public class MyClass {
  int a,b,c;
  String d;
}

You don't need a hashmap to store 4 values. To store 3 integers and 1 String:

public class MyClass {
  int a,b,c;
  String d;
}
一杆小烟枪 2024-12-09 14:56:05

你可以间接得到答案,就像将三个整数组合成一个字符串,

int val1=1;
int val2=2;
int val3=3;
Map<String,String> test = new HashMap<String,String>();
test.put("key1", val1+"_"+val2+"_"+val3);
when you wan to get the values, int[] rst = test.get("key1).split("_");

然后你就可以访问你的整数值。

You can get the answer indirectly, like composing the three integer to one character string,

int val1=1;
int val2=2;
int val3=3;
Map<String,String> test = new HashMap<String,String>();
test.put("key1", val1+"_"+val2+"_"+val3);
when you wan to get the values, int[] rst = test.get("key1).split("_");

Then you can access your integer values.

卖梦商人 2024-12-09 14:56:05

在我看来,您正在尝试将两种不同类型的事物存储为哈希映射中的值。这样做是没有问题的。只需使用默认构造函数创建哈希映射,然后使用 Object 作为值类型即可。所以 new HashMap()

It seems to me that you are trying to store two different types of things as values in the hash map. There is no problem in doing this. Just create the hash map with the default constructor, and then just use Object as the value type. so new HashMap<String, Object>()

像极了他 2024-12-09 14:56:05

您可以使用 HashMap<您的密钥类型,对象>存储任意对象。

You can use a HashMap< TypeOfYourKey, Object > to store arbitrary objects.

彩虹直至黑白 2024-12-09 14:56:05

我在同样的问题上遇到了困难。我最终创建了自定义类的哈希图。这完全有效,并允许我将我想要的任何属性放入我的类中,并以编程方式为任何项目提取这些属性。完整示例如下。

public class Test1 {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.addview);


//create the data mapping
    HashMap<Integer, myClass> hm = new HashMap<Integer, myClass>();
    hm.put(1, new myClass("Car", "Small", 3000));
    hm.put(2, new myClass("Truck", "Large", 4000));
    hm.put(3, new myClass("Motorcycle", "Small", 1000));



//pull the datastring back for a specific item.
//also can edit the data using the set methods.  this just shows getting it for display.
    myClass test1 = hm.get(1);
    String testitem = test1.getItem();
    int testprice = test1.getPrice();
    Log.i("Class Info Example",testitem+Integer.toString(testprice));

}

}








class myClass{
    private String item;
    private String type;
    private int price;


    public myClass(String itm, String ty, int pr){
        this.item = itm;
        this.price = pr;
        this.type = ty;
    }

    public String getItem() {
        return item;
    }

    public void setItem(String item) {
        this.item = item;
    }

    public String getType() {
        return item;
    }

    public void setType(String type) {
        this.type = type;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

}

I struggled with this same issue. I ended up creating a hashmap of a custom class. This fully worked, and allowed me to put whatever attributes I wanted in my class, and pull out those attributes for any item programmatically. Full example below.

public class Test1 {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.addview);


//create the data mapping
    HashMap<Integer, myClass> hm = new HashMap<Integer, myClass>();
    hm.put(1, new myClass("Car", "Small", 3000));
    hm.put(2, new myClass("Truck", "Large", 4000));
    hm.put(3, new myClass("Motorcycle", "Small", 1000));



//pull the datastring back for a specific item.
//also can edit the data using the set methods.  this just shows getting it for display.
    myClass test1 = hm.get(1);
    String testitem = test1.getItem();
    int testprice = test1.getPrice();
    Log.i("Class Info Example",testitem+Integer.toString(testprice));

}

}








class myClass{
    private String item;
    private String type;
    private int price;


    public myClass(String itm, String ty, int pr){
        this.item = itm;
        this.price = pr;
        this.type = ty;
    }

    public String getItem() {
        return item;
    }

    public void setItem(String item) {
        this.item = item;
    }

    public String getType() {
        return item;
    }

    public void setType(String type) {
        this.type = type;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

}
凌乱心跳 2024-12-09 14:56:04

创建一个新类,其中包含 3 个 Integerint

class Triple {
    Integer i;
    Integer j;
    Integer k;

    Triple(Integer i,Integer j, Integer k) {
        this.i = i;
        this.j = j;
        this.k = k;
    }
}

并将此类放入带有字符串的映射中。

HashMap map = new HashMap<String, Triple>();
map.put("keyString", new Triple(new Integer(1),new Integer(2),new Integer(3)));

Make a new class which holds 3 Integer or maybe int.

class Triple {
    Integer i;
    Integer j;
    Integer k;

    Triple(Integer i,Integer j, Integer k) {
        this.i = i;
        this.j = j;
        this.k = k;
    }
}

and put this class to a map with the String.

HashMap map = new HashMap<String, Triple>();
map.put("keyString", new Triple(new Integer(1),new Integer(2),new Integer(3)));
苄①跕圉湢 2024-12-09 14:56:04

您应该创建一个对象来保存该数据,然后像这样存储它:HashMap

另外,这些不是构造函数。它们是仿制药。

You should create an object to hold that data, and then store it like this: HashMap<String, MyObject>.

Also, these aren't constructors. They are generics.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文