0

I have Two Diffrent ArrayList lets say:

ArrayList<model_tasks_pending> Tasks = new ArrayList<model_tasks_pending>();
ArrayList<model_task> data = new ArrayList<model_task>();

Now i want combine this two array list to one and access each data in it. This is what i have tried,

 private List<Object> Data = new ArrayList<>();
 Data.addAll(Tasks);
 Data.addAll(data);

This able to get all data into new List. But how can i access data in it? Lets say i want access get_creat_on() in Tasks

Data.get(0).get_creat_on()

but showing error cannot resolve get_creat_on()

Also after combine both Arraylist I want sort them based on time. I have _time object in both Arraylist and it return time like this format 08:33 AM. Any guild on how to achieve this ?

Modal Class:

model_task.java

public class model_task {
    int _id;
    String _title;
    String _body;
    String _time;

    public String get_time() {
            return _time;
        }
    public int get_id() {
        return _id;
    }
    public String get_title() {
        return _title;
    } 
    public String get_body() {
        return _body;
    }
}

model_tasks_pending.java

 public class model_tasks_pending{
        int _id;
        String _title;
        String _creat_on;
        String _time;

    public String get_time() {
            return _time;
        }
    public String get_creat_on() {
        return _creat_on;
    }
    public int get_id() {
        return _id;
    }

    public String get_title() {
        return _title;
    }

    public String get_body() {
        return _body;
    }
}
8
  • And the reason you think that including the error message is not important is... Commented Aug 5, 2017 at 8:46
  • sorry Edited question Commented Aug 5, 2017 at 8:50
  • Ah. You really shouldn't be combining list types like that. Why can't they remain separate? Commented Aug 5, 2017 at 8:51
  • can you possibly give an example then? i have to create new modal class with all data in it? Commented Aug 5, 2017 at 8:52
  • What about a class with two different lists in it? Commented Aug 5, 2017 at 8:54

1 Answer 1

3

First : please follow Java naming convention. Classes should start with an uppercase and variable with a lowercase.
Besides _ is allowed but not advised.


About your question.

Storing instances of specific classes in a List of Object defeats the purposes of using generics.

If you want store and sort both instances of model_tasks_pending and model_tasks, you should reason in terms of interface.
Introduce a common interface with all required methods and make these classes implement it.

In this way you could replace :

 private List<Object> Data = new ArrayList<>();

by :

 private List<MyModelTask> Data = new ArrayList<>();

where MyModelTask is the common interface.

In this way you could access to any method defined in MyModelTask and you could sort MyModelTask by defining a Comparator and more specifically Comparator<MyModelTask> if you want to sort instances of both classes.

Sign up to request clarification or add additional context in comments.

6 Comments

If I understand your answer correctly i have to create a interface class MyModelTask and implement both class
You name it as you want but yes the idea is there. About get_creat_on(), that is only defined in model_tasks_pending, you have several options to implement it in the class that doesn't have it actually : returning null, a default value or even adding a method in the interface to identify the type of instances, etc... But as you don't give details about how it should be valued, it is hard to propose one solution rather than another.
Both class have _time which will return time in specific format. i want sort newly created list based on that time.
So for the sort you should have no problem as both classes have a have _time field. You are welcome :)
can you possibly give an example for that?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.