학생성적리스트 : (학생이름:점수) 출력 / 점수 합계 출력
import java.util.ArrayList;
import java.util.stream.Stream;
public class StreamEx03 {
public static void main(String[] args) {
// ArrayList<Student> list 생성 후
//list.add() 학생명,점수 추가
//list로 스트림 생성 후
//학생의 이름과 점수를 출력
//점수 합계 출력
ArrayList<Student> list = new ArrayList<>();
list.add(new Student("정은",100));
list.add(new Student("희은",90));
list.add(new Student("다래",80));
list.add(new Student("동글",70));
list.add(new Student("방글",60));
//이름:점수 출력
//1.toString이 있는 경우
Stream<Student> s = list.stream();
s.forEach(n->System.out.println(n));
//2.toString이 없는 경우
//{} : 처리구문이 많은 경우
list.stream().forEach(h->{
String name = h.getName();
int score = h.getScore();
System.out.println(name+": "+score);
});
//총 합계
//getScore() => Integer => int 변환
//추출 -> map, 추출 후 int 변환 => mapToInt
int sum = list.stream().mapToInt(b->b.getScore()).sum();
System.out.println("총 합계: " + sum);
//총 인원
System.out.println("총 인원: " + list.stream().count());
//점수가 70점 이상인 친구들의 합계
System.out.println("--점수가 70점 이상인 친구들--");
list.stream().filter(n->n.getScore()>=70).forEach(System.out::println);
System.out.println("= 70점 이상인 친구들의 합계");
int sum1 = list.stream().mapToInt(h->h.getScore()).filter(n->n>=70).sum();
System.out.println(sum1);
}
}
학생 명단기초
public class Student {
//name, score
//생성자, getter/setter
private String name;
private int score;
//생성자
public Student() {}
public Student(String name, int score) {
super();
this.name = name;
this.score = score;
}
//getter,setter
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
@Override
public String toString() {
return name + " : " + score;
}
}
'JAVA > java 코드' 카테고리의 다른 글
[JAVA] 여행고객명단 : 명단출력 / 비용계산 / 조건에 맞는 명단만 출력 (1) | 2023.09.12 |
---|---|
[JAVA] 배열에서 짝수만 출력 + 중복 값은 삭제 + 새로운 배열에 저장 (0) | 2023.09.12 |
[JAVA] 정렬 + 이름의 수가 5개 이상 출력 + 각 이름의 글자 수 출력 (0) | 2023.09.12 |
[JAVA] 성적 리스트에서 70점 이상만 합계 구하기 (0) | 2023.09.12 |
[JAVA] 성적 배열 a에서 70점 이상만 합계 구하기 (0) | 2023.09.12 |