스트림(Stream) = 또 다른 처리방식의 하나
객체명.stream().중간함수.중간함수.최종함수
중간함수
값을 불러오는 mapToInt : 정수를 불러옴
mapToDouble : 실수를 불러옴
조건설정(if~) filter.
정열 (sorted).Compartor...
package day27_1;
public class DTO {
String spname;
int su;
int dan;
int price;
public DTO() {}
public DTO(String spname, int su, int dan, int price) {
super();
this.spname = spname;
this.su = su;
this.dan = dan;
this.price = price;
}
public String getSpname() {
return spname;
}
public void setSpname(String spname) {
this.spname = spname;
}
public int getSu() {
return su;
}
public void setSu(int su) {
this.su = su;
}
public int getDan() {
return dan;
}
public void setDan(int dan) {
this.dan = dan;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
@Override
public String toString() {
return "DTO [spname=" + spname + ", su=" + su + ", dan=" + dan + ", price=" + price + "]";
}
}
package day27_1;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Menu {
public static void main(String[] args) throws FileNotFoundException {
Scanner sc = new Scanner(System.in);
DAO dao = new DAO();
while(true) {
System.out.println("[0]종료\t[1]입력\t[2]출력\t[3]자료건수");
System.out.println("[4]금액이 10만이상 자료건수");
System.out.println("[5]금액이 10만이상 목록");
System.out.println("[6]입력상품명 리스트");
System.out.print("원하시는 작업번호");
int n = sc.nextInt();
switch (n) {
case 0:System.exit(0);break;
case 1:dao.insert();break;
case 2:dao.out();break;
case 3:dao.count();break;
case 4:dao.forcount();break;
case 5:dao.onotherlist();break;
case 6:dao.spnamelist();break;
}
}
}
}
package day27_1;
public class Process {
public int tot (int su,int dan) {
return su*dan;
}
}
package day27_1;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;
public class DAO {
enum ss {a("사과군"),b("바나나"),o("오렌지"),m("망고군");
private String value;
private ss(String value) {
this.value = value;
}
public String getValue() {
return value;
}
}
ArrayList<DTO> list = new ArrayList<DTO>();
public void insert() throws FileNotFoundException {
FileReader fr = new FileReader("c:/data/01101.txt");
BufferedReader br= new BufferedReader(fr);
Process pr = new Process();
while(true) {
try {
String str = br.readLine();
if(str==null)break;
String name = ss.valueOf(str.substring(0, 1)).getValue();
int su = Integer.parseInt(str.substring(1, 3));
int dan = Integer.parseInt(str.substring(3, 7));
int price = pr.tot(su, dan);
list.add(new DTO(name, su, dan, price));
// //세터 입력방식
// DTO dto = new DTO();
// dto.setSpname(ss.valueOf(str.substring(0, 1)).getValue());
// dto.setSu(Integer.parseInt(str.substring(1, 3)));
// dto.setDan(Integer.parseInt(str.substring(3, 7)));
// dto.setPrice(pr.tot(dto.getSu(), dto.getDan()));
// list.add(dto);
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void out() {
System.out.println("==람다식출력==");
list.forEach(s->System.out.println(s.toString()));
System.out.println("==빠른 for==");
for(DTO s:list) {
System.out.println(s.toString());
}
}
//count 기능 .. 전체 자료의 건수
public void count() {
long su = list.stream().count(); // 자료의 갯수
System.out.println("자료의 건수 : "+su+"건");
}
//금액이 10만 이상인것을 찾아냄
public void forcount() {
long ks1 = list.stream().filter(s->s.getPrice()>=100000).count();
System.out.println("10만 이상의 자료의 건수 : "+ks1+"건");
}
public void forcount2() {
long ks2 = list.stream().filter(s->s.getDan()>=2000).count();
System.out.println("단가가 2000원 이상의 건수 : "+ks2+"건");
}
public void onotherlist() {
List<DTO> newlist = list.stream().filter(s->s.getPrice()>=100000).collect(Collectors.toList());
newlist.forEach(s->System.out.println(s.toString()));
}
public void spnamelist() {
System.out.println("상품명을 입력하시오");
Scanner sc = new Scanner(System.in);
String str = sc.next();
List<DTO> splist = list.stream().filter(s->s.getSpname().equals(str)).collect(Collectors.toList());
splist.forEach(s->System.out.println(s.toString()));
}
}
package day27_1;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Menu {
public static void main(String[] args) throws FileNotFoundException {
Scanner sc = new Scanner(System.in);
DAO dao = new DAO();
while(true) {
System.out.println("[0]종료\t[1]입력\t[2]출력");
System.out.println("[3]수량,단가,금액의 합");
System.out.println("[4]수량,단가,금액의 평균");
System.out.print("원하시는 작업번호");
int n = sc.nextInt();
switch (n) {
case 0:System.exit(0);break;
case 1:dao.insert();break;
case 2:dao.out();break;
case 3:dao.suhap();break;
case 4:dao.suavg();break;
}
}
}
}
package day27_1;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;
public class DAO {
enum ss {a("사과군"),b("바나나"),o("오렌지"),m("망고군");
private String value;
private ss(String value) {
this.value = value;
}
public String getValue() {
return value;
}
}
ArrayList<DTO> list = new ArrayList<DTO>();
DecimalFormat df = new DecimalFormat("#,##0.0");
public void insert() throws FileNotFoundException {
FileReader fr = new FileReader("c:/data/01101.txt");
BufferedReader br= new BufferedReader(fr);
Process pr = new Process();
while(true) {
try {
String str = br.readLine();
if(str==null)break;
String name = ss.valueOf(str.substring(0, 1)).getValue();
int su = Integer.parseInt(str.substring(1, 3));
int dan = Integer.parseInt(str.substring(3, 7));
int price = pr.tot(su, dan);
list.add(new DTO(name, su, dan, price));
// //세터 입력방식
// DTO dto = new DTO();
// dto.setSpname(ss.valueOf(str.substring(0, 1)).getValue());
// dto.setSu(Integer.parseInt(str.substring(1, 3)));
// dto.setDan(Integer.parseInt(str.substring(3, 7)));
// dto.setPrice(pr.tot(dto.getSu(), dto.getDan()));
// list.add(dto);
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void out() {
System.out.println("==람다식출력==");
list.forEach(s->System.out.println(s.toString()));
}
//수량의 합
public void suhap() {
int suhap = list.stream().mapToInt(DTO::getSu).sum();
int danhap = list.stream().mapToInt(DTO::getDan).sum();
int prhap = list.stream().mapToInt(DTO::getPrice).sum();
System.out.println("수량의 합 : "+suhap);
System.out.println("단가의 합 : "+danhap);
System.out.println("금액의 합 : "+prhap);
}
public void suavg() {
double suavg = list.stream().mapToInt(DTO::getSu).average().getAsDouble();
double danavg = list.stream().mapToInt(DTO::getDan).average().getAsDouble();
double pravg = list.stream().mapToInt(DTO::getPrice).average().getAsDouble();
System.out.println("수량의 평균 : "+df.format(suavg));
System.out.println("단가의 평균 : "+df.format(danavg));
System.out.println("금액의 평균 : "+df.format(pravg));
}
}
package day27_1;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeMap;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class DAO {
enum ss {a("사과군"),b("바나나"),o("오렌지"),m("망고군");
private String value;
private ss(String value) {
this.value = value;
}
public String getValue() {
return value;
}
}
ArrayList<DTO> list = new ArrayList<DTO>();
DecimalFormat df = new DecimalFormat("#,##0.0");
public void insert() throws FileNotFoundException {
FileReader fr = new FileReader("c:/data/01101.txt");
BufferedReader br= new BufferedReader(fr);
Process pr = new Process();
while(true) {
try {
String str = br.readLine();
if(str==null)break;
String name = ss.valueOf(str.substring(0, 1)).getValue();
int su = Integer.parseInt(str.substring(1, 3));
int dan = Integer.parseInt(str.substring(3, 7));
int price = pr.tot(su, dan);
list.add(new DTO(name, su, dan, price));
// //세터 입력방식
// DTO dto = new DTO();
// dto.setSpname(ss.valueOf(str.substring(0, 1)).getValue());
// dto.setSu(Integer.parseInt(str.substring(1, 3)));
// dto.setDan(Integer.parseInt(str.substring(3, 7)));
// dto.setPrice(pr.tot(dto.getSu(), dto.getDan()));
// list.add(dto);
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void out() {
System.out.println("==람다식출력==");
list.forEach(s->System.out.println(s.toString()));
}
//수량의 합
public void suhap(Scanner sc) {
System.out.print("원하는 상품명을 입력하시오");
String sname = sc.next();
int suhap = list.stream().filter(s->s.getSpname().equals(sname)).mapToInt(DTO::getSu).sum();
int danhap = list.stream().filter(s->s.getSpname().equals(sname)).mapToInt(DTO::getDan).sum();
int prhap = list.stream().filter(s->s.getSpname().equals(sname)).mapToInt(DTO::getPrice).sum();
System.out.println("수량의 합 : "+suhap);
System.out.println("단가의 합 : "+danhap);
System.out.println("금액의 합 : "+prhap);
}
public void suavg() {
double suavg = list.stream().mapToInt(DTO::getSu).average().getAsDouble();
double danavg = list.stream().mapToInt(DTO::getDan).average().getAsDouble();
double pravg = list.stream().mapToInt(DTO::getPrice).average().getAsDouble();
System.out.println("수량의 평균 : "+df.format(suavg));
System.out.println("단가의 평균 : "+df.format(danavg));
System.out.println("금액의 평균 : "+df.format(pravg));
}
//전체상품에 대한 금액의 합
public void totalhap() {
TreeMap<String, Integer> map = new TreeMap<String, Integer>();
for(int i=0;i<list.size();i++) {
map.put(list.get(i).getSpname(), list.get(i).getPrice());
}
//map구조에서 정의 .. 문자열(키)과 값중에서 키값만 모음
Set<String> kset = map.keySet(); //상품명 4가지만..
//키값들은 세트로 구성--> 낱개로 분리
Iterator<String> it = kset.iterator();//위의 세트에서 낱개로 구분
while(it.hasNext()) {
String spname = it.next();
int hap = list.stream().filter(s->s.getSpname().equals(spname)).mapToInt(DTO::getPrice).sum();
System.out.println(spname+"의 총금액은 : "+hap);
}
}
//이름을 오름차순으로 정렬
public void spnameAscSort() {
List<DTO> sortlist = list.stream().sorted(Comparator.comparing(DTO::getSpname)).collect(Collectors.toList());
System.out.println("상품명 오름차순 출력");
sortlist.forEach(s->System.out.println(s.toString()));
}
//이름을 내림차순으로 정렬
public void spnamedescSort() {
List<DTO> desclist = list.stream().sorted(Comparator.comparing(DTO::getSpname).reversed()).collect(Collectors.toList());
System.out.println("상품명 내림차순 출력");
desclist.forEach(s->System.out.println(s.toString()));
}
//상품명으로 오름..금액 오름차순 정렬
public void ascasc() {
// Stream<DTO> ascasc2 = list.stream().sorted(Comparator.comparing(DTO::getSpname).thenComparing(Comparator.comparing(DTO::getPrice)));
// ascasc2.forEach(s->System.out.println(s.toString()));
List<DTO> ascasc = list.stream().sorted(Comparator.comparing(DTO::getSpname).thenComparing(Comparator.comparing(DTO::getPrice))).collect(Collectors.toList());
ascasc.forEach(s->System.out.println(s.toString()));
}
//상품명은 오름..금액은 내림차순 정렬
public void ascdesc() {
// Stream<DTO> ascdesc2 = list.stream().sorted(Comparator.comparing(DTO::getSpname).thenComparing(Comparator.comparing(DTO::getPrice).reversed()));
// ascdesc2.forEach(s->System.out.println(s.toString()));
List<DTO> ascdesc = list.stream().sorted(Comparator.comparing(DTO::getSpname).thenComparing(Comparator.comparing(DTO::getPrice).reversed())).collect(Collectors.toList());
ascdesc.forEach(s->System.out.println(s.toString()));
}
//상품명은 내림..금액은 오름차순 정렬
public void descasc() {
List<DTO> descasc = list.stream().sorted(Comparator.comparing(DTO::getSpname).reversed().thenComparing(DTO::getPrice)).collect(Collectors.toList());
descasc.forEach(s->System.out.println(s.toString()));
}
//상품명은 내림..금액은 내림차순 정렬
public void descdesc() {
List<DTO> descdesc = list.stream().sorted(Comparator.comparing(DTO::getSpname).reversed().thenComparing(Comparator.comparing(DTO::getPrice).reversed())).collect(Collectors.toList());
descdesc.forEach(s->System.out.println(s.toString()));
}
//Map구조와 클래스 연결..
public void mapclass(Scanner sc) {
System.out.println("상품명을 입력하시오");
String name = sc.next();
Map<String, List<DTO>> mapa = list.stream().collect(Collectors.groupingBy(DTO::getSpname));
mapa.get(name).forEach(s->System.out.println(s.toString()));
}
//전체상품명에 대한 DTO출력
public void maptot() {
Map<String, List<DTO>> mapa = list.stream().collect(Collectors.groupingBy(DTO::getSpname));
Set<String> kset = mapa.keySet();
Iterator<String> it = kset.iterator();
while(it.hasNext()) {
String name = it.next();
mapa.get(name).forEach(s->System.out.println(s.toString()));
}
}
//각 상품명에 대한 건수
public void namecount() {
Map<String, List<DTO>> mapa = list.stream().collect(Collectors.groupingBy(DTO::getSpname));
Set<String> kset = mapa.keySet();
Iterator<String> it = kset.iterator();
while(it.hasNext()) {
String name = it.next();
long su = list.stream().filter(s->s.getSpname().equals(name)).count();
System.out.println(name+"의 건수 : "+su+"건");
}
}
}
'JAVA' 카테고리의 다른 글
230111_Stream (0) | 2023.01.11 |
---|---|
230109_Stream (0) | 2023.01.09 |
230106_Map (0) | 2023.01.06 |
230105_Map (0) | 2023.01.05 |
230104_Map (0) | 2023.01.04 |
댓글