feat(collection): add stream support to MyList
This commit is contained in:
@@ -1,11 +1,14 @@
|
|||||||
package ru.project.collection;
|
package ru.project.collection;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
import java.util.stream.IntStream;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
public class MyLinkedList<T> implements MyList<T> {
|
public class MyLinkedList<T> implements MyList<T> {
|
||||||
private Node<T> head;
|
private Node<T> head;
|
||||||
private int size;
|
private int size;
|
||||||
|
|
||||||
|
@Override
|
||||||
public void add(T value) {
|
public void add(T value) {
|
||||||
if (head == null) {
|
if (head == null) {
|
||||||
this.head = new Node<>(value);
|
this.head = new Node<>(value);
|
||||||
@@ -19,6 +22,7 @@ public class MyLinkedList<T> implements MyList<T> {
|
|||||||
size++;
|
size++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public T get(int index) {
|
public T get(int index) {
|
||||||
int currentIndex = 0;
|
int currentIndex = 0;
|
||||||
Node<T> temp = head;
|
Node<T> temp = head;
|
||||||
@@ -34,6 +38,7 @@ public class MyLinkedList<T> implements MyList<T> {
|
|||||||
throw new IllegalArgumentException();
|
throw new IllegalArgumentException();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public void remove(int index) {
|
public void remove(int index) {
|
||||||
if (index < 0 || index >= size) {
|
if (index < 0 || index >= size) {
|
||||||
throw new IndexOutOfBoundsException();
|
throw new IndexOutOfBoundsException();
|
||||||
@@ -57,6 +62,7 @@ public class MyLinkedList<T> implements MyList<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public T set(int index, T value) {
|
public T set(int index, T value) {
|
||||||
if (index < 0 || index >= size) {
|
if (index < 0 || index >= size) {
|
||||||
throw new IllegalArgumentException();
|
throw new IllegalArgumentException();
|
||||||
@@ -79,6 +85,8 @@ public class MyLinkedList<T> implements MyList<T> {
|
|||||||
public boolean isEmpty() {
|
public boolean isEmpty() {
|
||||||
return size == 0;
|
return size == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public void addAll(MyList<? extends T> collection) {
|
public void addAll(MyList<? extends T> collection) {
|
||||||
if (collection == null || collection.isEmpty()) {
|
if (collection == null || collection.isEmpty()) {
|
||||||
return;
|
return;
|
||||||
@@ -101,6 +109,11 @@ public class MyLinkedList<T> implements MyList<T> {
|
|||||||
return Arrays.toString(res);
|
return Arrays.toString(res);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Stream<T> stream() {
|
||||||
|
return IntStream.range(0, size()).mapToObj(this::get);
|
||||||
|
}
|
||||||
|
|
||||||
private static class Node<T> {
|
private static class Node<T> {
|
||||||
private T value;
|
private T value;
|
||||||
private Node<T> next;
|
private Node<T> next;
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
package ru.project.collection;
|
package ru.project.collection;
|
||||||
|
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
public interface MyList<T> {
|
public interface MyList<T> {
|
||||||
|
|
||||||
void add(T element);
|
void add(T element);
|
||||||
@@ -13,5 +15,8 @@ public interface MyList<T> {
|
|||||||
int size();
|
int size();
|
||||||
|
|
||||||
boolean isEmpty();
|
boolean isEmpty();
|
||||||
|
|
||||||
void addAll(MyList<? extends T> collection);
|
void addAll(MyList<? extends T> collection);
|
||||||
|
|
||||||
|
Stream<T> stream();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user