Java 如何在列表中存储多个参数?

Java 如何在列表中存储多个参数?,java,list,set,key,unique,Java,List,Set,Key,Unique,我想知道如何在单个列表中保存与一个键相关的不同参数 例如,假设我有一个名单,其中有一个姓名、一个姓、一个薪水和一个键,可以在名单中找到任何独特的人 我可以使用什么将这些信息存储在单个列表中 我可以使用哪些结构?如列表,数组列表,哈希映射,映射,集合等 我必须定义一个新类吗 您可能应该定义一个具有您提到的所有属性的Person类。然后,您可以将人员存储在地图或地图中,具体取决于人员的唯一标识符是整数还是字符串 如果您使用Map的HashMap实现,您将能够在预期的恒定时间内通过其标识符键定位Per

我想知道如何在单个列表中保存与一个键相关的不同参数

例如,假设我有一个名单,其中有一个姓名、一个、一个薪水和一个,可以在名单中找到任何独特的人

我可以使用什么将这些信息存储在单个列表中

我可以使用哪些结构?如列表数组列表哈希映射映射集合


我必须定义一个新类吗

您可能应该定义一个具有您提到的所有属性的Person类。然后,您可以将人员存储在
地图
地图
中,具体取决于人员的唯一标识符是
整数
还是
字符串


如果您使用
Map
HashMap
实现,您将能够在预期的恒定时间内通过其标识符键定位
Person

您可能应该定义一个具有您提到的所有属性的Person类。然后,您可以将人员存储在
地图
地图
中,具体取决于人员的唯一标识符是
整数
还是
字符串


如果您使用
Map
HashMap
实现,您将能够在预期的恒定时间内通过其标识符键定位
Person

您可能应该定义一个具有您提到的所有属性的Person类。然后,您可以将人员存储在
地图
地图
中,具体取决于人员的唯一标识符是
整数
还是
字符串


如果您使用
Map
HashMap
实现,您将能够在预期的恒定时间内通过其标识符键定位
Person

您可能应该定义一个具有您提到的所有属性的Person类。然后,您可以将人员存储在
地图
地图
中,具体取决于人员的唯一标识符是
整数
还是
字符串


如果您使用
Map
HashMap
实现,您将能够在预期的恒定时间内通过其标识符键定位
人员。

您可以使用一个映射,其中人员将是一个具有您提到的属性的人员类,并且该键将是一个标识每个人员实体的字符串

然后,你可以做一些类似的事情:

Map<String,Person> peopleMap = new Map<String,Person>();
Person person = new Person(); //let's imagine this person has a string id attribute with a get method defined

peopleMap.put(person.getId(),person); //adds the person to the list

peopleMap.get("p71"); //assume we're looking for a person which id="p71";

您可以使用一个映射,其中person是一个person类,具有您提到的属性,键是一个标识每个person实体的字符串

然后,你可以做一些类似的事情:

Map<String,Person> peopleMap = new Map<String,Person>();
Person person = new Person(); //let's imagine this person has a string id attribute with a get method defined

peopleMap.put(person.getId(),person); //adds the person to the list

peopleMap.get("p71"); //assume we're looking for a person which id="p71";

您可以使用一个映射,其中person是一个person类,具有您提到的属性,键是一个标识每个person实体的字符串

然后,你可以做一些类似的事情:

Map<String,Person> peopleMap = new Map<String,Person>();
Person person = new Person(); //let's imagine this person has a string id attribute with a get method defined

peopleMap.put(person.getId(),person); //adds the person to the list

peopleMap.get("p71"); //assume we're looking for a person which id="p71";

您可以使用一个映射,其中person是一个person类,具有您提到的属性,键是一个标识每个person实体的字符串

然后,你可以做一些类似的事情:

Map<String,Person> peopleMap = new Map<String,Person>();
Person person = new Person(); //let's imagine this person has a string id attribute with a get method defined

peopleMap.put(person.getId(),person); //adds the person to the list

peopleMap.get("p71"); //assume we're looking for a person which id="p71";
您不必这样做,但我强烈建议您定义一个类。它可能是这样的:

package com.yourcompany.projectname;

public class Person {
    public String name;
    public String familyName;
    public BigDecimal salary;
}
然后将其存储在HashMap中:

public static void main(String[] args) {
    Map <String, Person> persons = new HasMap <>();

    // define a new person
    Person dady = new Person();
    dady.name = "Tim";
    dady.familyName = "Smith";
    dady.salary = new BigDecimal("1234.56");

    // store it
    persons.put("dady", dady);

    // retrieve it
    Person misterX = persons.get("dady");

    // use it
    System.out.println("MisterX: "+misterX.familyName);
}
publicstaticvoidmain(字符串[]args){
Map persons=newhasmap();
//定义一个新人
人爸爸=新人();
dady.name=“Tim”;
dady.familyName=“史密斯”;
dady.salary=新的大十进制(“1234.56”);
//储存
人。把(“爸爸”,爸爸);
//找回它
Person misterX=persons.get(“爸爸”);
//使用它
System.out.println(“MisterX:+MisterX.familyName”);
}
您不必这样做,但我强烈建议您定义一个类。它可能是这样的:

package com.yourcompany.projectname;

public class Person {
    public String name;
    public String familyName;
    public BigDecimal salary;
}
然后将其存储在HashMap中:

public static void main(String[] args) {
    Map <String, Person> persons = new HasMap <>();

    // define a new person
    Person dady = new Person();
    dady.name = "Tim";
    dady.familyName = "Smith";
    dady.salary = new BigDecimal("1234.56");

    // store it
    persons.put("dady", dady);

    // retrieve it
    Person misterX = persons.get("dady");

    // use it
    System.out.println("MisterX: "+misterX.familyName);
}
publicstaticvoidmain(字符串[]args){
Map persons=newhasmap();
//定义一个新人
人爸爸=新人();
dady.name=“Tim”;
dady.familyName=“史密斯”;
dady.salary=新的大十进制(“1234.56”);
//储存
人。把(“爸爸”,爸爸);
//找回它
Person misterX=persons.get(“爸爸”);
//使用它
System.out.println(“MisterX:+MisterX.familyName”);
}
您不必这样做,但我强烈建议您定义一个类。它可能是这样的:

package com.yourcompany.projectname;

public class Person {
    public String name;
    public String familyName;
    public BigDecimal salary;
}
然后将其存储在HashMap中:

public static void main(String[] args) {
    Map <String, Person> persons = new HasMap <>();

    // define a new person
    Person dady = new Person();
    dady.name = "Tim";
    dady.familyName = "Smith";
    dady.salary = new BigDecimal("1234.56");

    // store it
    persons.put("dady", dady);

    // retrieve it
    Person misterX = persons.get("dady");

    // use it
    System.out.println("MisterX: "+misterX.familyName);
}
publicstaticvoidmain(字符串[]args){
Map persons=newhasmap();
//定义一个新人
人爸爸=新人();
dady.name=“Tim”;
dady.familyName=“史密斯”;
dady.salary=新的大十进制(“1234.56”);
//储存
人。把(“爸爸”,爸爸);
//找回它
Person misterX=persons.get(“爸爸”);
//使用它
System.out.println(“MisterX:+MisterX.familyName”);
}
您不必这样做,但我强烈建议您定义一个类。它可能是这样的:

package com.yourcompany.projectname;

public class Person {
    public String name;
    public String familyName;
    public BigDecimal salary;
}
然后将其存储在HashMap中:

public static void main(String[] args) {
    Map <String, Person> persons = new HasMap <>();

    // define a new person
    Person dady = new Person();
    dady.name = "Tim";
    dady.familyName = "Smith";
    dady.salary = new BigDecimal("1234.56");

    // store it
    persons.put("dady", dady);

    // retrieve it
    Person misterX = persons.get("dady");

    // use it
    System.out.println("MisterX: "+misterX.familyName);
}
publicstaticvoidmain(字符串[]args){
Map persons=newhasmap();
//定义一个新人
人爸爸=新人();
dady.name=“Tim”;
dady.familyName=“史密斯”;
dady.salary=新的大十进制(“1234.56”);
//储存
人。把(“爸爸”,爸爸);
//找回它
Person misterX=persons.get(“爸爸”);
//使用它
System.out.println(“MisterX:+MisterX.familyName”);
}

我不太清楚你在找什么。与单个列表中的一个键相关的不同参数?Java中的列表没有“键”,它们只有没有附加值的元素。你的意思是a吗?这有点像在一个结构中保存一个包含唯一值的表的数据,比如一个列表。我不太清楚你在找什么。与单个列表中的一个键相关的不同参数?Java中的列表没有“键”,它们只有没有附加值的元素