Fix iterable example
Add Custom collection for the Iterable implementation example.
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
package com.baeldung.collections.iterable;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.ConcurrentModificationException;
|
||||
import java.util.Iterator;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class CustomCollection<E> implements Iterable<E> {
|
||||
|
||||
private E[] elementData;
|
||||
private int size;
|
||||
|
||||
public CustomCollection() {
|
||||
this.elementData = (E[]) new Object[] {};
|
||||
}
|
||||
|
||||
public void add(E element) {
|
||||
ensureCapacity(size + 1);
|
||||
elementData[size++] = element;
|
||||
}
|
||||
|
||||
private void ensureCapacity(int minCapacity) {
|
||||
int oldCapacity = elementData.length;
|
||||
int newCapacity = oldCapacity + (oldCapacity >> 1);
|
||||
if (newCapacity - minCapacity < 0) {
|
||||
newCapacity = minCapacity;
|
||||
}
|
||||
elementData = Arrays.copyOf(elementData, newCapacity);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<E> iterator() {
|
||||
return new CollectionIterator();
|
||||
}
|
||||
|
||||
public class CollectionIterator implements Iterator<E> {
|
||||
int cursor;
|
||||
int lastReturned = -1;
|
||||
|
||||
public boolean hasNext() {
|
||||
return cursor != size;
|
||||
}
|
||||
|
||||
public E next() {
|
||||
return getNextElement();
|
||||
}
|
||||
|
||||
private E getNextElement() {
|
||||
int current = cursor;
|
||||
exist(current);
|
||||
|
||||
E[] elements = CustomCollection.this.elementData;
|
||||
validate(elements, current);
|
||||
|
||||
cursor = current + 1;
|
||||
lastReturned = current;
|
||||
return elements[lastReturned];
|
||||
}
|
||||
|
||||
private void exist(int current) {
|
||||
if (current >= size) {
|
||||
throw new NoSuchElementException();
|
||||
}
|
||||
}
|
||||
|
||||
private void validate(E[] elements, int current) {
|
||||
if (current >= elements.length) {
|
||||
throw new ConcurrentModificationException();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.collections.iterable;
|
||||
|
||||
class CustomIterableClient {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
CustomCollection<Product> products = new CustomCollection<>();
|
||||
products.add(new Product("Tuna", 42));
|
||||
products.add(new Product("Eggplant", 65));
|
||||
products.add(new Product("Salad", 45));
|
||||
products.add(new Product("Banana", 29));
|
||||
|
||||
for (Product product : products) {
|
||||
System.out.println(product.getName());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
package com.baeldung.collections.iterable;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
class ProductIterable implements Iterable<Product> {
|
||||
|
||||
List<Product> productList;
|
||||
|
||||
public ProductIterable(List<Product> productList) {
|
||||
this.productList = productList;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<Product> iterator() {
|
||||
return productList.iterator();
|
||||
}
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
package com.baeldung.collections.iterable;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
class ProductIterableClient {
|
||||
|
||||
public static void main(String[] args) {
|
||||
List<Product> products = Arrays.asList(new Product("Salad", 45), new Product("Tuna", 42), new Product("Eggplant", 65), new Product("Bread", 22), new Product("Banana", 29));
|
||||
|
||||
ProductIterable iterable = new ProductIterable(products);
|
||||
|
||||
for (Product product : iterable) {
|
||||
System.out.println(product.getName());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
package com.baeldung.collections.iterator;
|
||||
|
||||
interface CustomIterator<T> {
|
||||
|
||||
boolean hasNext();
|
||||
|
||||
T next();
|
||||
}
|
||||
@@ -1,9 +1,11 @@
|
||||
package com.baeldung.collections.iterator;
|
||||
|
||||
import java.util.Iterator;
|
||||
|
||||
class CustomIteratorClient {
|
||||
|
||||
public static void main(String[] args) {
|
||||
CustomIterator<Integer> iterator = Numbers.iterator();
|
||||
Iterator<Integer> iterator = Numbers.iterator();
|
||||
|
||||
while (iterator.hasNext()) {
|
||||
System.out.println(iterator.next());
|
||||
|
||||
@@ -1,24 +1,54 @@
|
||||
package com.baeldung.collections.iterator;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
class Numbers {
|
||||
|
||||
private static final List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
|
||||
private static final List<Integer> NUMBER_LIST = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
|
||||
|
||||
private Numbers() {
|
||||
}
|
||||
|
||||
public static CustomIterator<Integer> iterator() {
|
||||
public static Iterator<Integer> iterator() {
|
||||
return new PrimeIterator();
|
||||
}
|
||||
|
||||
private static class PrimeIterator implements CustomIterator<Integer> {
|
||||
private static class PrimeIterator implements Iterator<Integer> {
|
||||
|
||||
private int currentPosition;
|
||||
private int cursor;
|
||||
|
||||
private static boolean isPrime(int number) {
|
||||
@Override
|
||||
public Integer next() {
|
||||
exist(cursor);
|
||||
return NUMBER_LIST.get(cursor++);
|
||||
}
|
||||
|
||||
private void exist(int current) {
|
||||
if (current >= NUMBER_LIST.size()) {
|
||||
throw new NoSuchElementException();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
if (cursor > NUMBER_LIST.size()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (int i = cursor; i < NUMBER_LIST.size(); i++) {
|
||||
if (isPrime(NUMBER_LIST.get(i))) {
|
||||
cursor = i;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private boolean isPrime(int number) {
|
||||
for (int i = 2; i <= number / 2; ++i) {
|
||||
if (number % i == 0) {
|
||||
return false;
|
||||
@@ -26,26 +56,5 @@ class Numbers {
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer next() {
|
||||
return numbers.get(currentPosition++);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
if (currentPosition > numbers.size()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (int i = currentPosition; i < numbers.size(); i++) {
|
||||
if (isPrime(numbers.get(i))) {
|
||||
currentPosition = i;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -51,4 +51,4 @@ class IterableUnitTest {
|
||||
iterableExample.iterateUsingForEachLoop(numbers);
|
||||
assertEquals(4, numbers.size());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user