繁体   English   中英

在java中映射数组列表

[英]Mapping Array List in java

我试图在 Java 中运行此代码:

List<Map<String , String>> studentList = new ArrayList<>();
Map<String , String> studentRecord = new HashMap();

//Record for first Student
studentRecord.put("Name","aaa");
studentRecord.put("Age","22");
studentRecord.put("Sex","m");
studentList.add(studentRecord);

//Record for second Student
studentRecord.put("Name","bbb");
studentRecord.put("Age","44");
studentRecord.put("Sex","f");
studentList.add(studentRecord);

输出是:

[{Sex=f,Age=44,Name=bbb},{Sex=f,Age=44,Name=bbb}]

代替

 [{Sex=m,Age=22,Name=aaa},{Sex=f,Age=44,Name=bbb}]
  • 一种。 我究竟做错了什么?
  • 假设输出是正确的。 如何仅打印 StudentList[0] 的“名称”值,表示“aaa”?

谢谢

您有一个地图实例:

Map<String , String> studentRecord = new HashMap();

您将其添加到列表中两次。

当您为第二个学生调用studentRecord.put ,您是在一个且唯一的 map instance上调用它,从而覆盖第一个学生的键。

最好的解决方案是:不要使用地图来表示数据对象 使用一个类。 这就是它们的设计目的。

enum Sex
{
    MALE, FEMALE
}

class Student
{
    private final String name;
    private final int age;
    private final Sex sex;

    Student(String name, int age, Sex sex) {
        this.name = name;
        this.age = age;
        this.sex = sex;
    }

    // getters...
}

List<Student> students = new ArrayList<>();
students.add(new Student("aaa", 222, Sex.MALE));
students.add(new Student("bbb", 44, Sex.FEMALE));

如果你只想要快速而肮脏的解决方案,你可以使用这个:

//Record for second Student
studentRecord = new HashMap<>(); // I added this line

studentRecord.put("Name","bbb");
studentRecord.put("Age","44");
studentRecord.put("Sex","f");
studentList.add(studentRecord);

它为第二个学生创建了第二个HashMap ,从而确保第一个映射中的键不会被覆盖。

当您在 ArrayList 上调用“.add()”时 - 您将 HashMap引用放入其中:

    List<Map<String , String>> studentList = new ArrayList<>();
    Map<String , String> studentRecord = new HashMap();

    studentRecord.put("Name","aaa");
    studentRecord.put("Age","222");
    studentRecord.put("Sex","m");
    studentList.add(studentRecord);
    studentList.forEach(map -> System.out.println(map.hashCode()));
    System.out.println("--------");


    studentRecord.put("Name","bbb");
    studentRecord.put("Age","44");
    studentRecord.put("Sex","f");
    studentList.add(studentRecord);
    studentList.forEach(map -> System.out.println(map.hashCode()));
}

那么你改变 HashMap 的状态并重新放置它。 输出将是:

2661442
--------
2612488
2612488

因此,您只是使用 1 个对象,而不是使用 2 个不同的对象。

看起来您需要一个“Student”类来使用它,它会更“Java 风格”:

public class Main {

        public static void main(String[] args) {

            List<Student> students = new ArrayList<>();
            students.add(new Student("aaa", 222, "m"));
            students.add(new Student("bbb", 44, "f"));

            System.out.println(students);
    }
}

class Student {
    String name;
    Integer age;
    String sex;

    public Student(String name, Integer age, String sex) {
        this.name = name;
        this.age = age;
        this.sex = sex;
    }

    @Override
    public String toString() {
        return "Student{" +
            "name='" + name + '\'' +
            ", age=" + age +
            ", sex='" + sex + '\'' +
            '}';
    }
}

您只创建了一个 Map 实例。 因此,失去了记录。 请创建两个地图实例,如下所示:

List<Map<String, String>> studentList = new ArrayList<>();

//First instance
Map<String, String> studentRecord1 = new HashMap();

//Record for first Student
studentRecord1.put("Name", "aaa");
studentRecord1.put("Age", "22");
studentRecord1.put("Sex", "m");
studentList.add(studentRecord1);

