Difference between Iterable and Iterator
Add examples of the Iterable and the Iterator usage.
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
package com.baeldung.collections.iterable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
class IterableExample {
|
||||
|
||||
public static void iterateUsingIterator() {
|
||||
List<Integer> numbers = getNumbers();
|
||||
|
||||
Iterator<Integer> iterator = numbers.iterator();
|
||||
while (iterator.hasNext()) {
|
||||
System.out.println(iterator.next());
|
||||
}
|
||||
}
|
||||
|
||||
public static void removeElementsUsingIterator() {
|
||||
List<Integer> numbers = getNumbers();
|
||||
|
||||
Iterator<Integer> iterator = numbers.iterator();
|
||||
while (iterator.hasNext()) {
|
||||
iterator.remove();
|
||||
}
|
||||
}
|
||||
|
||||
public static void iterateUsingEnhancedForLoop() {
|
||||
List<Integer> numbers = getNumbers();
|
||||
|
||||
for (Integer number : numbers) {
|
||||
System.out.println(number);
|
||||
}
|
||||
}
|
||||
|
||||
public static void iterateUsingForEachLoop() {
|
||||
List<Integer> numbers = getNumbers();
|
||||
numbers.forEach(System.out::println);
|
||||
}
|
||||
|
||||
private static List<Integer> getNumbers() {
|
||||
List<Integer> numbers = new ArrayList<>();
|
||||
numbers.add(10);
|
||||
numbers.add(20);
|
||||
numbers.add(30);
|
||||
numbers.add(40);
|
||||
return numbers;
|
||||
}
|
||||
}
|
||||
@@ -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,18 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
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());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.baeldung.collections.iterator;
|
||||
|
||||
interface CustomIterator<T> {
|
||||
|
||||
boolean hasNext();
|
||||
|
||||
T next();
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.baeldung.collections.iterator;
|
||||
|
||||
class CustomIteratorClient {
|
||||
|
||||
public static void main(String[] args) {
|
||||
CustomIterator<Integer> iterator = Numbers.iterator();
|
||||
|
||||
while (iterator.hasNext()) {
|
||||
System.out.println(iterator.next());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package com.baeldung.collections.iterator;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
class Numbers {
|
||||
|
||||
private static final List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
|
||||
|
||||
private Numbers() {
|
||||
}
|
||||
|
||||
public static CustomIterator<Integer> iterator() {
|
||||
return new PrimeIterator();
|
||||
}
|
||||
|
||||
private static class PrimeIterator implements CustomIterator<Integer> {
|
||||
|
||||
private int currentPosition;
|
||||
|
||||
private static boolean isPrime(int number) {
|
||||
for (int i = 2; i <= number / 2; ++i) {
|
||||
if (number % i == 0) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user