【问题标题】:How access map values stored within objects [duplicate]如何访问存储在对象中的地图值[重复]
【发布时间】:2020-04-19 13:54:26
【问题描述】:

我正在尝试创建一个哈希映射,但我似乎找不到在映射中检索属性值的方法。

我通过使用子类Student 中的实例变量来创建新的键值对。

如何检索从子类创建时共享相同属性值“A”的所有条目?

Map<String, Student> students = new HashMap<>();
students.put("0001", new Student("Mike Myers","60 Hey", 'A'));
students.put("0002", new Student("Victor Hughes","21 ddd", 'F'));
students.put("0003", new Student("Elisabeth Carter","56 fff", 'A'));

如果我这样做了

for (Map.Entry<String, Student> entry : students.entrySet())
{
   System.out.println(entry.getValue());
}

然后我得到所有属性(名称、地址、类别)的值。所以我想知道如何才能获得只有“A”类别的键?

【问题讨论】:

  • Student有检索类别的方法吗?
  • 是的,有一个 getCategory 可以在 Student 子类下检索对象的类别
  • Category 的数据类型是什么?

标签: java hashmap


【解决方案1】:

您需要检查该属性的值,如下所示:

for (Map.Entry<String, Student> entry : students.entrySet()) {
    if(entry.getValue().getCategory() == 'A') {
        System.out.println(entry.getValue());
    }
}

如果要创建相关键的Set,可以如下操作:

Set<String> keySet = new HashSet<String>();
for (Map.Entry<String, Student> entry : students.entrySet()) {
    if (entry.getValue().getCategory() == 'A') {
        keySet.add(entry.getKey());
    }
}
System.out.println(keySet);

【讨论】:

  • 使用== 比较Strings 是个坏建议。这是不正确的,因为“==”只检查两个字符串的引用相等性,这意味着它们是否引用相同的对象,但在这种情况下,如果它是否相同 category,则不检查。
  • @smithnblack - 这不是String。请再看一遍,是char
【解决方案2】:

对于 Java 8,这是一个不错的解决方案

students.values()
    .stream()
    .filter(student -> student.getCategory() == 'A')
    .collect(Collectors.toList());

如果您想打印类别而不是学生列表,您可以添加 mapforeach 中介操作

students.values()
    .stream()
    .map(Student::getCategory)
    .filter(cat -> cat == 'A')
    .foreach(System.out::println);

【讨论】:

  • 使用== 比较Strings 是个坏建议。这是不正确的,因为“==”只检查两个字符串的引用相等性,这意味着它们是否引用相同的对象,但在这种情况下,如果它是否相同 category,则不。
  • @smithnblack 我们不知道category 是字符串还是字符。如果是 char,因为它是原始的,它不会有 equals() 方法。由于 OP 使用单引号,我认为它是一个字符
  • 你是对的,修正了那个不赞成票 ;)。对不起!
【解决方案3】:

试试下面:

System.out.println(Students.getValues().stream().filter(student -> "A".equalsIgnoreCase(student.getCategory())).collect(Collectors.toList()));

希望对你有帮助!!!

【讨论】:

    【解决方案4】:

    这可以通过以下方式实现:

    List<String> keys = students.entrySet().stream() //
      .filter(e -> e.getValue().getCategory().equals("A")) //
      .map(e -> e.getKey()) //
      .collect(Collectors.toList());
    

    【讨论】:

      【解决方案5】:

      假设您的CategoryString,您可以像这样在for loop 中执行此操作:

      for (Map.Entry<String, Student> entry : students.entrySet())
      {
         if (entry.getCategory().equals("A")) {
           // do something with that entry here
           System.out.println(entry.getValue());
         }
      }
      

      你需要你的getter getCategory() in Student Class 才能工作。

      在 Java 8 中,您也可以像这样使用流:

      List<Student> result = student.entrySet().stream()
          .filter(student -> "A".equals(student.getCategory()))
          .map(x->x.getValue())
          .collect(Collectors.toList());
      

      【讨论】:

        猜你喜欢
        • 2021-03-16
        • 2019-08-13
        • 2018-03-26
        • 2018-06-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-02-20
        • 2016-09-16
        相关资源
        最近更新 更多