Java コレクター ツール クラスの使用方法
## ●averagingInt: ストリーム内の要素を int 型の要素にマッピングします。ストリーム内の要素を int 型の要素にマッピングし、平均値を見つけます。
#● summarizingInt: ストリーム内の要素を int 型の要素にマッピングし、説明情報を取得します。実践手順1. 前提条件 パーソンクラスpackage com.example;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.springframework.context.annotation.Configuration;
import java.util.Objects;
/**
* @BelongsProject: StreamOperate
* @BelongsPackage: com.example
* @CreateTime: 2023-05-01 11:18
* @Description: Person实体类
* @Version: 1.0
*/
public class Person implements Comparable<Person>{
public String getName() {
return name;
}
public Person setName(String name) {
this.name = name;
return this;
}
public int getAge() {
return age;
}
public Person setAge(int age) {
this.age = age;
return this;
}
public int getScore() {
return score;
}
public Person setScore(int score) {
this.score = score;
return this;
}
private String name;
private int age;
private int score;
public Person(String name, int age, int score) {
this.name = name;
this.age = age;
this.score = score;
}
public Person() {
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", score=" + score +
'}';
}
@Override
public boolean equals(Object o) {
//地址相同,为true
if (this == o) return true;
//为null,并且类型不一样,为false
if (o == null || getClass() != o.getClass()) return false;
//向下转型,再去比较属性值
Person person = (Person) o;
//如果属性值相同,最后的结果为true
return age == person.age && score == person.score && Objects.equals(name, person.name);
//return false;
}
@Override
public int hashCode() {
return Objects.hash(name, age, score);
}
@Override
public int compareTo(Person o) {
return this.getScore()-o.getScore();
}
}
ログイン後にコピー
データクラスpackage com.example; import lombok.AllArgsConstructor; import lombok.Data; import org.springframework.context.annotation.Configuration; import java.util.Objects; /** * @BelongsProject: StreamOperate * @BelongsPackage: com.example * @CreateTime: 2023-05-01 11:18 * @Description: Person实体类 * @Version: 1.0 */ public class Person implements Comparable<Person>{ public String getName() { return name; } public Person setName(String name) { this.name = name; return this; } public int getAge() { return age; } public Person setAge(int age) { this.age = age; return this; } public int getScore() { return score; } public Person setScore(int score) { this.score = score; return this; } private String name; private int age; private int score; public Person(String name, int age, int score) { this.name = name; this.age = age; this.score = score; } public Person() { } @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", age=" + age + ", score=" + score + '}'; } @Override public boolean equals(Object o) { //地址相同,为true if (this == o) return true; //为null,并且类型不一样,为false if (o == null || getClass() != o.getClass()) return false; //向下转型,再去比较属性值 Person person = (Person) o; //如果属性值相同,最后的结果为true return age == person.age && score == person.score && Objects.equals(name, person.name); //return false; } @Override public int hashCode() { return Objects.hash(name, age, score); } @Override public int compareTo(Person o) { return this.getScore()-o.getScore(); } }
package com.example; import org.springframework.context.annotation.Configuration; import java.util.ArrayList; /** * @BelongsProject: StreamOperate * @BelongsPackage: com.example * @CreateTime: 2023-05-01 11:08 * @Description: Data类 * @Version: 1.0 */ public class Data { public static ArrayList<Person> getData() { ArrayList<Person> personList = new ArrayList<>(); personList.add(new Person("张三", 18, 90)); personList.add(new Person("李四", 19, 100)); personList.add(new Person("王五", 17, 60)); personList.add(new Person("赵六", 18, 89)); personList.add(new Person("孙七", 20, 96)); personList.add(new Person("郑十", 20, 46)); personList.add(new Person("周八", 20, 96)); personList.add(new Person("周八", 20, 96)); personList.add(new Person("吴九", 20, 45)); personList.add(new Person("邓十一", 20, 35)); personList.add(new Person("刘十二", 20, 99)); personList.add(new Person("小十三", 20, 56)); personList.add(new Person("小十三", 20, 56)); return personList; } }
public static void main(String[] args) {
Stream<Person> stream = Data.getData().stream();
//maxBy:获取流中最大元素;minBy:获取流中最小元素
System.out.println(Data.getData().stream().collect(Collectors.maxBy((ele1, ele2) -> ele1.getScore() - ele2.getScore())));
System.out.println(Data.getData().stream().collect(Collectors.minBy((ele1, ele2) -> ele1.getAge() - ele2.getAge())));
}
ログイン後にコピー
joining: ストリーム内の要素を文字列形式でマージ、結合します public static void main(String[] args) { Stream<Person> stream = Data.getData().stream(); //maxBy:获取流中最大元素;minBy:获取流中最小元素 System.out.println(Data.getData().stream().collect(Collectors.maxBy((ele1, ele2) -> ele1.getScore() - ele2.getScore()))); System.out.println(Data.getData().stream().collect(Collectors.minBy((ele1, ele2) -> ele1.getAge() - ele2.getAge()))); }
public static void main(String[] args) { Stream<Person> stream = Data.getData().stream(); //joining:合并,将流中的元素,以字符串的形式拼接起来 //将集合中person对象的姓名拼接成一个字符串 System.out.println(Data.getData().stream().map(Person::getName).collect(Collectors.joining())); System.out.println(Data.getData().stream().map(Person::getName).collect(Collectors.joining("-"))); System.out.println(Data.getData().stream().map(Person::getName).collect(Collectors.joining("-", "{", "}"))); }
public static void main(String[] args) { Stream<Person> stream = Data.getData().stream(); //summingInt:把流中的元素映射成int类型的元素,求和 System.out.println(Data.getData().stream().collect(Collectors.summingInt(Person::getScore))); }
public static void main(String[] args) { Stream<Person> stream = Data.getData().stream(); //averagingInt:把流中的元素映射成int类型的元素,求平均值 System.out.println(Data.getData().stream().collect(Collectors.averagingInt(Person::getScore))); }
public static void main(String[] args) { Stream<Person> stream = Data.getData().stream(); //summarizingInt:把流中的元素映射成int类型的元素,获取描述信息 IntSummaryStatistics collect = Data.getData().stream().collect(Collectors.summarizingInt(Person::getScore)); System.out.println(collect); System.out.println(collect.getCount()); System.out.println(collect.getSum()); System.out.println(collect.getMax()); System.out.println(collect.getMax()); System.out.println(collect.getAverage());
以上がJava コレクター ツール クラスの使用方法の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

AI Hentai Generator
AIヘンタイを無料で生成します。

人気の記事

ホットツール

メモ帳++7.3.1
使いやすく無料のコードエディター

SublimeText3 中国語版
中国語版、とても使いやすい

ゼンドスタジオ 13.0.1
強力な PHP 統合開発環境

ドリームウィーバー CS6
ビジュアル Web 開発ツール

SublimeText3 Mac版
神レベルのコード編集ソフト(SublimeText3)

ホットトピック









Java の乱数ジェネレーターのガイド。ここでは、Java の関数について例を挙げて説明し、2 つの異なるジェネレーターについて例を挙げて説明します。

Java の Weka へのガイド。ここでは、weka java の概要、使い方、プラットフォームの種類、利点について例を交えて説明します。

この記事では、Java Spring の面接で最もよく聞かれる質問とその詳細な回答をまとめました。面接を突破できるように。

Java 8は、Stream APIを導入し、データ収集を処理する強力で表現力のある方法を提供します。ただし、ストリームを使用する際の一般的な質問は次のとおりです。 従来のループにより、早期の中断やリターンが可能になりますが、StreamのForeachメソッドはこの方法を直接サポートしていません。この記事では、理由を説明し、ストリーム処理システムに早期終了を実装するための代替方法を調査します。 さらに読み取り:JavaストリームAPIの改善 ストリームを理解してください Foreachメソッドは、ストリーム内の各要素で1つの操作を実行する端末操作です。その設計意図はです

Java での日付までのタイムスタンプに関するガイド。ここでは、Java でタイムスタンプを日付に変換する方法とその概要について、例とともに説明します。
