언어/자바
-
List.of와 NullPointExecption언어/자바 2024. 3. 27. 08:14
자바에서 List.of를 사용해서 간편하게 List를 잘 만들어서 사용하고 있습니다. 모든 요소가 null 인 경우를 찾기 위해서 아래와 같은 방법을 사용하려고 했는데 public boolean isEmpty() { List checkList = List.of(title, requirements, preferred, stack, icon, status); return checkList.stream().noneMatch(Objects::nonNull); } 바로 NullPointException이 발생합니다. 잉? 자바 문서를 찾아보니 Null을 넣을 수 없다고 떡하니 적혀있습니다. (공식 문서) 대안을 찾아보니 Arrays.asList를 사용할 수 있을 것 같습니다. public boolean isEmp..
-
List 정렬에 대하여언어/자바 2024. 1. 12. 01:06
List를 정렬하는 여러가지 방법들을 정리해본다. Collections.sort 에서부터 stream.sorted까지 살펴본다. 글의 흐름은 Head First Java를 기초로 하고 있다. 정렬하는 방법들 1. Collections.sort(List) List인 경우만 정렬이 가능하다. /* 배열은 정렬 불가 */ String[] sArr = {"z", "b", "k"}; Collections.sort(sArr); // error /* List로 변경해서 정렬 */ String[] sArr = {"z", "b", "k"}; List sList = Arrays.asList(sArr); System.out.println("b4" + sList); // z,b,k Collections.sort(sList);..
-
Java IO <7 - Writer>언어/자바 2023. 12. 25. 23:30
막연하게만 사용하던 I/O를 조금 더 이해해보고자 작성한 글입니다. 기본적으로 I/O는 O/S 레벨에서 이루어지고 byte를 다룬다는 것을 생각야해합니다. 자바 I/O 관련 마지막 글입니다 Writer Reader 클래스와 비슷하게 OutputStream을 String기반으로 사용할 수 있도록 하는 클래스이다. Writer = OutputStream + String 핸들링 1.FileWriter FileWriter를 사용하면 기존에 String을 byte[]로 변경해서 write작업을 수행하던 것을 하지않고 곧 바로 String을 사용해서 write를 할 수 있다. FileOutputStream public static void main(String[] args) throws IOException { F..
-
Java I/O <6 - Reader>통계언어/자바 2023. 12. 25. 23:25
막연하게만 사용하던 I/O를 조금 더 이해해보고자 작성한 글입니다. 기본적으로 I/O는 O/S 레벨에서 이루어지고 byte를 다룬다는 것을 생각야해합니다. Reader byte를 다루는 InputStream과 달리 Reader 클래스는 String을 다룰 때 사용되는 클래스이다. Reader클래스들은 InputStream을 내부에서 사용하거나 랩핑하여 사용한다. Reader = InputStream + String 핸들링 1. FileReader 영문을 입력할 때와 한글을 읽을 때를 각각 살펴보자. // test.txt this is a test sentence 한글 테스트 입니다. public static void main(String[] args) throws IOException { FileInpu..
-
Java I/O <5 - Buffered Stream>언어/자바 2023. 12. 25. 23:19
막연하게만 사용하던 I/O를 조금 더 이해해보고자 작성한 글입니다. 기본적으로 I/O는 O/S 레벨에서 이루어지고 byte를 다룬다는 것을 생각야해합니다. Buffered I/O Stream 앞에서 다루었던 I/O Stream들을 보다 효율적으로 다루기 위한 방법이다. 버퍼를 사용할 때와 안할 때의 차이를 왜 효율적인지 생각해보자. 동기식 I/O의 동작 방식 핵심은 파일이나 네트워크를 다루는 I/O는 CPU연산에 비해 엄청나게 느리기 때문에 적게 할수록 효율적이다는 것이다. 위 그림을 보면 1. InputStream은 매번 read()가 호출될 때마다 운영체제 혹은 디스크 레벨까지 접근을 하게 된다. 하지만 2번 BufferedInputStream 같은 경우에는 일단 디스크에서 버퍼 크기만큼의 데이터를 ..
-
Java I/O <4 - Network Stream>언어/자바 2023. 12. 25. 23:16
막연하게만 사용하던 I/O를 조금 더 이해해보고자 작성한 글입니다. 기본적으로 I/O는 O/S 레벨에서 이루어지고 byte를 다룬다는 것을 생각야해합니다. 네트워크를 왜 I/O로 분류하는가를 이해해보자. Socket과 I/O Stream 쓰레드를 사용하지 않고, socket에서 제공하는 Input/Output Stream을 사용해서 간단하게 메세지를 console에서 확인해보자. 1. 클라이언트에서 서버로 메세지를 보내보자 우선 원하는 포트를 지정해서 응답을 기다리는 서버를 만든다. 서버에 도착한 stream을 console에 출력해보자. // server public static void main(String[] args) { ServerSocket serverSocket = new ServerSock..
-
Java I/O <3 - FileStream>언어/자바 2023. 12. 25. 23:10
막연하게만 사용하던 I/O를 조금 더 이해해보고자 작성한 글입니다. 기본적으로 I/O는 O/S 레벨에서 이루어지고 byte를 다룬다는 것을 생각야해합니다. FileOutputStream 앞에서 다루었던 System클래스 속의 stream들은 콘솔을 기반으로 I/O를 수행했다면, 이번에 다루는 FileStream들은 파일을 기반으로 I/O를 수행한다. 1. 파일 쓰기 - 덮어쓰기 FileOutputStream 클래스에 전달된 경로에 파일이 없는 경우, 파일이 생성되고, 있는 경우는 해당 경로의 파일을 이용한다. public static void main(String[] args) throws IOException { FileOutputStream fileOutputStream = new FileOutput..
-
Java I/O <2 - InputStream>언어/자바 2023. 12. 23. 04:05
막연하게만 사용하던 I/O를 조금 더 이해해보고자 작성한 글입니다. 기본적으로 I/O는 O/S 레벨에서 이루어지고 byte를 다룬다는 것을 생각야해합니다. 글 작성에 참고한 영상입니다. Java IO - Output Streams by Kody Simpson InputStream 기본적으로 들어온 입력을 byte로 변환해주는 역할을 한다. System.in의 read 메소드를 사용해서 콘솔에서 받은 입력을 어떻게 보여주는지 확인해보자. 1. read() 입력된 스트림에서 1byte를 읽어서 int로 된 결과를 반환한다. - 영문 입력 (UTF-8 기준 1Byte 입력) class TestInputStream { public static void main(String[] args) { InputStream..