Java 8 Stream – Simple Example

IntStream
	.range(1, 10)
	.forEach(System.out::println);
IntStream
	.range(1, 10)
	.skip(5)
	.forEach(x -> System.out.println(x));
int sum = IntStream
	.range(1, 10)
	.sum();
System.out.println(sum);
Stream.of("Ccccc", "Aaaa", "Bbbbb")
	.sorted()
	.filter(x -> x.startsWith("X"))
	.findFirst()
	.ifPresent(x -> System.out.println("!!! = " + x));
String[] test = {"Aa", "Ab", "Ba", "Bb", "Ca", "Cb"};
Arrays.stream(test)
	.filter(x -> x.endsWith("a"))
	.sorted()
	.forEach(System.out::println);
List<String> items = Arrays.asList("aa", "bb", "cc", "dd");
items
	.stream()
	.map(String::toUpperCase)
	.forEach(System.out::println);
import java.util.Arrays;
import java.util.List;

public class JavaStream7 {

	public static void main(String[] args) {

		List<String> items = Arrays.asList("aa", "bb", "cc", "dd");

		items
				.stream()
				.map(x -> new StringDto(x))
				.forEach(System.out::println);

	}

	static class StringDto {

		private String name;

		public StringDto(String name) {
			this.name = name;
		}

		@Override
		public String toString() {
			return "StringDto{" +
					"name='" + name + '\'' +
					'}';
		}
	}

}
public class JavaStream8 {

	public static void main(String[] args) {

		List<OldA> oldList = Arrays.asList(
				new OldA("A"),
				new OldA("B"),
				new OldA("C")
		);


		List<OldB> newList = oldList
				.stream()
				.filter(x -> !x.getName().startsWith("A"))
				.map(x -> new OldB(x.getName()))
				.collect(Collectors.toList());

		newList.forEach(System.out::println);
	}

	static class OldA {
		private String name;

		public OldA(String name) {
			this.name = name;
		}

		public String getName() {
			return name;
		}

		@Override
		public String toString() {
			return "OldA{" +
					"name='" + name + '\'' +
					'}';
		}
	}

	static class OldB {
		private String name;

		public OldB(String name) {
			this.name = name;
		}

		@Override
		public String toString() {
			return "OldB{" +
					"name='" + name + '\'' +
					'}';
		}
	}

}
public class JavaStream9 {

	public static void main(String[] args) {

		int[] numbers = new int[]{5, 4, 1, 2, 6, 7, 3, 8, 9, 0, 10};

		Arrays.stream(numbers)
				.sorted()
				.filter(x -> x % 2 == 0)
				.forEach(System.out::println);

		Arrays.stream(numbers)
				.sorted()
				.filter(x -> x % 2 == 0)
				.mapToObj(x -> new SomeDto(x))
				.collect(Collectors.toList())
				.forEach(System.out::println);

	}

	static class SomeDto {
		public int val;

		public SomeDto(int val) {
			this.val = val;
		}

		@Override
		public String toString() {
			return "SomeDto{" +
					"val=" + val +
					'}';
		}
	}
}
public class JavaStream11 {

	public static void main(String[] args) {

		List<PostItem> postItems = Arrays.asList(
				new PostItem(1),
				new PostItem(2),
				new PostItem(3),
				new PostItem(4),
				new PostItem(5)
		);

		List<PostItem> collect = postItems
				.stream()
				.skip(2)
				.limit(5)
				.collect(Collectors.toList());

		collect.forEach(System.out::println);
	}


	static class PostItem {

		public int id;

		public PostItem(int id) {
			this.id = id;
		}

		@Override
		public String toString() {
			return "PostItem{" +
					"id=" + id +
					'}';
		}
	}

}
public class JavaStream12 {

