๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Today I Learned(TIL)/์ŠคํŒŒ๋ฅดํƒ€ ๋‚ด์ผ๋ฐฐ์›€์บ ํ”„

ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๊ธฐ์ดˆ_Day 13

by carrot0911 2024. 11. 23.

์˜ค๋Š˜ ํ•œ ๊ณต๋ถ€๐Ÿง

  • Java ๋ฌธ๋ฒ• ์ข…ํ•ฉ๋ฐ˜_5์ฃผ์ฐจ ๊ฐ•์˜ ์ˆ™์ œ

 

์˜ค๋Š˜ ์–ป์€ ๋‚ด์šฉ ์ •๋ฆฌโœ๏ธ

5์ฃผ์ฐจ ๊ฐ•์˜ ์ˆ™์ œ ๋‚ด์šฉ - ์ฑ… ๋ถ„๋ฅ˜ํ•ด์„œ ์กฐํšŒํ•˜๊ธฐ

  • ๋จผ์ € ์˜ˆ์‹œ ์ฝ”๋“œ๋ฅผ ์ธํ…”๋ฆฌ์ œ์ด์— ์„ธํŒ…ํ•ด์ค€๋‹ค.
๋”๋ณด๊ธฐ
  • Main.java
package homework05;

import java.util.Arrays;
import java.util.List;
import java.util.ArrayList;

public class Main {
    public static void main(String[] args) {
        List<Book> bookList = Arrays.asList(
                new Book(1L, "๋ชจ๋‘์˜ ๋”ฅ๋Ÿฌ๋‹", "์กฐํƒœํ˜ธ", "IT", 21600),
                new Book(2L, "์ด๋“์šฐ์˜ ๊ฒŒ์ž„ ์ˆ˜ํ•™", "์ด๋“์šฐ", "IT", 40500),
                new Book(3L, "์ž๋ฐ” ์›น ๊ฐœ๋ฐœ ์›Œํฌ๋ถ", "๊ตฌ๋ฉ๊ฐ€๊ฒŒ ์ฝ”๋”ฉ๋‹จ", "IT", 31500),
                new Book(4L, "์‹ค์ „ ์‹œ๊ณ„์—ด ๋ถ„์„", "์—์ผ๋ฆฐ ๋‹์Šจ", "IT", 34200),
                new Book(5L, "๋ฐ์ดํ„ฐ ๋ถ„์„๊ฐ€์˜ ์ˆซ์ž์œ ๊ฐ", "๊ถŒ์ •๋ฏผ", "IT", 14400),
                new Book(6L, "์Šคํ”„๋ง ๋ถ€ํŠธ ์‹ค์ „ ํ™œ์šฉ ๋งˆ์Šคํ„ฐ", "๊ทธ๋ ‰ ํ„ดํ‚ค์ŠคํŠธ", "IT", 25200),
                new Book(7L, "์˜ค๋Š˜๋ถ€ํ„ฐ IT๋ฅผ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค", "๊ณ ์ฝ”๋”", "IT", 16200),
                new Book(8L, "๊ทธ๋ฆผ์œผ๋กœ ์ดํ•ดํ•˜๋Š” ์ธ์ง€๊ณผํ•™", "๊ธฐํƒ€ํ•˜๋ผ ์š”์‹œ๋…ธ๋ฆฌ", "IT", 16200),
                new Book(9L, "๊ดœ์ฐฎ์•„, ๊ทธ ๊ธธ ๋์— ํ–‰๋ณต์ด ๊ธฐ๋‹ค๋ฆด ๊ฑฐ์•ผ", "์†๋ฏธ๋‚˜", "์—ฌํ–‰", 17100),
                new Book(10L, "์—ฌํ–‰์˜ ์ด์œ ", "๊น€์˜ํ•˜", "์—ฌํ–‰", 12150),
                new Book(11L, "์—ฌํ–‰์˜ ์‹œ๊ฐ„", "๊น€์ง„์• ", "์—ฌํ–‰", 16200),
                new Book(12L, "๋กœ๋ด‡ ์‹œ๋Œ€ ์‚ด์•„๋‚จ๊ธฐ", "์—ผ๊ทœํ˜„", "์—ญ์‚ฌ", 14850),
                new Book(13L, "๊ฒฝ์ œ ์ „์Ÿ์˜ ํ‘์—ญ์‚ฌ", "์ด์™„๋ฐฐ", "์—ญ์‚ฌ", 15750),
                new Book(14L, "100๊ฐ€์ง€ ๋™๋ฌผ๋กœ ์ฝ๋Š” ์„ธ๊ณ„์‚ฌ", "์‚ฌ์ด๋จผ ๋ฐ˜์ฆˆ", "์—ญ์‚ฌ", 29700),
                new Book(15L, "k ๋ฐฐํ„ฐ๋ฆฌ ๋ ˆ๋ณผ๋ฃจ์…˜", "๋ฐ•์ˆœํ˜", "๊ฒฝ์ œ", 17100),
                new Book(16L, "์ •ํ•˜์ค€์˜ ๊ฒฝ์ œํ•™ ๋ ˆ์‹œํ”ผ", "์žฅํ•˜์ค€", "๊ฒฝ์ œ", 16200),
                new Book(17L, "๋ ˆ๋ฒ„๋ฆฌ์ง€", "๋กญ ๋ฌด์–ด", "๊ฒฝ์ œ", 16200)
        );

        // ๋ฌธ์ œ์— ํ•ด๋‹นํ•˜๋Š” ์ฝ”๋“œ ์ž‘์„ฑ ๋ถ€๋ถ„
        

    }
}
  • Book.java
