java 可迭代与可比较的实现

一、可迭代
定义一个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);
}



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值