//Second instance
Map<String, String> studentRecord2 = new HashMap();

//Record for second Student
studentRecord2.put("Name", "bbb");
studentRecord2.put("Age", "44");
studentRecord2.put("Sex", "f");
studentList.add(studentRecord2);

此代码返回一个包含所有您需要的数组:

package Test;
import java.awt.event.KeyEvent;                                                               
import java.lang.reflect.Field;                                                               
import java.lang.reflect.Modifier;                                                            
import java.util.*;                                                                           
                                                                                              
public class Test {                 
    public static void main(String[] a) {   
        ArrayList<Integer> ints = new ArrayList<Integer>();
        ArrayList<String> strs = new ArrayList<String>();
        
        for(int i = 0; i < 1000000; ++i) {                                                    
            String text = java.awt.event.KeyEvent.getKeyText(i);                              
            if(!text.contains("Unknown keyCode: ")) {                                         
                strs.add("\""+text+"\"");
                ints.add(i);
            }                                                                                 
        }                                                                                     
        
        System.out.println(ints);
        System.out.println(strs);
    }                                                                                         
}

输出:

[3, 8, 9, 10, 12, 16, 17, 18, 19, 20, 21, 24, 25, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 59, 61, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 150, 151, 152, 153, 154, 155, 156, 157, 160, 161, 162, 192, 222, 224, 225, 226, 227, 240, 241, 242, 243, 244, 245, 256, 257, 258, 259, 260, 261, 262, 263, 512, 513, 514, 515, 516, 517, 518, 519, 520, 521, 522, 523, 524, 525, 61440, 61441, 61442, 61443, 61444, 61445, 61446, 61447, 61448, 61449, 61450, 61451, 65312, 65368, 65406, 65480, 65481, 65482, 65483, 65485, 65487, 65488, 65489]
["Cancel", "Backspace", "Tab", "Enter", "Clear", "Shift", "Ctrl", "Alt", "Pause", "Caps Lock", "Kana", "Final", "Kanji", "Escape", "Convert", "No Convert", "Accept", "Mode Change", "Space", "Page Up", "Page Down", "End", "Home", "Left", "Up", "Right", "Down", "Comma", "Minus", "Period", "Slash", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "Semicolon", "Equals", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "Open Bracket", "Back Slash", "Close Bracket", "NumPad-0", "NumPad-1", "NumPad-2", "NumPad-3", "NumPad-4", "NumPad-5", "NumPad-6", "NumPad-7", "NumPad-8", "NumPad-9", "NumPad *", "NumPad +", "NumPad ,", "NumPad -", "NumPad .", "NumPad /", "F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "F10", "F11", "F12", "Delete", "Dead Grave", "Dead Acute", "Dead Circumflex", "Dead Tilde", "Dead Macron", "Dead Breve", "Dead Above Dot", "Dead Diaeresis", "Dead Above Ring", "Dead Double Acute", "Dead Caron", "Dead Cedilla", "Dead Ogonek", "Dead Iota", "Dead Voiced Sound", "Dead Semivoiced Sound", "Num Lock", "Scroll Lock", "Ampersand", "Asterisk", "Double Quote", "Less", "Print Screen", "Insert", "Help", "Meta", "Greater", "Left Brace", "Right Brace", "Back Quote", "Quote", "Up", "Down", "Left", "Right", "Alphanumeric", "Katakana", "Hiragana", "Full-Width", "Half-Width", "Roman Characters", "All Candidates", "Previous Candidate", "Code Input", "Japanese Katakana", "Japanese Hiragana", "Japanese Roman", "Kana Lock", "Input Method On/Off", "At", "Colon", "Circumflex", "Dollar", "Euro", "Exclamation Mark", "Inverted Exclamation Mark", "Left Parenthesis", "Number Sign", "Plus", "Right Parenthesis", "Underscore", "Windows", "Context Menu", "F13", "F14", "F15", "F16", "F17", "F18", "F19", "F20", "F21", "F22", "F23", "F24", "Compose", "Begin", "Alt Graph", "Stop", "Again", "Props", "Undo", "Copy", "Paste", "Find", "Cut"]

暂无
暂无

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

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