package homework05;

public class Book {

    // ๋ถ„๋ฅ˜๋ฒˆํ˜ธ
    private Long id;
    // ์ฑ… ์ด๋ฆ„
    private String bookName;
    // ์ž‘๊ฐ€ ์ด๋ฆ„
    private String author;
    // ์นดํ…Œ๊ณ ๋ฆฌ
    private String category;
    // ๊ฐ€๊ฒฉ
    private double price;

    public Book(Long id, String bookName, String author, String category, double price) {
        this.id = id;
        this.bookName = bookName;
        this.author = author;
        this.category = category;
        this.price = price;
    }

    public String getBookName() {
        return bookName;
    }

    public String getCategory() {
        return category;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }


}

์กฐ๊ฑด1) ์นดํ…Œ๊ณ ๋ฆฌ๊ฐ€ ์—ฌํ–‰์ธ ์ฑ… ์ œ๋ชฉ ์กฐํšŒ

๋”๋ณด๊ธฐ
// ์นดํ…Œ๊ณ ๋ฆฌ๊ฐ€ ์—ฌํ–‰์ธ ์ฑ… ์ œ๋ชฉ ์กฐํšŒ
bookList.stream().filter(book -> book.getCategory().equals("์—ฌํ–‰"))
        .forEach(f -> System.out.println("์นดํ…Œ๊ณ ๋ฆฌ๊ฐ€ ์—ฌํ–‰์ธ ์ฑ… ์ œ๋ชฉ: " + f.getBookName()));
System.out.println();
  • filter( book -> book.getCategory( ).equals("์—ฌํ–‰"))
    • ์นดํ…Œ๊ณ ๋ฆฌ๊ฐ€ "์—ฌํ–‰"์ธ ์ฑ…์„ ๋ถ„๋ฅ˜ํ•œ๋‹ค.
  • forEach( f -> System.out.println("์นดํ…Œ๊ณ ๋ฆฌ๊ฐ€ ์—ฌํ–‰์ธ ์ฑ… ์ œ๋ชฉ : " + f.getBookName( )));
    • ์นดํ…Œ๊ณ ๋ฆฌ๊ฐ€ "์—ฌํ–‰"์ธ ์ฑ…์„ ์ถœ๋ ฅํ•œ๋‹ค.
  •  

์กฐ๊ฑด2) ๊ฐ€๊ฒฉ์ด 16,200์› ์ดํ•˜์ธ ์ฑ… ์ œ๋ชฉ ์กฐํšŒ

๋”๋ณด๊ธฐ
// ๊ฐ€๊ฒฉ์ด 16,200์› ์ดํ•˜์ธ ์ฑ… ์ œ๋ชฉ ์กฐํšŒ
bookList.stream().filter( book -> book.getPrice() <= 16200)
        .forEach( f -> System.out.println("๊ฐ€๊ฒฉ 16200์› ์ดํ•˜ ์ฑ… ์ œ๋ชฉ: " + f.getBookName()));
