前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >java-iterable和iterator的区别

java-iterable和iterator的区别

原创
作者头像
cg错过
修改2020-11-26 18:15:38
5450
修改2020-11-26 18:15:38
举报
文章被收录于专栏:程序笔记程序笔记

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 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档