在Java的數據處理中,iterate技術是非常常用的一種技術。iterate技術可以迭代集合中的元素,並且將其傳遞給某個方法進行處理。Java工程師可以使用該技術來進行多種數據處理任務,如下:
一、數據過濾
在Java中,可以使用iterate技術來過濾集合中的元素。代碼示例如下:
List<Integer> nums = Arrays.asList(1,2,3,4,5);
nums.stream()
.filter(num -> num % 2 == 0)
.forEach(System.out::println);
上述代碼可以過濾出集合中的偶數,並將其列印出來。
二、數據轉換
Java工程師可以使用iterate技術來對集合中的元素進行轉換。代碼示例如下:
List<Integer> nums = Arrays.asList(1,2,3,4,5);
nums.stream()
.map(num -> num * num)
.forEach(System.out::println);
上述代碼將集合中的每個元素平方,並將結果列印出來。
三、數據統計
Java工程師可以使用iterate技術來對集合中的元素進行統計。代碼示例如下:
List<Integer> nums = Arrays.asList(1,2,3,4,5);
long count = nums.stream().count();
int sum = nums.stream().mapToInt(Integer::intValue).sum();
double avg = nums.stream().mapToInt(Integer::intValue).average().getAsDouble();
System.out.println("Count: " + count);
System.out.println("Sum: " + sum);
System.out.println("Average: " + avg);
上述代碼分別統計了集合中元素的個數、總和和平均值,並將結果列印出來。
四、數據分組
Java工程師可以使用iterate技術來對集合中的元素進行分組。代碼示例如下:
List<Person> people = Arrays.asList(
new Person("Alice", 23),
new Person("Bob", 25),
new Person("Charlie", 20),
new Person("David", 20),
new Person("Eva", 25));
Map<Integer, List<Person>> groups = people.stream()
.collect(Collectors.groupingBy(Person::getAge));
groups.entrySet().stream()
.forEach(entry -> {
System.out.print("Age " + entry.getKey() + ": ");
entry.getValue().stream()
.forEach(person -> System.out.print(person.getName() + " "));
System.out.println();
});
上述代碼將Person對象按照年齡進行分組,並將每個分組中的Person對象列印出來。
五、數據排序
Java工程師可以使用iterate技術來對集合中的元素進行排序。代碼示例如下:
List<Person> people = Arrays.asList(
new Person("Alice", 23),
new Person("Bob", 25),
new Person("Charlie", 20),
new Person("David", 20),
new Person("Eva", 25));
people.stream()
.sorted(Comparator.comparing(Person::getAge).thenComparing(Person::getName))
.forEach(System.out::println);
上述代碼將Person對象按照年齡和姓名進行排序,並將排序結果列印出來。
完整代碼示例
以下是上述例子的完整代碼:
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
public class Main {
public static void main(String[] args) {
// 數據過濾
List<Integer> nums = Arrays.asList(1,2,3,4,5);
nums.stream()
.filter(num -> num % 2 == 0)
.forEach(System.out::println);
// 數據轉換
nums = Arrays.asList(1,2,3,4,5);
nums.stream()
.map(num -> num * num)
.forEach(System.out::println);
// 數據統計
nums = Arrays.asList(1,2,3,4,5);
long count = nums.stream().count();
int sum = nums.stream().mapToInt(Integer::intValue).sum();
double avg = nums.stream().mapToInt(Integer::intValue).average().getAsDouble();
System.out.println("Count: " + count);
System.out.println("Sum: " + sum);
System.out.println("Average: " + avg);
// 數據分組
List<Person> people = Arrays.asList(
new Person("Alice", 23),
new Person("Bob", 25),
new Person("Charlie", 20),
new Person("David", 20),
new Person("Eva", 25));
Map<Integer, List<Person>> groups = people.stream()
.collect(Collectors.groupingBy(Person::getAge));
groups.entrySet().stream()
.forEach(entry -> {
System.out.print("Age " + entry.getKey() + ": ");
entry.getValue().stream()
.forEach(person -> System.out.print(person.getName() + " "));
System.out.println();
});
// 數據排序
people = Arrays.asList(
new Person("Alice", 23),
new Person("Bob", 25),
new Person("Charlie", 20),
new Person("David", 20),
new Person("Eva", 25));
people.stream()
.sorted(Comparator.comparing(Person::getAge).thenComparing(Person::getName))
.forEach(System.out::println);
}
}
原創文章,作者:MSTOR,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/316310.html
微信掃一掃
支付寶掃一掃