I don't know what meaning you put behind "Ordened"
and "UnOrdened"
, and if it makes sens to have a class implementing both, but anyway, here is an example on how you can use multiple implementations of interfaces, trying to keep up with your stuff.
I suppose by ordered the Java meaning as explained here, which is not sorted.
Let's say an Ordered
interface gives the possibility to access some object (of type T
) by index and to search or find for a given object:
public interface Ordered<T> {
T getNth(int i);
T find(T o);
}
Let's say an UnOrdered
interface provides only the way to find an object:
public interface UnOrdered<T> {
T find(T o);
}
Now we can defined a class that implements both interfaces:
public class MyList<T> implements Ordered<T>, UnOrdered<T> {
List<T> theList;
public MyList(T...a) {
theList = Arrays.asList(a);
}
// The list of objects are neither sorted nor hashed.
// Only way to find an object is to iterate through the list
public T find(T o) {
for (T e : theList) {
if (e.equals(o)) {
return e;
}
}
return null;
}
public T getNth(int i) {
return theList.get(i);
}
}
public static void main(String[] args) {
MyList<Integer> mylist = new MyList<>(2,8,6,1,7,3,5,9,10,4);
System.out.println(mylist.getNth(3));
// => print 1
System.out.println(mylist.find(3));
// => print 3
System.out.println(mylist.find(42));
// => print null
}