简体   繁体   English

在 hashSet 中插入多个值 Java

[英]Insert multiple values in hashSet Java

The idea is to set different values from one class to another using the interface.这个想法是使用接口将不同的值从一个 class 设置到另一个。

My code:我的代码:

public Make(String name, int foundingYear, String founder) {
    this.name = name;
    this.foundingYear = foundingYear;
    this.founder = founder;
}
//other code

So I want to set those values in another class like this:所以我想在另一个 class 中设置这些值,如下所示:

public record anotherClass (String name, String color, int hp) {

Make make = new Make();
Set<Make> makes = new HashSet<>();
makes.add("name", 1995, "founder");

The thing is when I'm trying to add values ( makes.add("name", 1995, "founder"); ) I can't do that, because it expects just one argument to be added of type makes .问题是当我尝试添加值时( makes.add("name", 1995, "founder"); )我不能这样makes ,因为它只希望添加一个 make 类型的参数。 What I'm I doing wrong?我做错了什么?

You must instantiate Make using the 3 params, then add this make into the Set.您必须使用 3 个参数实例化 Make,然后将此 make 添加到 Set 中。 See below:见下文:

Make make = new Make("name", 1995, "founder"); // use this make
Set<Make> makes = new HashSet<>();
makes.add(make);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM