Java
-
Java - String을 int로 변환하기Java 2022. 7. 15. 20:41
String 문자열을 int(Integer)로 바꾸는 방법입니다. 1. Integer.parseInt() class StringToInt { public static void main(String[] args) { String str = "123"; int num = Integer.parseInt(str); System.out.println(num); } } 결과 123 2. Integer.valueOf() class StringToInt { public static void main(String[] args) { String str = "123"; int num = Integer.valueOf(str); System.out.println(num); } } 결과 123 3. Exception 처리 cla..
-
Java - byte[]를 String으로 변환Java 2022. 7. 15. 18:10
자바에서 String을 byte[] 로 변환하여 파일에 저장하거나, 데이터로부터 byte[]를 읽어와서 String으로 변환할 수 있습니다. 1. String to byte[], byte[] to String class BytesToString { public static void main(String[] args) { String str = "string to bytes\n"; byte[] bytes = str.getBytes(); System.out.println("str: " + str); String bytesToStr = new String(bytes); System.out.println("bytes to str: " + bytesToStr); } } str.getBytes()를 이용해서 byt..
-
Java - base64 인코딩, 디코딩하기Java 2022. 7. 14. 18:57
java.util.Base64 클래스를 사용하면 base64로 인코딩, 디코딩을 쉽게 할 수 있습니다. 1. Base64 encoding 예제 "test" 라는 스트링을 base64로 인코딩하는 예제입니다. import java.util.Base64; class Base64Test { public static void main(String[] args) { String str = "test"; String encodedStr = Base64.getEncoder().encodeToString(str.getBytes()); System.out.println("encoded string: " + encodedStr); } } 결과 encoded string: dGVzdA== 2. Base64 decoding ..
-
Java - ArrayList.removeAll() 사용법 및 예제Java 2022. 7. 7. 18:22
ArrayList의 removeAll() 메소드 인자로 전달된 Collection 아이템들과 일치하는 객체를 리스트에서 삭제 1. ArrayList.removeAll() public boolean removeAll(Collection c) parameter c는 list에서 삭제할 아이템 Collection 입니다. return value는 list가 변하면 true를 반환합니다. 2. 예제 import java.util.ArrayList; class ArrayListExample { public static void main(String[] args) { ArrayList list1 = new ArrayList(); list1.add("AA"); list1.add("BB"); list1.add("CC"..
-
Java - ArrayList.retainAll() 사용법 및 예제 (리스트 교집합 구하기)Java 2022. 7. 6. 18:18
ArrayList의 retainAll(Collection) 해당 리스트에서 Collection 객체가 가지고 있는 요소를 제외한 나머지를 삭제합니다. 교집합을 구한다고 생각하면 됩니다. 공통된 부분만 남기고 전부 제거함 아래의 코드는 listA와 listB의 공통 요소(교집합)만 남기고 나머지는 listA에서 제거합니다. listA.retainAll(listB) 1. ArrayList.retainAll() 예제 import java.util.ArrayList; class ArrayListExample { public static void main(String[] args) { ArrayList list1 = new ArrayList(); list1.add("AA"); list1.add("BB"); lis..
-
Java - 1개의 element로 list 생성하기Java 2022. 6. 21. 18:36
하나의 element를 가지고 list를 생성하는 예제입니다. 1. Arrays.asList(element) String arr = "one"; List list = Arrays.asList(arr); 2. Collections.singletonList(element) List list = Collections.singletonList(new Person("john")) 차이점 Arrays.asList()로 만들어진 경우는 추가, 삭제는 안되지만 요소 속성은 변경할 수 있다. String arr = "one"; List list = Arrays.asList(arr); list.set(0, "two"); System.out.println(list); // ["two"] Collections.singleto..
-
Java - CSV 파일 읽기Java 2022. 6. 21. 18:10
public static List readFile(String filePath) throws Exception { File file = new File(filePath); BufferedReader fileReader = new BufferedReader(new FileReader(file)); CSVParser csvParser = new CSVParserBuilder() .withSeparator(';') .build(); CSVReader csvReader = new CSVReaderBuilder(fileReader) .withCSVParser(csvParser) .build(); String [] record = null; List result = new ArrayList(); while ((re..