Iterable: 可迭代
Iterator: 迭代器
Iterable
中包含Iterator
如部分源码
public interface Iterable<T> {
/**
* Returns an iterator over elements of type {@code T}.
*
* @return an Iterator.
*/
Iterator<T> iterator();
...
而我目前遇到个问题就是需要将list
转换成Iterable
代码如下
package com.cgspace.tool;
import com.alibaba.fastjson.JSON;
import io.netty.handler.codec.http.cookie.Cookie;
import io.netty.handler.codec.http.cookie.DefaultCookie;
import org.junit.Test;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* Author: cg
* Date: 2020/4/29 18:38
*/
public class DoTest {
@Test
public void doRun(){
List<TCookie> tCookies = new ArrayList<>();
tCookies.add(new TCookie(1, 2002));
tCookies.add(new TCookie(2, 2004));
tCookies.add(new TCookie(3, 2006));
IterableCookie iterableCookie = new IterableCookie(tCookies);
Iterator<TCookie> tCookieIter = iterableCookie.iterator();
while(tCookieIter.hasNext()){
TCookie tCookie = tCookieIter.next();
System.out.println("MSG: " + JSON.toJSONString(tCookie, true));
}
}
}
class IterableCookie implements Iterable<TCookie>{
private int length = 0;
private List<TCookie> cookies = null;
public IterableCookie(List<TCookie> cookies){
this.cookies = cookies;
this.length = cookies.size();
}
@Override
public Iterator<TCookie> iterator() {
return new Iter();
}
private class Iter implements Iterator<TCookie>{
// 游标
private int cursor = 0;
private int lastRet = -1;
@Override
public boolean hasNext() {
return cursor != length;
}
@Override
public TCookie next() {
int i = cursor;
TCookie cookie = cookies.get(i);
lastRet = i;
cursor = i + 1;
return cookie;
}
public void remove(){
if(lastRet < 0){
return;
}
if(lastRet < cursor){
cursor --;
}
lastRet = -1;
}
}
}
class TCookie{
public TCookie(int id, int code){
this.id = id;
this.code = code;
}
private int id;
private int code;
public int getId() {
return id;
}
public int getCode() {
return code;
}
public void setId(int id) {
this.id = id;
}
public void setCode(int code) {
this.code = code;
}
}
输出
MSG: {
"code":2002,
"id":1
}
MSG: {
"code":2004,
"id":2
}
MSG: {
"code":2006,
"id":3
}
至于代码意思, 这里就不写了, 浪费时间
我的原则是, 不必要给未来的我写太多的注解, 因为未来的我不可能比现在的我差
网上解释
Iterable
一个集合对象要表明自己支持迭代,能有使用foreach语句的特权,就必须实现Iterable接口,表明我是可迭代的!然而实现Iterable接口,就必需为foreach语句提供一个迭代器。
这个迭代器是用接口定义的 iterator方法提供的。也就是iterator方法需要返回一个Iterator对象。
Iterator
包含3个方法: hasNext , next , remove。remove按需求实现,一般它很少用到,以至于Eclipse接口方法自动补全时,都忽略了remove放方法。
1、每次在迭代前 ,先调用hasNext()探测是否迭代到终点(本次还能再迭代吗?)。
2、next方法不仅要返回当前元素,还要后移游标cursor
3、remove()方法用来删除最近一次已经迭代出的元素
4、迭代出的元素是原集合中元素的拷贝(重要)
5、配合foreach使用
文章首发来自公众号: 程序员品
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有