【问题标题】:Arraylist to Hashmap with multiple values and unique key per row?Arraylist to Hashmap,每行具有多个值和唯一键?
【发布时间】:2021-09-10 05:53:04
【问题描述】:

我有一个包含数据的数组列表:

NAME   LASTNAME   CREDIT_CARD  COUNTRY
name1  lastname1   card1       country1
name2  lastname2   card2       country2
name3  lastname3   card3       country3

上面的arraylist是由我目前使用的这段代码创建的:

public ArrayList reader() throws IOException {
    while (true) {
        System.out.println("Give me the Data file here : ");
        String path = "";
        Scanner sc = new Scanner(System.in);
        path = sc.nextLine();
        path = path.toLowerCase(Locale.ROOT);
        if (path.contains(".txt") && !path.isEmpty() ) {
            FileReader fr = new FileReader(path);
            BufferedReader br = new BufferedReader(fr);
            String str;
            String[] wordsArray;
            while ((str = br.readLine()) != null) {
                wordsArray = str.split("\\t");
                for (String each : wordsArray) {
                    if (!"".equals(each)) {
                        words.add(each);
                    }
                }
            }             
            System.out.println(words);
            br.close();
            return words;
        } else {
            System.out.println("Wrong type,try again");
        }
    }
}

我尝试将上面的列表作为一个 hasmap,其中每一行都有一个唯一的键和用户的值,例如: 键 1 = name1,lastname1,card1,country1 键 2 = name2,lastname2,card2,country2 有什么办法吗?

【问题讨论】:

  • 1.代码无法编译,因为 words 未定义。 2. 不清楚预期地图的键源应该是什么。 3.不清楚map的值应该是什么:字符串列表或一些Row对象。

标签: java arraylist hashmap


【解决方案1】:

对不起。

你有没有试过编译它?

什么是“词”?

除了返回类型之外,您的方法中肯定没有 ArrayList。

创建一个保存您的地址数据的类。

创建一个该类型的 ArrayList。

请参阅 Collectors.toMap() 的 JavaDoc,了解如何使用 Java 功能接口从列表创建地图。

【讨论】:

    【解决方案2】:

    与其使用 HashMap,不如创建一个自定义类,将 String name, lastName, creditCard, country 作为变量。该类的每个实例都将像一组数据,它们可以加载到 ArrayList 中或插入到 SQLite 等数据库中。我不确定您为什么需要使用 HashMap,您的选择有什么特别的原因吗?

    You might find this YouTube video useful

    【讨论】:

      【解决方案3】:

      您不应该使用地图来执行此操作。将值放在一个类中,然后使用 Map 来保存该类的每个实例会更好。

      • 从控制台读取输入,每行一行。
      • 使用\\s+ 将字符串拆分为一个或多个空格字符。
      • 连接名字和姓氏作为地图的键。
      • 将字段传递给构造函数以创建Person 类的实例
      
      
      Map<String, Person> map = new HashMap<>();
      Scanner scanner = new Scanner(System.in);
      System.out.print("Enter number of rows: ");
      int nRows = scanner.nextInt();
      System.out.println("Enter + " nRows + " of data.");
      for (int i = 0; i < nRows; i++) {
          String[] fields = row.split("\\s+");
          map.put(fields[0] + fields[1], new Person(fields[0],
                  fields[1], fields[2], fields[3]));
      }
      
      map.entrySet().forEach(System.out::println);
      

      打印

      name1lastname1=[name1 lastname1, card1, country1]
      name3lastname3=[name3 lastname3, card3, country3]
      name2lastname2=[name2 lastname2, card2, country2]
      

      这是类定义。这里还有其他可能性,比如更改toString() 返回并添加setters

      class Person {
          private String firstName;
          private String lastName;
          private String card;
          private String country;
          
          public Person(String firstName, String lastName, String card,
                  String country) {
              this.firstName = firstName;
              this.lastName = lastName;
              this.card = card;
              this.country = country;
          }
          
          public String getFirstName() {
              return firstName;
          }
          
          public String getLastName() {
              return lastName;
          }
          
          public String getCard() {
              return card;
          }
          
          public String getCountry() {
              return country;
          }
          
          @Override
          public String toString() {
              return "[" + firstName + " " + lastName + ", " + card + ", "
                      + country + "]";
          }
      }
      

      您也可以使用streams 创建地图,但在这种情况下并没有真正的优势。我建议您在尝试使用流之前先熟悉 Java 的更多基本方面。

      【讨论】:

      • 感谢您的回答!我想我需要地图,因为我需要从 c onsole 读取一个 txt 文件!我不能硬编码:(
      • 你误解了我的回答。您可以从控制台读取每一行,然后应用拆分。我修改了答案以向您展示。而且地图与从控制台获取数据无关。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-05-17
      • 2018-09-16
      • 1970-01-01
      • 1970-01-01
      • 2011-06-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多