ตัวอย่างน่าใช้ Java 8 Stream
Example 1.
List<String> myList =
Arrays.asList("a1", "a3", "a2","b1", "b2", "c2", "c1");
Example 1.1 ต้องการ list ของคำที่ขึ้นต้นด้วย a เท่านั้น
public static void main(String[] args) {
List<String> myList =
Arrays.asList("a1", "a3", "a2","b1", "b2", "c2", "c1");
myList = myList.stream()
.filter(s -> s.startsWith("a"))
.collect(Collectors.toList());
System.out.println(myList);
}
Output : [a1, a3, a2]
Example 1.2 ต้องการ list ของคำที่ขึ้นต้นด้วย a เท่านั้นและให้เรียงตามลำดับตัวอักษร
public static void main(String[] args) {
List<String> myList =
Arrays.asList("a1", "a3", "a2","b1", "b2", "c2", "c1");
myList = myList.stream()
.filter(s -> s.startsWith("a"))
.sorted()
.collect(Collectors.toList());
System.out.println(myList);
}
Output : [a1, a2, a3]
Example 1.3 ต้องการ list ของคำที่ไม่ขึ้นต้นด้วย a และให้เรียงตามลำดับตัวอักษรและให้เปลี่ยนตัวอักษรเป็นตัวใหญ่ทั้งหมด
public static void main(String[] args) {
List<String> myList =
Arrays.asList("a1", "a3", "a2","b1", "b2", "c2", "c1");
myList = myList.stream()
.filter(s -> !s.startsWith("a"))
.map(String::toUpperCase)
.sorted()
.collect(Collectors.toList());
System.out.println(myList);
}
Output : [B1, B2, C1, C2]
Example 1.4 ต้องการ list ของคำที่ไม่ขึ้นต้นด้วย a และให้เรียงตามลำดับตัวอักษรและให้เปลี่ยนตัวอักษรเป็นตัวใหญ่ถ้ามีเลข 1 ในคำ
public static void main(String[] args) {
List<String> myList =
Arrays.asList("a1", "a3", "a2","b1", "b2", "c2", "c1");
myList = myList.stream()
.filter(s -> !s.startsWith("a"))
.sorted()
.map(s -> s.endsWith("1")? s.toUpperCase() : s )
.collect(Collectors.toList());
System.out.println(myList);
}
Output : [B1, b2, C1, c2]
Example 1.4 ต้องการทราบว่าใน list มีคำว่า “b2” หรือไม่
public static void main(String[] args) {
List<String> myList =
Arrays.asList("a1", "a3", "a2","b1", "b2", "c2", "c1");
System.out.println(myList.contains("b2"));
//or
System.out.println(myList.stream()
.filter(s -> s.contains("b2"))
.findAny()
.isPresent());
}
Output :
true
true
Example 2.
List<Integer> myList =
Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
Example 2.1 ต้องการทราบค่าสูงสุดใน myList
public static void main(String[] args) {
List<Integer> myList =
Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
System.out.println(myList
.stream()
.max(Comparator.comparingInt(i -> i))
.get());
}
Output : 10
Example 2.2 ต้องการทราบค่าต่ำสุดใน myList
public static void main(String[] args) {
List<Integer> myList =
Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); System.out.println(myList
.stream()
.min(Comparator.comparingInt(i -> i))
.get());
}
หรือ
public static void main(String[] args) {
List<Integer> myList =
Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
System.out.println(Collections.max(myList));
}
Output : 1
Example 2.3 ต้องการทราบค่าต่ำสุดใน myList
public static void main(String[] args) {
List<Integer> myList =
Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);System.out.println(myList
.stream()
.min(Comparator.comparingInt(i -> i))
.get());
}
หรือ
public static void main(String[] args) {
List<Integer> myList =
Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
System.out.println(Collections.min(myList));
}
Output : 1
Example 2.4 ต้องการทราบค่า averate ของตัวเลขใน myList
public static void main(String[] args) {
List<Integer> myList =
Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
System.out.println(myList
.stream()
.mapToInt(i -> i)
.average()
.getAsDouble()
);
}
Output : 5.5
Example 2.5 print เลขคู่ ของตัวเลขใน myList
public static void main(String[] args) {
List<Integer> myList =
Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
myList.stream()
.filter(i -> i % 2 == 0)
.collect(Collectors.toList())
.forEach((System.out::println));
}
Output :
2
4
6
8
10
Example 2.6 หาค่า sum ของเลขคู่ ใน myList
public static void main(String[] args) {
List<Integer> myList =
Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
System.out.println(
myList.stream()
.filter(i -> i % 2 == 0)
.mapToInt(i -> i)
.sum()
);
}
Output : 30
Example 2.7 print เลขคู่ ของตัวเลขใน myList และตัดตัวเลขที่ซ้ำออกไป
public static void main(String[] args) {
List<Integer> myList =
Arrays.asList(1, 2, 3, 3, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10);
myList.stream()
.distinct()
.collect(Collectors.toList())
.forEach((System.out::println));
}
Output :
1
2
3
4
5
6
7
8
9
10
Example 2.8 สร้าง List ใหม่จาก myList ที่มีค่ายกกำลังสองของค่าเดิม (Reduce is the operations to reduces a stream into a single resultant value)
public static void main(String[] args) {
List<Integer> numbers =
Arrays.asList(1, 2, 3, 4, 5);
numbers.stream()
.map(x -> x * x)
.collect(Collectors.toList())
.forEach((System.out::println));
}
Output :
1
4
9
16
25
Example 2.9 พิมพ์คำที่ยาวที่สุดใน List
public static void main(String[] args) {
List<String> words = Arrays.asList("a", "abcdef", "abc",
"ab", "abcd");
Optional<String> longestString = words.stream()
.reduce((word1, word2)
-> word1.length() > word2.length()
? word1 : word2);
longestString.ifPresent(System.out::println);
}
Output : abcdef
Example 2.10 หาเลขจำนวนคู่ที่มีค่ามากที่สุดใน list
public static void main(String[] args) {
List<Integer> number = Arrays.asList(2, 3, 8, 5, 6, 4);
number.stream()
.filter(x -> x % 2== 0)
.reduce((x, y) -> (x > y) ? x : y)
.ifPresent(System.out::println);
}
Output : 8
Example 2.11 หาค่า sum ใน object list
public class TestMe {
static class Simple {
private BigDecimal amount;
private String label;
public Simple(BigDecimal amount, String label) {
this.amount = amount;
this.label = label;
}
public BigDecimal getAmount() {
return amount;
}
public void setAmount(BigDecimal amount) {
this.amount = amount;
}
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
}
public static void main(String[] args) {
List<Simple> simpleList = Arrays.asList(
new Simple(new BigDecimal("1.3"), "A"),
new Simple(new BigDecimal("10.3"), "B"),
new Simple(new BigDecimal("21.3"), "C")
);
BigDecimal sum = simpleList.stream()
.map(Simple::getAmount)
.reduce(BigDecimal.ZERO, BigDecimal::add);
System.out.println(sum);
}
Output : 32.9