Reputation: 1180
need help.Have create arraylist and then add multiple object(in my case,add 2 type object inside arraylist).Then sort each object inside arraylist .Able to sort but I think my coding is too long.Is their any way or method to make it simple and effective.Hope you guys can help.That all,hope you can help.Thank you.
public class Sample {
static ArrayList<Object> object = new ArrayList<>();
public static void main(String[] args){
//initialize object
User user1 = new User("user1","1");
User user2 = new User("user1","3");
Staff staf1 = new Staff("staf1","2");
Staff staf2 = new Staff("staf2","4");
//add object to arraylist
object.add(user1);
object.add(user2);
object.add(staf1);
object.add(staf2);
//sort
Collections.sort(object,new Comparator<Object>() {
Object one,two;
@Override
public int compare(Object o1, Object o2) {
User user1,user2;
Staff staf1,staf2;
///identify ol class
if(o1 instanceof User){
user1 = (User) o1;
one = (Object) user1;
}else if(o1 instanceof Staff){
staf1 = (Staff) o1;
one = (Object) staf1;
}
//identify o2 class
if(o2 instanceof User){
user2 = (User) o2;
two = (Object) user2;
}else if(o2 instanceof Staff){
staf2 = (Staff) o2;
two = (Object) staf2;
}
//identify each object class
//then compare value
if(one instanceof User && two instanceof User){
//execute process
User userOne = (User) one;
User userTwo = (User) two;
return userOne.getRegister().compareTo(userTwo.getRegister());
}else if(one instanceof Staff && two instanceof Staff){
//execute process
Staff stafOne = (Staff) one;
Staff stafTwo = (Staff) two;
return stafOne.getRegister().compareTo(stafTwo.getRegister());
}else if(one instanceof User && two instanceof Staff){
//execute process
User userOne = (User) one;
Staff stafTwo = (Staff) two;
return userOne.getRegister().compareTo(stafTwo.getRegister());
}else if(one instanceof Staff && two instanceof User){
//execute process
Staff stafOne = (Staff) one;
User userTwo = (User) two;
return stafOne.getRegister().compareTo(userTwo.getRegister());
}else{
return 0;
}
}
});
//display item
for(Object object : object){
if(object instanceof User){
User user = (User)object;
//display data
System.out.println(user.getRegister());
}else if(object instanceof Staff){
Staff staf = (Staff) object;
//display data
System.out.println(staf.getRegister());
}
}
}
}
Upvotes: 0
Views: 71
Reputation: 6946
As your objects have the same fields and methods I am recomend you to use inheritance
int this situation. First you have to exclude one abstract class which be a parent of yours two classes:
public abstract class Parent implements Comparable<Parent> {
abstract Integer getRegister();
@Override
public int compareTo(final Parent parent) {
return this.getRegister().compareTo(parent.getRegister());
}
}
Then you have to make, that your classes User
and Staff
will extends Person
class. Then, you can create list: List<Parent> list
and sort it by its own comparator. So assuming there are matchinbg constructors available, User extends Parent
and Staff extends Parent
:
final List<Parent> list = new ArrayList<>();
list.add(new User("user1","1"));
list.add(new User("user1","3"));
list.add(new Staff("staf1","2"));
list.add(new Staff("staf2","4"));
Collections.sort(list);
EDIT (as OP don't want to use inheritance):
Instead of checking few times, do it only once and get register field in moment you know about type.
@Override
public int compare(Object o1, Object o2) {
Integer register1 = null;
Integer register2 = null;
if(o1 instanceof User) {
register1 = ((User)o1).getRegister();
} else if(o1 instanceof Staff) {
register1 = ((Staff)o1).getRegister();
}
if(o2 instanceof User) {
register2 = ((User)o2).getRegister();
} else if(o1 instanceof Staff) {
register2 = ((Staff)o2).getRegister();
}
if(register1 != null && register2 != null) {
return register1.compareTo(register2);
}
return 0;
}
Upvotes: 1