Chetna rustagi
Chetna rustagi

Reputation: 481

Search Partially between two maps

I have two Hashmaps Map1<List,List> and Map2<List,List> , Key List contains two elements for both the maps - String name and time.

I want to iterate through Map1 and check if key of map1 is contained in map 2, but since my key has 2 elements ,I want to compare only on basis of time.

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

Note: It is just an example: I want to see whether student1 and student2 entered the class at same time. In this example 1st and last rows match should return true

Is this possible ? if yes,What should be my if condition?

Upvotes: 1

Views: 86

Answers (2)

user8097737
user8097737

Reputation:

Since it's unclear what you really want, here some suggestion:

  1. Create a class for the "students" with all data-fields (Note: You could use LocalTime for 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. When you need to access the students not only over the the time, then just store them in a List<Student>. (Since you wrote "I have a file on which I am iterating, It doesnt has any unique element")

  3. If you need to group the objects you can use the Java8-Stream-API. For example:

    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. When you have two lists without any duplicated entry you can simple concatenated them and then group:

    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());
            }
        }
    }
    

Upvotes: 1

user9181740
user9181740

Reputation:

This will be your conditional statement

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

Your condition was wrong so I corrected it.

Upvotes: 0

Related Questions