System.out.println();
  • filter( book -> book.getprice( ) <= 16200)
    • ๊ฐ€๊ฒฉ์ด 16,200์› ์ดํ•˜์ธ ์ฑ…์„ ๋ถ„๋ฅ˜ํ•œ๋‹ค.
  • forEach( f -> System.out.println("๊ฐ€๊ฒฉ 16,200์› ์ดํ•˜ ์ฑ… ์ œ๋ชฉ : " + f.getBookName( )))
    • ๊ฐ€๊ฒฉ์ด 16,200์› ์ดํ•˜์ธ ์ฑ…์˜ ์ œ๋ชฉ์„ ์ถœ๋ ฅํ•œ๋‹ค.

์กฐ๊ฑด3) ์ฑ… ์ œ๋ชฉ์— "๊ฒฝ์ œ”๋ผ๋Š” ์šฉ์–ด๊ฐ€ ๋“ค์–ด๊ฐ„ ์ฑ… ์ œ๋ชฉ ์กฐํšŒ

๋”๋ณด๊ธฐ
// ์ฑ… ์ œ๋ชฉ์— "๊ฒฝ์ œ"๋ผ๋Š” ์šฉ์–ด๊ฐ€ ๋“ค์–ด๊ฐ„ ์ฑ… ์ œ๋ชฉ ์กฐํšŒ
bookList.stream().filter(book -> book.getBookName().contains("๊ฒฝ์ œ"))
        .forEach(f -> System.out.println("์ฑ… ์ œ๋ชฉ์— '๊ฒฝ์ œ'๊ฐ€ ํฌํ•จ๋œ ์ฑ… ์ œ๋ชฉ: " + f.getBookName()));
System.out.println();
  • filter( book -> book.getBookName( ). contains("๊ฒฝ์ œ"))
    • ์ฑ… ์ œ๋ชฉ์— "๊ฒฝ์ œ"๊ฐ€ ํฌํ•จ๋œ ์ฑ…์„ ๋ถ„๋ฅ˜ํ•œ๋‹ค.
  • forEach(f -> System.out.println("์ฑ… ์ œ๋ชฉ์— '๊ฒฝ์ œ'๊ฐ€ ํฌํ•จ๋œ ์ฑ… ์ œ๋ชฉ : " + f.getbookName( )))
    • ์ฑ… ์ œ๋ชฉ์— "๊ฒฝ์ œ"๊ฐ€ ๋“ค์–ด๊ฐ„ ์ฑ…์˜ ์ œ๋ชฉ์„ ์ถœ๋ ฅํ•œ๋‹ค.
  •  

์กฐ๊ฑด4) ๊ฐ€๊ฒฉ์ด ๊ฐ€์žฅ ๋น„์‹ผ ์ฑ… ๊ฐ€๊ฒฉ ์กฐํšŒ

๋”๋ณด๊ธฐ
// ๊ฐ€๊ฒฉ์ด ๊ฐ€์žฅ ๋น„์‹ผ ์ฑ… ๊ฐ€๊ฒฉ ์กฐํšŒ
double maxPrice = bookList.stream().mapToDouble(Book::getPrice)
        .max().getAsDouble();
System.out.println("์ฑ… ๋ชฉ๋ก ์ค‘ ๊ฐ€์žฅ ๋น„์‹ผ ๊ธˆ์•ก: " + maxPrice);
System.out.println();
  • mapToDouble(Book::getPrice)
    • ์ฑ…์˜ ๊ฐ€๊ฒฉ์„ double ํƒ€์ž…์œผ๋กœ ๊ฐ€์ ธ์˜จ๋‹ค.
  • max( ).getAsDouble( )
    • ๊ฐ€์ ธ์˜จ ๊ฐ€๊ฒฉ ์ค‘ ์ตœ๋Œ€๊ฐ’์„ ๊ตฌํ•˜๊ณ  ์‹ค์ œ ๊ฐ’์œผ๋กœ ๊บผ๋‚ธ๋‹ค.
  • System.out.println("์ฑ… ์ œ๋ชฉ ์ค‘ ๊ฐ€์žฅ ๋น„์‹ผ ๊ธˆ์•ก : " + maxPrice)
    • ๊ฐ€์žฅ ๋น„์‹ผ ์ฑ…์˜ ์ œ๋ชฉ์„ ์ถœ๋ ฅํ•œ๋‹ค.
  •  

