본문 바로가기
점프 투 자바(Java)/23년 1월

1월 26일(목) - 7장(자바 날개 달기)

by C0MPAS 2023. 1. 26.

7장 - 자바 날개 달기(7-4, 7-5, 7-6)

ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ

 

7-4 예외처리

1) finally

// 7-4 finally
public class Sample{
    public void shouldBeRun(){
        System.out.println("ok thanks.");
    }

    public static void main(String[] args){
        Sample sample =  new Sample();
        int c;
        try {
            c = 4 / 0;
            //sample.shouldBeRun();
        }catch (ArithmeticException e){
            c = -1;
        } finally{
            sample.shouldBeRun();
        }
    }
}

2) RuntimeException과 Exception

//7-4 RuntimeException 과 Exception
class FoolException extends Exception{

}
public class Sample{
    public void sayNick(String nick){
        try {
            if ("fool".equals(nick)) {
                throw new FoolException();
            }
            System.out.println("당신의 별명은 " + nick + " 입니다.");
        }catch(FoolException e){
            System.err.println("FoolException이 발생했습니다.");
        }
    }
    public static void main(String[] args){
        Sample sample = new Sample();
        sample.sayNick("fool");
        sample.sayNick("genious");
    }
}

3)

ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ

 

7-5 쓰레드

1) Thread

//7-5 Thread
public class Sample extends Thread{
    int seq;

    public Sample(int seq){
        this.seq = seq;
    }
    public void run(){
        System.out.println(this.seq + " thread start.");
        try{
            Thread.sleep(1000);
        }catch (Exception e){
        }
        System.out.println(this.seq + " thread end");
    }
    public static void main(String[] args){
        for(int i=0; i<10; i++){
            Thread t = new Sample(i);
            t.start();
        }
        System.out.println("main end.");
    }
}

2) Join

//7-5 Join
import java.util.ArrayList;
public class Sample extends Thread{
    int seq;

    public Sample(int seq){
        this.seq = seq;
    }
    public void run(){
        System.out.println(this.seq + " thread start.");
        try{
            Thread.sleep(1000);
        }catch (Exception e){
        }
        System.out.println(this.seq + " thread end");
    }
    public static void main(String[] args){
        ArrayList<Thread> threads = new ArrayList<>();
        for(int i=0; i<10; i++){
            Thread t = new Sample(i);
            t.start();
            threads.add(t);
        }
        for(int i=0; i<threads.size(); i++){
            Thread t = threads.get(i);
            try{
                t.join();
            }catch (Exception e){
            }
        }

        System.out.println("main end.");
    }
}

3) Runnable

//7-5 Runnable
import java.util.ArrayList;
import java.util.concurrent.TransferQueue;

public class Sample implements Runnable{
    int seq;

    public Sample(int seq){
        this.seq = seq;
    }
    public void run(){
        System.out.println(this.seq + " thread start.");
        try{
            Thread.sleep(1000);
        }catch (Exception e){
        }
        System.out.println(this.seq + " thread end");
    }
    public static void main(String[] args){
        ArrayList<Thread> threads = new ArrayList<>();
        for(int i=0; i<10; i++){
            Thread t = new Thread(new Sample(i));
            t.start();
            threads.add(t);
        }
        for(int i=0; i<threads.size(); i++){
            Thread t = threads.get(i);
            try{
                t.join();
            }catch (Exception e){
            }
        }

        System.out.println("main end.");
    }
}

ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ

 

7-6 함수형 프로그래밍

1) Lambda

// 7-6 함수형 프로그래밍 - Lambda
@FunctionalInterface
interface Calculator{
    int sum(int a, int b);
}
public class Sample{
    public static void main(String[] args){
        Calculator mc = (int a, int b) -> a +b;
        int result = mc.sum(3,4);
        System.out.println(result);
    }
}

2) Stream

// 7-6 Stream 적용 전
import java.util.Arrays;
import java.util.Comparator;

public class Sample{
    public static void main(String[] args){
        int[] data = {5, 6, 4, 2, 3, 1, 1, 2, 2, 4, 8};
        int[] result = Arrays.stream(data)
                .boxed()
                .filter((a) -> a%2 == 0)
                .distinct()
                .sorted(Comparator.reverseOrder())
                .mapToInt(Integer::intValue)
                .toArray()
                ;
    }
}
// 7-6 Stream 적용 후
import java.util.*;

public class Sample{
    public static void main(String[] args){
        int[] data = {5, 6, 4, 2, 3, 1, 1, 2, 2, 4, 8};

        ArrayList<Integer> dataList = new ArrayList<>();
        for(int i=0; i<data.length; i++){
            if(data[i] % 2 == 0){
                dataList.add(data[i]);
            }
        }

        HashSet<Integer> dataSet = new HashSet<>(dataList);
        ArrayList<Integer> distinctList = new ArrayList<>(dataSet);
        distinctList.sort(Comparator.reverseOrder());
        int[] result = new int[distinctList.size()];
        for(int i=0; i< distinctList.size(); i++){
            result[i] = distinctList.get(i);
        }
    }
}

ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ

댓글