Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class Main {
    public static void main(String[] args) {
        Iterable<String> iterable = new MyIterable<String>("a", "b", "a", "b");

        for (Iterator<String> iterator = iterable.iterator(); iterator.hasNext(); iterator.remove()) {
            System.out.println(iterator.next());
        }
        System.out.println("Used in foreach");
        for (String s : iterable) {
            System.out.println(s);
        }

    }

}

class MyIterable<E> implements Iterable<E> {
    private Set<E> internalSet = new HashSet<E>();

    public MyIterable(E... all) {
        for (E e : all) {
            internalSet.add(e);
        }
    }

    public Iterator<E> iterator() {
        final Iterator<E> i = internalSet.iterator();
        return new Iterator<E>() {
            public boolean hasNext() {
                return i.hasNext();
            }

            public E next() {
                return i.next();
            }

            public void remove() {
                //throw new UnsupportedOperationException();
            }
        };
    }

}