์กฐ๊ฑด5) ์นดํ…Œ๊ณ ๋ฆฌ๊ฐ€ IT์ธ ์ฑ…๋“ค์˜ ๊ฐ€๊ฒฉ ํ•ฉ ์กฐํšŒ

๋”๋ณด๊ธฐ
// ์นดํ…Œ๊ณ ๋ฆฌ๊ฐ€ IT์ธ ์ฑ…๋“ค ๊ฐ€๊ฒฉ ํ•ฉ ์กฐํšŒ
double sumPrice = bookList.stream().filter(book -> book.getCategory().equals("IT"))
        .mapToDouble(Book::getPrice)
        .sum();
System.out.println("์นดํ…Œ๊ณ ๋ฆฌ IT ์ฑ…๋“ค์˜ ๊ฐ€๊ฒฉ ํ•ฉ: " + sumPrice);
System.out.println();
  • filter(book -> book.getCategory( ). equals("IT"))
    • ์นดํ…Œ๊ณ ๋ฆฌ๊ฐ€ "IT"์ธ ์ฑ…์„ ๋ถ„๋ฅ˜ํ•œ๋‹ค.
  • mapToDouble(Book::getPrice)
    • ์ฑ…์˜ ๊ฐ€๊ฒฉ์„ double ํƒ€์ž…์œผ๋กœ ๊ฐ€์ ธ์˜จ๋‹ค.
  • sum( )
    • ๊ฐ€๊ฒฉ์˜ ํ•ฉ๊ณ„๋ฅผ ๊ตฌํ•œ๋‹ค.
  • System.out.println("์นดํ…Œ๊ณ ๋ฆฌ IT ์ฑ…๋“ค์˜ ๊ฐ€๊ฒฉ ํ•ฉ : " + sumPrice)
    • "IT" ์นดํ…Œ๊ณ ๋ฆฌ ์ฑ…๋“ค์˜ ๊ฐ€๊ฒฉ ํ•ฉ์„ ์ถœ๋ ฅํ•œ๋‹ค.
  •  

์กฐ๊ฑด6) IT ์ฑ… ํ• ์ธ ์ด๋ฒคํŠธ!

๋”๋ณด๊ธฐ
// IT ์ฑ… ํ• ์ธ ์ด๋ฒคํŠธ
// ์นดํ…Œ๊ณ ๋ฆฌ๊ฐ€ IT ์ธ ์ฑ…๋“ค์˜ ๊ฐ€๊ฒฉ์„ 40% ํ• ์ธํ•˜์—ฌ ์ƒˆ๋กœ์šด ์ฑ… ๋ฆฌ์ŠคํŠธ ๋งŒ๋“ค๊ธฐ, discountedBookList
List<Book> discountedBookList = bookList.stream().filter(book -> book.getCategory().equals("IT"))
        .map(book -> {
            book.setPrice(book.getPrice() * 0.6);
            return book;
        }).toList();

for (Book book : discountedBookList) {
    System.out.println("ํ• ์ธ๋œ ์ฑ… ์ œ๋ชฉ: " + book.getBookName());
    System.out.println("ํ• ์ธ๋œ ์ฑ… ๊ฐ€๊ฒฉ: " + book.getPrice() + "\n");
}
  • List<Book> discountedBookList = bookList.stream( ). filter(book -> book.getCategory( ).equals("IT"))
    • ์นดํ…Œ๊ณ ๋ฆฌ๊ฐ€ "IT"์ธ ์ฑ…์„ ๋ถ„๋ฅ˜ํ•ด์„œ discountedBookList ๋ฆฌ์ŠคํŠธ์— ์ €์žฅํ•œ๋‹ค.
  • book.setPrice(book.getprice( ) * 0.6)
    • ์ฑ…์˜ ๊ฐ€๊ฒฉ์„ 40% ํ• ์ธ๋œ ๊ฐ€๊ฒฉ์œผ๋กœ ์„ค์ •ํ•œ๋‹ค.
  • ํ–ฅ์ƒ๋œ for๋ฌธ์„ ์ด์šฉํ•ด์„œ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ˆœํšŒํ•˜๋ฉฐ ์ฑ…์˜ ์ œ๋ชฉ๊ณผ ๊ฐ€๊ฒฉ์„ ์ถœ๋ ฅํ•œ๋‹ค.

 

๋‚ด์ผ ๊ณ„ํšโฐ

  • ํ‚ค์˜ค์Šคํฌ ๊ณผ์ œ ๋„์ „!!