简体   繁体   中英

Iterating hashmap of arraylist of two different Class types

I have to create an hashmap of arraylist but arraylist can be of type StudentRecord or TeacherRecord(which are two different classes) I have added both the records but I am not able to iterate through the contents of hashmap using display method.can anybody help me to define the display function?

public class StudentRecord 
    {
        String first_Name;
        String last_Name;
        String [] courses_Registered;
        String status;
        Date date;

    StudentRecord(String f_name,String l_name,String [] courses,String status,Date dat )
    {
        this.first_Name=f_name;
        this.last_Name=l_name;
        this.courses_Registered=courses;
        this.status=status;
        this.date=dat;
    }

}

public class TeacherRecord 
{
    String first_Name;
    String last_Name;
    String address;
    String phone;
    String specilization;
    String location;

    TeacherRecord(String f_name,String l_name,String addr,String number,String spec,String loc )
    {
        this.first_Name=f_name;
        this.last_Name=l_name;
        this.address=addr;
        this.phone=number;
        this.location=loc;
    }
}

public class Montreal_server
{

    String record_id;
    ArrayList<StudentRecord> student_arraylist=new ArrayList<StudentRecord>();
    ArrayList<TeacherRecord>teacher_arraylist=new ArrayList<TeacherRecord>();

    HashMap<String,ArrayList> map=new HashMap<String,ArrayList>();

    void createTRecord(String f_name,String l_name,String addr,String number,String spec,String loc)
    {
        TeacherRecord t=new TeacherRecord(f_name,l_name,addr,number,spec,loc);
        teacher_arraylist.add(t);
        map.put("x",teacher_arraylist);


    }

    void createSRecord(String f_name,String l_name,String [] courses,String status,Date dat)
    {
        StudentRecord s=new StudentRecord(f_name,l_name,courses,status,dat);
        student_arraylist.add(s);
        map.put("s",student_arraylist);




    }

    void display()
    {
        System.out.println("inside display");
        for(Entry<String, ArrayList> en : map.entrySet())
        {
            java.util.Iterator<StudentRecord> itr = ((List<StudentRecord>)student_arraylist).iterator();



            while(itr.hasNext())
            {
                Object c=(Object)itr.next();

                System.out.println(c.first_Name+" "+c.last_Name+" "+c.status+" "+c.date+" "+c.courses_Registered);
            }
        }

    }

}

A simple solution is to override toString() on each of your classes:

public class StudentRecord {
    // fields, constructor etc.

    @Override
    public String toString() {
        return first_Name+" "+last_Name+" "+status+" "+date+" "+Arrays.toString(courses_Registered);  
    }
}

Create a similar method for TeacherRecord . Now, in your loop, simply call System.out.println(c); , which will implicitly print the result of your toString() implementation.

Additionally, a lot of the confusion is a result of trying to coerce lists containing unrelated types into a single map. The simplest solution is to just print each list separately:

void display() {
    student_arraylist.forEach(System.out::println);
    teacher_arraylist.forEach(System.out::println);
}

Have both classes implement a common interface, eg

public interface PersonRecord {
String firstName();
String lastName();
...
}

and

public class StudentRecord implements PersonRecord
    { ... }

which allows

List<PersonRecord> students = new List<>();
students.add(new StudentRecord());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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