I am tired of the below issue. please help?
I have a below interface
/**
* <T> Type of Timestamp. For ex: Date, long, Calendar etc
*
*/
public interface TimeStamp<T> extends Comparable<TimeStamp<T>>
{
/**
* Returns the timestamp.
* @return
*/
public T getTimeStamp();
}
And in my code I have 2 lists:
List<FileTimeStamp> f = new ArrayList<FileTimeStamp>();
List<DefaultTimeStamp> t = new ArrayList<DefaultTimeStamp>();
I want to create a method that takes the above 2 lists containing TimeStamps and then merge them.
Ex:
void merge(List<TimeStamp> l1, List<TimeStamp> l2)
{
l1.addAll(l2);
Collections.sort(l1);
}
But the above generates compile time warnings.
void merge(List l1, List l2)
missing type arguments for generic class TimeStamp
where T is a type-variable:
T extends Object declared in interface TimeStamp
How should I define the above method without any compile time warnings? What should be my void merge(List<TimeStamp<????>> l1, List<TimeStamp<????>> l2) ???? in left be?
If you care about type safety you must have equal types.
Solution for your question is:
How this will work:
We well be able to perform:
We will not be able to:
The key to success is that
rightlist item must be assignalbe to type of items inleftlist.After merge you wan to sort, this is another part of task. To sort List the class that is stored in list should implements interface
ComparableSo we end up with structure like this:
To merge and sort we need to limie the
Tto type that implementsComparable<T>at least onTAnd this look like this:
So we can say that you have what you want.
But i would separate the operations and use Iterables.concat(Iterable …), in Combine multiple Collections into a single logical Collection? is describe how it works; for merging and after that i would sort it.