Difference between Iterable and Iterator (#12079)
* Difference between Iterable and Iterator Add examples of the Iterable and the Iterator usage. * Minor changes Add UnitTests for Iterable examples * Fix iterable example Add Custom collection for the Iterable implementation example. * Fix iterable example Change the example for the Iterable interface implementation * Revert "Fix iterable example" This reverts commit 4015cbc038b1b657c54fa666193c0c7019a474ad. * Minor changes
This commit is contained in:
parent
0ffd9d81b0
commit
fa47170025
|
@ -0,0 +1,17 @@
|
|||
package com.baeldung.collections.iterable;
|
||||
|
||||
class CustomIterableClient {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
ShoppingCart<Product> shoppingCart = new ShoppingCart<>();
|
||||
shoppingCart.add(new Product("Tuna", 42));
|
||||
shoppingCart.add(new Product("Eggplant", 65));
|
||||
shoppingCart.add(new Product("Salad", 45));
|
||||
shoppingCart.add(new Product("Banana", 29));
|
||||
|
||||
for (Product product : shoppingCart) {
|
||||
System.out.println(product.getName());
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
package com.baeldung.collections.iterable;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
public class IterableExample {
|
||||
|
||||
public void iterateUsingIterator(List<Integer> numbers) {
|
||||
Iterator<Integer> iterator = numbers.iterator();
|
||||
while (iterator.hasNext()) {
|
||||
System.out.println(iterator.next());
|
||||
}
|
||||
}
|
||||
|
||||
public void iterateUsingEnhancedForLoop(List<Integer> numbers) {
|
||||
for (Integer number : numbers) {
|
||||
System.out.println(number);
|
||||
}
|
||||
}
|
||||
|
||||
public void iterateUsingForEachLoop(List<Integer> numbers) {
|
||||
numbers.forEach(System.out::println);
|
||||
}
|
||||
|
||||
public void removeElementsUsingIterator(List<Integer> numbers) {
|
||||
Iterator<Integer> iterator = numbers.iterator();
|
||||
while (iterator.hasNext()) {
|
||||
iterator.next();
|
||||
iterator.remove();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
package com.baeldung.collections.iterable;
|
||||
|
||||
class Product {
|
||||
|
||||
private String name;
|
||||
private double price;
|
||||
|
||||
public Product(String code, double price) {
|
||||
this.name = code;
|
||||
this.price = price;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public double getPrice() {
|
||||
return price;
|
||||
}
|
||||
|
||||
public void setPrice(double price) {
|
||||
this.price = price;
|
||||
}
|
||||
}
|
|
@ -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 ShoppingCart<E> implements Iterable<E> {
|
||||
|
||||
private E[] elementData;
|
||||
private int size;
|
||||
|
||||
public ShoppingCart() {
|
||||
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 ShoppingCartIterator();
|
||||
}
|
||||
|
||||
public class ShoppingCartIterator 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 = ShoppingCart.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,14 @@
|
|||
package com.baeldung.collections.iterator;
|
||||
|
||||
import java.util.Iterator;
|
||||
|
||||
class CustomIteratorClient {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Iterator<Integer> iterator = Numbers.iterator();
|
||||
|
||||
while (iterator.hasNext()) {
|
||||
System.out.println(iterator.next());
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,60 @@
|
|||
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> NUMBER_LIST = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
|
||||
|
||||
private Numbers() {
|
||||
}
|
||||
|
||||
public static Iterator<Integer> iterator() {
|
||||
return new PrimeIterator();
|
||||
}
|
||||
|
||||
private static class PrimeIterator implements Iterator<Integer> {
|
||||
|
||||
private int cursor;
|
||||
|
||||
@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;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,54 @@
|
|||
package com.baeldung.collections.iterable;
|
||||
|
||||
import com.baeldung.collections.iterable.IterableExample;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
class IterableUnitTest {
|
||||
|
||||
private static List<Integer> getNumbers() {
|
||||
List<Integer> numbers = new ArrayList<>();
|
||||
numbers.add(10);
|
||||
numbers.add(20);
|
||||
numbers.add(30);
|
||||
numbers.add(40);
|
||||
return numbers;
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenNumbers_whenUsingIterator_thenCorrectSize() {
|
||||
List<Integer> numbers = getNumbers();
|
||||
IterableExample iterableExample = new IterableExample();
|
||||
iterableExample.iterateUsingIterator(numbers);
|
||||
assertEquals(4, numbers.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenNumbers_whenRemoveElements_thenEmptyList() {
|
||||
List<Integer> numbers = getNumbers();
|
||||
IterableExample iterableExample = new IterableExample();
|
||||
iterableExample.removeElementsUsingIterator(numbers);
|
||||
assertEquals(0, numbers.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenNumbers_whenIterateUsingEnhancedForLoop_thenCorrectSize() {
|
||||
List<Integer> numbers = getNumbers();
|
||||
IterableExample iterableExample = new IterableExample();
|
||||
iterableExample.iterateUsingEnhancedForLoop(numbers);
|
||||
assertEquals(4, numbers.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenNumbers_whenIterateUsingForEachLoop_thenCorrectSize() {
|
||||
List<Integer> numbers = getNumbers();
|
||||
IterableExample iterableExample = new IterableExample();
|
||||
iterableExample.iterateUsingForEachLoop(numbers);
|
||||
assertEquals(4, numbers.size());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue