Java/Collection(컬렉션)
-
[JAVA] 배열 검색하기Java/Collection(컬렉션) 2024. 9. 17. 16:52
목차 자바에서는 배열을 검색하는 다양한 방법이 있습니다. 이번 문서에서는 배열을 검색하는 여러 가지 방법에 대해 설명하겠습니다. 주요 메서드로는 순차 검색, 이진 검색, 라이브러리 메서드를 사용한 검색, 그리고 2차원 배열에서 값의 존재 여부를 확인하는 방법이 있습니다. 추가로 배열에서 최대 및 최소값을 찾는 방법도 설명하겠습니다. 1. 순차 검색순차 검색을 사용하여 배열에서 특정 값을 찾는 방법입니다. 배열의 첫 번째 요소부터 시작하여 찾아야 하는 값을 순차적으로 비교합니다./** * 순차 검색을 사용하여 배열에서 특정 값을 찾음 * * @param array 검색할 배열 * @param value 검색할 값 * @return 배열에서 값의 인덱스 (존재하지 않으면 -1) */public int lin..
-
[JAVA] 배열 정렬하기 : 오름차순, 내림차순Java/Collection(컬렉션) 2024. 9. 17. 16:52
목차 자바에서는 배열을 정렬하는 다양한 방법이 있습니다. 이번 문서에서는 Arrays.sort, Arrays.parallelSort 메서드 등을 사용하여 배열을 오름차순 및 내림차순으로 정렬하는 방법을 기본과 병렬 방식으로 구분하여 설명하겠습니다. 1. 정수 배열을 오름차순 정렬Arrays.sort 메서드를 사용하여 정수 배열을 오름차순으로 정렬합니다./** * Arrays.sort 메소드를 사용하여 배열을 오름차순으로 정렬 * * @param arr 정렬할 배열 * @return 정렬된 배열 */public int[] arraySortAscending(int[] arr) { Arrays.sort(arr); return arr;}단위 테스트@Order(1)@DisplayName("arraySortAs..
-
[Java] 배열(Array)을 리스트(List)로 변환 - 반복문, Arrays, Collection, StreamJava/Collection(컬렉션) 2023. 5. 6. 17:36
목차 배열(Array)을 리스트(List)로 변환배열을 리스트로 변환하는 방법은 반복문을 사용하거나 Arrays, Collection, Stream 클래스에세 제공하는 메서드를 사용하여 변환할 수 있습니다. 1. 반복문을 사용빈 리스트를 생성한 후, 배열의 모든 요소를 리스트에 추가하는 방법입니다. Integer, Double, String 등 여러 타입의 배열을 리스트로 변환하는 경우 제네릭 타입의 메서드를 정의하여 코드의 양을 줄일 수 있습니다.import java.util.ArrayList;import java.util.Arrays;import java.util.List;/** * 배열(Array)을 리스트(List)로 변환 - 반복문, Arrays, Collection, Steram * * 배열을..
-
[Java] List 중복 제거 (HashSet, TreeSet, Stream)Java/Collection(컬렉션) 2019. 12. 16. 13:50
목차 List의 중복을 제거하는 방법List에 중복을 제거하기 위한 방법으로는 반복문을 통해 데이터를 확인하여 중복된 요소를 제거, 중복을 허용하지 않는 set 인터페이스를 사용하여 중복된 요소를 제거, 중복된 요소가 제거된 새로운 스트림을 반환하는 distinct() 메서드를 사용하는 방법 등이 있습니다. 1. List의 데이터를 확인하여 중복을 제거 public void getDistinctLogic() { List originList = new ArrayList(); originList.add("1"); originList.add("1"); originList.add("가나다"); originList.add("가나다"); originList.add("ABC"); originList.add("ABC..
-
[Java] Map 전체 출력(entrySet, keySet, Iterator, Lambda, Stream)Java/Collection(컬렉션) 2018. 12. 27. 14:55
목차 Map 객체의 내용을 출력하기 위한 방법1. entrySet()entrySet() 메서드는 Map에서 모든 Entry(Key-Value 쌍)를 가져와 Set 객체로 반환합니다. foreach 루프를 사용하여 각 Entry에 순차적으로 접근할 수 있습니다.Map map = new HashMap();map.put("key01", "value01");map.put("key02", "value02");map.put("key03", "value03");map.put("key04", "value04");map.put("key05", "value05");// 방법 01 : entrySet()for (Map.Entry entry : map.entrySet()) { System.out.println("[key]:"..