[집합] Collections 도구 클래스 사용 팁 요약
2025 단어 컬렉션Collections도구류
요소의 정렬, 조회, 수정 등 조작을 제공하고 집합 대상을 불가변류로 설정하며 집합 대상에 대해 동기화 제어를 실현한다.
import java.util.*;
public class TestCol {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ArrayList al = new ArrayList();
al.addAll(a1,"9","3","-3","0","0","5");
al.add(1);
al.add(-2);
al.add(7);
System.out.println(al);
// 、
System.out.println(Collections.max(al));
System.out.println(Collections.min(al));
//
Collections.replaceAll(al, 0, 1);
System.out.println(al);
// -5
System.out.println(Collections.frequency(al, -5));
//
Collections.sort(al);
System.out.println(al);
//
System.out.println(Collections.binarySearch(al, -5));
}
}
및 copy 방법:List<String> list1 = new ArrayList<String>();
Collections.addAll(list1, "1", "2", "3", "4");
List<String> list2 = new ArrayList<String>();
Collections.addAll(list2, "a", "b", "c");
Collections.copy(list2, list1);
comparable 방법:
List<String> list1 = new ArrayList<String>();
Collections.addAll(list1, "5", "3", "2", "4");
Collections.sort(list1);
System.out.println(list1);
결과 출력:
[2, 3, 4, 5]
shuffle 방법: 집합 중의 원소의 위치를 무작위로 흐트러뜨리기
동기화 제어:
public class TestSynchronized {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Collection c = Collections.synchronizedCollection(new ArrayList());
List l = Collections.synchronizedList(new ArrayList());
Set s = Collections.synchronizedSet(new HashSet());
Map m = Collections.synchronizedMap(new HashMap());
}
}
이상은 제가 자바의 세 번째 총결산을 진행할 때 정리한 자료를 찾았습니다. 총결산을 해 봤는데 이 유형은 조작 집합을 할 때 정말 편리합니다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Scala의 ListMap은 삽입 순서를 유지하지 않습니다.ListMap(mutable인)은 이름적으로 순서를 보관 유지한 Map를 만들어 준다고 생각해 구현하고 있었습니다만, 사실은 아니었기 때문에 공부를 위해 구현을 쫓아보기로했습니다. ※ 아직 Scala 처음으로 일주일...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.