【问题标题】:Search Partially between two maps在两张地图之间进行部分搜索
【发布时间】:2018-02-22 05:03:17
【问题描述】:

我有两个 Hashmaps Map1<List,List>Map2<List,List> ,键列表包含两个映射的两个元素 - 字符串 name and time

我想遍历 Map1 并检查 map1 的键是否包含在映射 2 中,但由于我的键有 2 个元素,我只想根据时间进行比较。

for (Entry<List, List> entry : Map1.entrySet()) {
        if("Map2.contains(entry.getKey().get(1))"){
        }
}

example: Map1 ,Key1: student1, 14:30:20(time of entering class) Map1 ,Key2: student1, 14:30:12 Map2 ,Key1: student2, 14:30:13 Map2 ,Key2: student2, 14:30:20

注意:这只是一个例子:我想看看 student1 和 student2 是否同时进入班级。 在这个例子中,第一行和最后一行匹配应该返回 true

这可能吗?如果是,我的if条件应该是什么?

【问题讨论】:

  • 您可能需要再次考虑您的keyList 作为 key 不是很好的选择。
  • 除非您提供更多信息,例如地图类型和它可能使用的任何比较器,否则您基本上必须进行线性搜索。
  • 是的,潜在的可变键是一个糟糕的主意
  • 这并不能解释任何事情,只是这是一个 xy 问题。请提出一个正确的问题,解释您实际想要达到的目标。请务必包含输入示例和包含代码的当前尝试描述
  • 这是一个完美的例子,说明为什么显示实际代码是个好主意。

标签: java hashmap


【解决方案1】:

由于不清楚你真正想要什么,这里有一些建议:

  1. 为“学生”创建一个包含所有数据字段的类(注意:您可以将LocalTime 用于time

    public class Student {
        private LocalTime time;
        private String name;
    
        public Student(LocalTime time, String name) {
            this.time = time;
            this.name = name;
        }
    
        public LocalTime getTime() {
            return time;
        }
    
        public String getName() {
            return name;
        }
    
        //TODO add the other fields
    }
    
  2. 当您不仅需要在时间访问学生时,只需将它们存储在List&lt;Student&gt; 中。 (因为你写了“我有一个我正在迭代的文件,它没有任何独特的元素”

  3. 如果您需要对对象进行分组,您可以使用 Java8-Stream-API。例如:

    public static void main(String[] args) {
        List<Student> students = Arrays.asList(
            new Student(LocalTime.parse("14:30:12"),"s1"),
            new Student(LocalTime.parse("14:30:12"),"s2"),
            new Student(LocalTime.parse("14:30:13"),"s3"));
    
        Map<LocalTime,List<Student>> groupByTime = students.stream()
            .collect(Collectors.groupingBy(Student::getTime));
    
        // print the grouped students to System.out
        for(Entry<LocalTime,List<Student>> entry : groupByTime.entrySet()) {
            System.out.println(entry.getKey());
            for(Student student : entry.getValue()) {
                System.out.println("\t"+student.getName());
            }
        }
    }
    
  4. 当你有两个没有任何重复条目的列表时,你可以简单地将它们连接起来然后分组:

    public static void main(String[] args) {
        List<Student> students1 = Arrays.asList(
                new Student(LocalTime.parse("14:30:20"),"s1"),
                new Student(LocalTime.parse("14:30:12"),"s1"));
    
        List<Student> students2 = Arrays.asList(
                new Student(LocalTime.parse("14:30:13"),"s2"),
                new Student(LocalTime.parse("14:30:20"),"s2"));
    
        Map<LocalTime,List<Student>> groupByTime = Stream.concat(students1.stream(), students2.stream())
            .collect(Collectors.groupingBy(Student::getTime));
    
    
        for(Entry<LocalTime,List<Student>> entry : groupByTime.entrySet()) {
            System.out.println(entry.getKey());
            for(Student student : entry.getValue()) {
                System.out.println("\t"+student.getName());
            }
        }
    }
    

【讨论】:

    【解决方案2】:

    这将是你的条件陈述

    if(map2.containsKey(entry.getKey().get(1)){
    }
    

    你的情况不对,所以我更正了。

    【讨论】:

    • @chetnarustagi 双引号通常被赋予字符串,据我所知,你如何使用 if 使用这个语句?它没有意义。
    • 这应该是评论而不是答案,您是否尝试过您的答案?你没有仔细阅读问题。
    • @CalculusProgrammer 这将不起作用,因为它将 map2 完整键与 keyList 1 的第一个元素进行比较,这将始终返回 false
    • @ChaojunZhong 那我应该删除它吗?
    • @CalculusProgrammer 你可以更新你的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多