Collection 인터페이스는 List 인터페이스와 Set 인터페이스에 의해 상속됩니다. 일반 배열과의 차이점은 고정된 크기 제한이 없으며 요소를 추가하거나 삭제할 수 있다는 것입니다.
public class List { public static void main(String[] args) { var list = new ArrayList(); } }
add()
public class List { public static void main(String[] args) { var list = new ArrayList(); list.add("a"); list.add(1); list.add(1); System.out.println(list); } }
public class List { public static void main(String[] args) { var list = new ArrayList(); list.add("a"); list.add(1); list.add(1); System.out.println(list); list.clear(); System.out.println(list); } }
지정된 인덱스에서 객체 수정 position
set ()
public class List { public static void main(String[] args) { var list = new ArrayList(); list.add("a"); list.add(1); list.add(1); System.out.println(list); list.set(2, "a"); System.out.println(list); } }
HashSet은 HashMap을 기반으로 구현되었으며 중복 요소를 허용하지 않는 집합입니다.
HashSet은 null 값을 허용합니다.
HashSet은 스레드로부터 안전하지 않습니다. 여러 스레드가 동시에 HashSet을 수정하려고 하면 최종 결과가 정의되지 않습니다. 여러 스레드에서 액세스할 때 HashSet에 대한 동시 액세스를 명시적으로 동기화해야 합니다.
ArrayList 개체 만들기:
public class Set { public static void main(String[] args) { var set = new HashSet<>(); } }
add()
public class Set { public static void main(String[] args) { var set = new HashSet<>(); set.add(1); set.add("a"); set.add("a"); System.out.println(set); } }
public class Set { public static void main(String[] args) { var set = new HashSet<>(); set.add(1); set.add("a"); set.add("a"); System.out.println(set); set.clear(); System.out.println(set); } }
public class Set { public static void main(String[] args) { var set = new HashSet<>(); set.add(1); set.add("a"); set.add("a"); System.out.println(set); set.remove("a"); System.out.println(set); } }
Generics
일반 프로그래밍(일반 프로그래밍)은 프로그래밍 언어의 스타일 또는 패러다임입니다. 제네릭을 사용하면 프로그래머는 나중에 지정되고 인스턴스화 시 매개변수로 지정되는 유형을 사용하여 강력한 유형의 프로그래밍 언어로 코드를 작성할 수 있습니다. 다양한 프로그래밍 언어, 해당 컴파일러 및 런타임 환경은 제네릭에 대해 서로 다른 지원을 제공합니다.
일반적으로 제네릭은 컬렉션을 사용할 때 사용됩니다. 제네릭은 컬렉션에 유형 제약 조건을 추가하는 것입니다. String 또는 Object일 수 있습니다. 제네릭은 기본 유형 int를 지원하지 않습니다. 제네릭을 생성하려면 래퍼 유형 Integer
를 사용하세요.
List<String> list = new ArrayList<>();
위 내용은 Java 컬렉션에서 ArrayList 및 HashSet을 사용하는 방법의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!