一、可迭代
定义一个Class MyClass1,若需要使得MyClass1 具有可迭代的特性,则其需实现Iterator或是Iterable接口。
Iterator 和Iterable的区别
参见 http://blog.csdn.net/lipengcn/article/details/51700153
public interface Iterable<T> {
/**
* Returns an iterator over a set of elements of type T.
* @return an Iterator.
*/
Iterator<T> iterator();
}
public interface Iterator<E> {
boolean hasNext();
E next();
void remove();
}
二、可比较
定义一个Class MyClass2,若需要使得MyClass2 具有可比较的特性,则其需实现Comparator 或是Comparable 接口。
Comparator 和 Comparable 的区别:Comparator是作为MyClass2类外部比较器进行实现,而Comparable是MyClass2类内部进行实现。
参见 http://blog.csdn.net/u012289441/article/details/45154401?ref=myread
public interface Comparator<T> {
/* @return a negative integer, zero, or a positive integer as the
* first argument is less than, equal to, or greater than the
* second.
*/
int compare(T o1, T O2);
/*
* @return <code>true</code> only if the specified object is also
* a comparator and it imposes the same ordering as this
* comparator.
*/
bool equals(Object obj);
}
public interface Comparable {
/*
*@return a negative integer, zero, or a positive integer as this object
* is less than, equal to, or greater than the specified object.
*/
public int compareTo(T o);
}
定义一个Class MyClass1,若需要使得MyClass1 具有可迭代的特性,则其需实现Iterator或是Iterable接口。
Iterator 和Iterable的区别
参见 http://blog.csdn.net/lipengcn/article/details/51700153
public interface Iterable<T> {
/**
* Returns an iterator over a set of elements of type T.
* @return an Iterator.
*/
Iterator<T> iterator();
}
public interface Iterator<E> {
boolean hasNext();
E next();
void remove();
}
二、可比较
定义一个Class MyClass2,若需要使得MyClass2 具有可比较的特性,则其需实现Comparator 或是Comparable 接口。
Comparator 和 Comparable 的区别:Comparator是作为MyClass2类外部比较器进行实现,而Comparable是MyClass2类内部进行实现。
参见 http://blog.csdn.net/u012289441/article/details/45154401?ref=myread
public interface Comparator<T> {
/* @return a negative integer, zero, or a positive integer as the
* first argument is less than, equal to, or greater than the
* second.
*/
int compare(T o1, T O2);
/*
* @return <code>true</code> only if the specified object is also
* a comparator and it imposes the same ordering as this
* comparator.
*/
bool equals(Object obj);
}
public interface Comparable {
/*
*@return a negative integer, zero, or a positive integer as this object
* is less than, equal to, or greater than the specified object.
*/
public int compareTo(T o);
}