	public static void main(String[] args) {

		List<Person> people = Arrays.asList(
				new Person("Sara", Gender.FEMALE, 20),
				new Person("Sara", Gender.FEMALE, 22),
				new Person("Bob", Gender.MALE, 20),
				new Person("Paula", Gender.FEMALE, 32),
				new Person("Paul", Gender.MALE, 32),
				new Person("Jack", Gender.MALE, 2),
				new Person("Jack", Gender.MALE, 72),
				new Person("Jill", Gender.FEMALE, 12)
		);

		List<String> listOfName = people
				.stream()
				.filter(x -> x.age > 18)
				.map(x -> x.name.toUpperCase())
				.collect(Collectors.toList());

		System.out.println(listOfName);
	}

	static class Person {

		public String name;
		public Gender gender;
		public int age;

		public Person(String name, Gender gender, int age) {
			this.name = name;
			this.gender = gender;
			this.age = age;
		}
	}

	enum Gender {
		MALE,
		FEMALE
	}
}
public class JavaStream13 {

	public static void main(String[] args) {

		List<Person> people = Arrays.asList(
				new Person("Sara", Gender.FEMALE, 20),
				new Person("Sara", Gender.FEMALE, 22),
				new Person("Bob", Gender.MALE, 20),
				new Person("Paula", Gender.FEMALE, 32),
				new Person("Paul", Gender.MALE, 32),
				new Person("Jack", Gender.MALE, 2),
				new Person("Jack", Gender.MALE, 72),
				new Person("Jill", Gender.FEMALE, 12)
		);

		Comparator<Person> personComparator = (p1, p2) -> p1.age > p2.age ? 1 : -1;

		Optional<Person> personMax = people.stream().max(personComparator);
		Optional<Person> personMin = people.stream().min(personComparator);

		System.out.println(personMax.get());
		System.out.println(personMin.get());
	}

	static class Person {

		public String name;
		public Gender gender;
		public int age;

		public Person(String name, Gender gender, int age) {
			this.name = name;
			this.gender = gender;
			this.age = age;
		}

		@Override
		public String toString() {
			return "Person{" +
					"name='" + name + '\'' +
					", gender=" + gender +
					", age=" + age +
					'}';
		}
	}

	enum Gender {
		MALE,
		FEMALE
	}

}
public class JavaStream14 {

	public static void main(String[] args) {

		List<ProductItem> items = Arrays.asList(
				new ProductItem("A", 35, true),
				new ProductItem("B", 30, false),
				new ProductItem("C", -25, true),
				new ProductItem("D", -15, false),
				new ProductItem("E", 10, true)
		);

		int sum = items.stream()
				.filter(x -> x.price > 0)
				.filter(x -> x.inCard)
				.mapToInt(x -> x.price)
				.sum();

		System.out.println(sum);

	}

	static class ProductItem {
		public String name;
		public int price;
		public boolean inCard;

		public ProductItem(String name, int price, boolean inCard) {
			this.name = name;
			this.price = price;
			this.inCard = inCard;
		}

		@Override
		public String toString() {
			return "ProductItem{" +
					"name='" + name + '\'' +
					", price=" + price +
					", inCard=" + inCard +
					'}';
		}
	}
}
public class JavaStream15 {

	public static void main(String[] args) {

		List<String> words = Arrays.asList(
				"Cat",
				"Dog",
				"Empty?",
				"",
				null,
				""
		);

		List<String> newWords = words.stream()
				.filter(x -> x != null && !x.isEmpty())
				.collect(Collectors.toList());

		long count = words.stream()
				.filter(x -> x != null && !x.isEmpty())
				.count();

		System.out.println(count);
		System.out.println(newWords);
	}

}
public class JavaStream16 {

	public static void main(String[] args) {
		List<String> words = Arrays.asList("tasty", "rush",
				"shade", "welcome", "untidy", "church",
				"knock", "current", "bawdy", "chemical",
				"stiff", "cloth", "ship", "spiteful",
				"ink", "nest", "damage", "shallow", "touch", "blush"
		);

		System.out.println(
				words.stream().reduce("", (a, b) -> a + " " + b)
		);

		System.out.println(
				words.stream().reduce((a, b) -> a + " " + b)
		);

		System.out.println(
				words.stream().collect(Collectors.joining(" "))
		);

	}

}

Leave a Reply