Split or move java-streams module
This commit is contained in:
@@ -1,84 +0,0 @@
|
||||
package com.baeldung.forEach;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
class ReverseList extends ArrayList<String> {
|
||||
|
||||
List<String> list = Arrays.asList("A", "B", "C", "D");
|
||||
|
||||
Consumer<String> removeElement = s -> {
|
||||
System.out.println(s + " " + list.size());
|
||||
if (s != null && s.equals("A")) {
|
||||
list.remove("D");
|
||||
}
|
||||
};
|
||||
|
||||
@Override
|
||||
public Iterator<String> iterator() {
|
||||
|
||||
final int startIndex = this.size() - 1;
|
||||
final List<String> list = this;
|
||||
return new Iterator<String>() {
|
||||
|
||||
int currentIndex = startIndex;
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return currentIndex >= 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String next() {
|
||||
String next = list.get(currentIndex);
|
||||
currentIndex--;
|
||||
return next;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public void forEach(Consumer<? super String> action) {
|
||||
for (String s : this) {
|
||||
action.accept(s);
|
||||
}
|
||||
}
|
||||
|
||||
public void iterateParallel() {
|
||||
list.forEach(System.out::print);
|
||||
System.out.print(" ");
|
||||
list.parallelStream().forEach(System.out::print);
|
||||
}
|
||||
|
||||
public void iterateReverse() {
|
||||
List<String> myList = new ReverseList();
|
||||
myList.addAll(list);
|
||||
myList.forEach(System.out::print);
|
||||
System.out.print(" ");
|
||||
myList.stream().forEach(System.out::print);
|
||||
}
|
||||
|
||||
public void removeInCollectionForEach() {
|
||||
list.forEach(removeElement);
|
||||
}
|
||||
|
||||
public void removeInStreamForEach() {
|
||||
list.stream().forEach(removeElement);
|
||||
}
|
||||
|
||||
public static void main(String[] argv) {
|
||||
|
||||
ReverseList collectionForEach = new ReverseList();
|
||||
collectionForEach.iterateParallel();
|
||||
collectionForEach.iterateReverse();
|
||||
collectionForEach.removeInCollectionForEach();
|
||||
collectionForEach.removeInStreamForEach();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.baeldung.stream;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class InfiniteStreams {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(InfiniteStreams.class);
|
||||
|
||||
public static void main(String[] args) {
|
||||
doWhileOldWay();
|
||||
|
||||
doWhileStreamWay();
|
||||
|
||||
}
|
||||
|
||||
private static void doWhileOldWay() {
|
||||
|
||||
int i = 0;
|
||||
while (i < 10) {
|
||||
LOG.debug("{}", i);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
private static void doWhileStreamWay() {
|
||||
Stream<Integer> integers = Stream.iterate(0, i -> i + 1);
|
||||
integers.limit(10).forEach(System.out::println);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.baeldung.stream;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class StreamApi {
|
||||
|
||||
public static String getLastElementUsingReduce(List<String> valueList) {
|
||||
Stream<String> stream = valueList.stream();
|
||||
return stream.reduce((first, second) -> second).orElse(null);
|
||||
}
|
||||
|
||||
public static Integer getInfiniteStreamLastElementUsingReduce() {
|
||||
Stream<Integer> stream = Stream.iterate(0, i -> i + 1);
|
||||
return stream.limit(20).reduce((first, second) -> second).orElse(null);
|
||||
}
|
||||
|
||||
public static String getLastElementUsingSkip(List<String> valueList) {
|
||||
long count = (long) valueList.size();
|
||||
Stream<String> stream = valueList.stream();
|
||||
return stream.skip(count - 1).findFirst().orElse(null);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
package com.baeldung.stream;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
import com.codepoetics.protonpack.Indexed;
|
||||
import com.codepoetics.protonpack.StreamUtils;
|
||||
|
||||
import io.vavr.collection.Stream;
|
||||
import one.util.streamex.EntryStream;
|
||||
|
||||
public class StreamIndices {
|
||||
|
||||
public static List<String> getEvenIndexedStrings(String[] names) {
|
||||
List<String> evenIndexedNames = IntStream.range(0, names.length)
|
||||
.filter(i -> i % 2 == 0)
|
||||
.mapToObj(i -> names[i])
|
||||
.collect(Collectors.toList());
|
||||
return evenIndexedNames;
|
||||
}
|
||||
|
||||
public List<String> getEvenIndexedStringsVersionTwo(List<String> names) {
|
||||
List<String> evenIndexedNames = EntryStream.of(names)
|
||||
.filterKeyValue((index, name) -> index % 2 == 0)
|
||||
.values()
|
||||
.toList();
|
||||
return evenIndexedNames;
|
||||
}
|
||||
|
||||
public static List<Indexed<String>> getEvenIndexedStrings(List<String> names) {
|
||||
List<Indexed<String>> list = StreamUtils.zipWithIndex(names.stream())
|
||||
.filter(i -> i.getIndex() % 2 == 0)
|
||||
.collect(Collectors.toList());
|
||||
return list;
|
||||
}
|
||||
|
||||
public static List<Indexed<String>> getOddIndexedStrings(List<String> names) {
|
||||
List<Indexed<String>> list = StreamUtils.zipWithIndex(names.stream())
|
||||
.filter(i -> i.getIndex() % 2 == 1)
|
||||
.collect(Collectors.toList());
|
||||
return list;
|
||||
}
|
||||
|
||||
public static List<String> getOddIndexedStrings(String[] names) {
|
||||
List<String> oddIndexedNames = IntStream.range(0, names.length)
|
||||
.filter(i -> i % 2 == 1)
|
||||
.mapToObj(i -> names[i])
|
||||
.collect(Collectors.toList());
|
||||
return oddIndexedNames;
|
||||
}
|
||||
|
||||
public static List<String> getOddIndexedStringsVersionTwo(String[] names) {
|
||||
List<String> oddIndexedNames = Stream.of(names)
|
||||
.zipWithIndex()
|
||||
.filter(tuple -> tuple._2 % 2 == 1)
|
||||
.map(tuple -> tuple._1)
|
||||
.toJavaList();
|
||||
return oddIndexedNames;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package com.baeldung.stream.filter;
|
||||
|
||||
import javax.net.ssl.HttpsURLConnection;
|
||||
import java.io.IOException;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
|
||||
public class Customer {
|
||||
private String name;
|
||||
private int points;
|
||||
private String profilePhotoUrl;
|
||||
|
||||
public Customer(String name, int points) {
|
||||
this(name, points, "");
|
||||
}
|
||||
|
||||
public Customer(String name, int points, String profilePhotoUrl) {
|
||||
this.name = name;
|
||||
this.points = points;
|
||||
this.profilePhotoUrl = profilePhotoUrl;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public int getPoints() {
|
||||
return points;
|
||||
}
|
||||
|
||||
public boolean hasOver(int points) {
|
||||
return this.points > points;
|
||||
}
|
||||
|
||||
public boolean hasOverHundredPoints() {
|
||||
return this.points > 100;
|
||||
}
|
||||
|
||||
public boolean hasValidProfilePhoto() throws IOException {
|
||||
URL url = new URL(this.profilePhotoUrl);
|
||||
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
|
||||
return connection.getResponseCode() == HttpURLConnection.HTTP_OK;
|
||||
}
|
||||
|
||||
public boolean hasValidProfilePhotoWithoutCheckedException() {
|
||||
try {
|
||||
URL url = new URL(this.profilePhotoUrl);
|
||||
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
|
||||
return connection.getResponseCode() == HttpURLConnection.HTTP_OK;
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.baeldung.stream.sum;
|
||||
|
||||
public class ArithmeticUtils {
|
||||
|
||||
public static int add(int a, int b) {
|
||||
return a + b;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.baeldung.stream.sum;
|
||||
|
||||
public class Item {
|
||||
|
||||
private int id;
|
||||
private Integer price;
|
||||
|
||||
public Item(int id, Integer price) {
|
||||
super();
|
||||
this.id = id;
|
||||
this.price = price;
|
||||
}
|
||||
|
||||
// Standard getters and setters
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Integer getPrice() {
|
||||
return price;
|
||||
}
|
||||
|
||||
public void setPrice(Integer price) {
|
||||
this.price = price;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package com.baeldung.stream.sum;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class StreamSumCalculator {
|
||||
|
||||
public static Integer getSumUsingCustomizedAccumulator(List<Integer> integers) {
|
||||
return integers.stream()
|
||||
.reduce(0, ArithmeticUtils::add);
|
||||
|
||||
}
|
||||
|
||||
public static Integer getSumUsingJavaAccumulator(List<Integer> integers) {
|
||||
return integers.stream()
|
||||
.reduce(0, Integer::sum);
|
||||
|
||||
}
|
||||
|
||||
public static Integer getSumUsingReduce(List<Integer> integers) {
|
||||
return integers.stream()
|
||||
.reduce(0, (a, b) -> a + b);
|
||||
|
||||
}
|
||||
|
||||
public static Integer getSumUsingCollect(List<Integer> integers) {
|
||||
|
||||
return integers.stream()
|
||||
.collect(Collectors.summingInt(Integer::intValue));
|
||||
|
||||
}
|
||||
|
||||
public static Integer getSumUsingSum(List<Integer> integers) {
|
||||
|
||||
return integers.stream()
|
||||
.mapToInt(Integer::intValue)
|
||||
.sum();
|
||||
}
|
||||
|
||||
public static Integer getSumOfMapValues(Map<Object, Integer> map) {
|
||||
|
||||
return map.values()
|
||||
.stream()
|
||||
.mapToInt(Integer::valueOf)
|
||||
.sum();
|
||||
}
|
||||
|
||||
public static Integer getSumIntegersFromString(String str) {
|
||||
|
||||
Integer sum = Arrays.stream(str.split(" "))
|
||||
.filter((s) -> s.matches("\\d+"))
|
||||
.mapToInt(Integer::valueOf)
|
||||
.sum();
|
||||
|
||||
return sum;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package com.baeldung.stream.sum;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class StreamSumCalculatorWithObject {
|
||||
|
||||
public static Integer getSumUsingCustomizedAccumulator(List<Item> items) {
|
||||
return items.stream()
|
||||
.map(x -> x.getPrice())
|
||||
.reduce(0, ArithmeticUtils::add);
|
||||
}
|
||||
|
||||
public static Integer getSumUsingJavaAccumulator(List<Item> items) {
|
||||
return items.stream()
|
||||
.map(x -> x.getPrice())
|
||||
.reduce(0, Integer::sum);
|
||||
}
|
||||
|
||||
public static Integer getSumUsingReduce(List<Item> items) {
|
||||
return items.stream()
|
||||
.map(item -> item.getPrice())
|
||||
.reduce(0, (a, b) -> a + b);
|
||||
}
|
||||
|
||||
public static Integer getSumUsingCollect(List<Item> items) {
|
||||
return items.stream()
|
||||
.map(x -> x.getPrice())
|
||||
.collect(Collectors.summingInt(Integer::intValue));
|
||||
}
|
||||
|
||||
public static Integer getSumUsingSum(List<Item> items) {
|
||||
return items.stream()
|
||||
.mapToInt(x -> x.getPrice())
|
||||
.sum();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user