저장소 : GitHub Repositories leeconomy1121/java-study
App class
package string_array;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;
public class App {
public static void main(String[] args) {
// 리스트 => .stream()
// 리스트가 아닐 경우
// 1. Stream.of(1, 2, 3) 정수형 스트림, Stream.of("하나", "둘") 문자열 스트림
// 2. 배열일 경우 Array.stream(배열 입력)
Stream.of("Hello", "Bottle", "Africa")
.sorted() // 정렬 : 알파벳순
.findFirst() // 첫 번째
.ifPresent(x -> System.out.println(x));
String[] items = {"치킨", "원숭이", "고릴라", "치약", "고라니", "치과", "고양이"};
Stream.of(items) // 배열을 입력해서 스트림 생성
.filter(s -> s.startsWith("치")) // filter는 참이면 남기고 거짓이면 제거한다.
.forEach(x -> System.out.print(x + ", "));
System.out.println();
int[] numbers = {2, 4, 6, 8, 10};
Arrays.stream(numbers)
.map(n -> n * n) // 제곱
.average() // 평균, max(), min()
.ifPresent(n -> System.out.print(n));
System.out.println();
// 배열을 리스트로
List<String> list = Arrays.asList(items);
list.stream()
.filter(x -> x.startsWith("고")) // 고릴라, 고라니, 고양이
.sorted() // 고라니, 고릴라, 고양이 (정렬)
.forEach(x -> System.out.print(x + ", "));
System.out.println();
int[] nums = {9, 4, 6, 3, 1, 2, 7};
Arrays.stream(nums)
.filter(n -> n > 3) // 9, 4, 6, 7
.sorted() // 4, 6, 7, 9
.map(s -> s * 10) // 40, 60, 70, 90
.forEach(n -> System.out.println(n));
}
}
📝결과
App class
package numbering;
public class App {
public static void main(String[] args) {
/*
* 10진수 decimal 0-9
* 2진수 binary 0-1 (컴퓨터)
* 16진수 hexadecimal 0-F
*
* 1234567890
* 0, 1, 10, 11, 100, 111, 1000 => 2진수
* A0, FB, CD, FFFF, FF00 (16진수)
*/
// 16진수 리터럴(값) 표시는 0x 2진수는 0b
int val = 0b11111111;
System.out.println("10진수 : " + val);
System.out.println(" 2진수 : " + Integer.toBinaryString(val)); // 10진수 정수를 => 2진수 표현
System.out.println("16진수 : " + Integer.toHexString(val));
}
}
📝결과
App class
package operators;
public class XOR_NOT {
public static void main(String[] args) {
// 비트 XOR, NOT 연산
int v1 = 0b01010011;
int v2 = 0b11010110;
int result1 = v1 ^ v2; // XOR(^) 비트 연산 : 같으면 0 다르면 1
System.out.println(toBinary(v1));
System.out.println(toBinary(v2));
System.out.println(toBinary(result1));
System.out.println();
// NOT(~) 비트 연산 : 1 -> 0, 0 -> 1
System.out.println(toBinary(~result1));
}
public static String toBinary(int value) { // 이진수 문자열로 출력!
// String.format은 printf처럼 사용하면 문자열로 리턴된다.
return String.format("%8s", Integer.toBinaryString(value)).replace(" ", "0");
}
}
📝결과
NOT(~) 결과값은 뒤에서 8개의 숫자 01111010
App class
package operators;
public class SHIFT {
public static void main(String[] args) {
// 비트 shift >>, << 연산
int value = 0b00010001;
System.out.println(toBinary(value));
int result1 = value << 3;
System.out.println(toBinary(result1));
int result2 = result1 >> 3;
System.out.println(toBinary(result2));
System.out.println(100 << 3); // 곱하기 값이 2^n 즉 2* 2
System.out.println(100 >> 2); // 나누기 2 * 2
}
public static String toBinary(int value) { // 이진수 문자열로 출력!
// String.format은 printf처럼 사용하면 문자열로 리턴된다.
return String.format("%8s", Integer.toBinaryString(value)).replace(" ", "0");
}
}
📝결과