developer tip

컬렉션의 구문을 설명하십시오.

optionbox 2020. 11. 2. 07:53
반응형

컬렉션의 구문을 설명하십시오.emptyList ()


방금 일반 프로그래밍, List<E>인터페이스 및 에 대해 공부 ArrayList했으므로 아래 설명을 이해할 수 있습니다.

ArrayList<String> list = new ArrayList<String>();

그러나 나는 웹 서핑을하면서 내가 본 다음 진술을 이해하지 못한다.

List<String> list2 = Collections.<String>emptyList();
  1. 무엇입니까 Collections? Collections<E>그렇지 Collections<String>않습니까?
  2. <String>메소드 이름 앞에 배치 emptyList됩니까?

( emptyList<String>()일반에 맞지 않습니까?)

  1. 이 진술은 무엇을 의미합니까?

이 줄은 제네릭 형식 매개 변수로 정적 메서드를 호출하여 빈 문자열 목록을 만듭니다.

Collections클래스 내부에는 다음 emptyList과 같이 선언 된 정적 메서드 가 있습니다.

public static final <T> List<T> emptyList() {
    return (List<T>) EMPTY_LIST;
}

여기에는 일반 유형 매개 변수가 T있습니다. 다음을 사용하여이 메서드를 호출합니다.

List<String> list = Collections.emptyList();

T을 할 infered되어 String있기 때문에의 종류 list.

T호출 할 때 꺾쇠 괄호 안에 넣어 유형을 지정할 수도 있습니다 emptyList. 추론 된 것보다 더 구체적인 유형을 원할 경우 필요할 수 있습니다.

List<? extends Object> list = Collections.<String>emptyList();

emptyList<String>()이 배치는 메서드를 호출하지 않고 제네릭 클래스의 인스턴스를 만들 때만 유효하기 때문에 올바르지 않습니다. 사용할 때 new가능한 두 가지 유형 매개 변수가 있습니다. 클래스 이름 앞의 매개 변수는 생성자 전용이고 클래스 이름 뒤의 매개 변수는 전체 인스턴스에 대한 것이므로 클래스와 함께 :

class MyClass<A> {
    public <B> MyClass(A a, B b) {
        System.out.println(a + ", " + b);
    }
}

여기서 우리는 생성자를 호출 할 수 A있습니다 StringB입니다 Integer같은 :

MyClass<String> a = new <Integer>MyClass<String>("a", 3);

또는 유형 추론을 사용하여 :

MyClass<String> a = new MyClass<>("a", 3);

또한보십시오:


무엇입니까 Collections? Collections<E>그렇지 Collections<String>않습니까?

Collections JDK 클래스입니다.

이 클래스는 컬렉션에서 작동하거나 반환하는 정적 메서드로만 구성됩니다. 여기에는 컬렉션에서 작동하는 다형성 알고리즘, "래퍼"가 포함되어 있으며 지정된 컬렉션에 의해 지원되는 새 컬렉션과 몇 가지 다른 승률과 끝을 반환합니다.

일반적이지 않으며 인스턴스화 할 수 없습니다.

<String>메소드 이름 앞에 배치 emptyList됩니까?

Collections#emptyList일반적인 방법입니다. 여기에서는 형식 인수를 명시 적으로 지정합니다 String.

( emptyList<String>()일반에 맞지 않습니까?)

아니오, Java에서는 메소드에 대한 일반 유형 인수가 메소드 이름 앞에옵니다.

이 진술은 무엇을 의미합니까?

정적 emptyList메서드 를 호출 하고 반환 값을 유형의 변수에 할당합니다 List<String>.


요컨대, 이것은 비어 있고 변경 불가능한 문자열 목록을 만듭니다.

표정을 조금씩 살펴 보겠습니다.

Collections클래스의 이름입니다. 로부터 자바 독 :

이 클래스는 컬렉션에서 작동하거나 반환하는 정적 메서드로만 구성됩니다. 여기에는 컬렉션에서 작동하는 다형성 알고리즘, "래퍼"가 포함되어 있으며 지정된 컬렉션에 의해 지원되는 새 컬렉션과 몇 가지 다른 승률과 끝을 반환합니다.

emptyList()Collections클래스 ( Javadoc )에 정의 된 정적 메소드의 이름입니다 . 제네릭 메서드이고 <String>in Collections.<String>emptyList()은 제네릭 형식 인수 지정합니다.

이 메서드는를 반환하며 List<T>,이 경우 List<String>문자열 목록입니다. 구체적으로는 반환 , 불변의 문자열의 목록을.


1.Collection is an interface and collections is a static class consists exclusively of static methods that operate on or return collections. Hence we cannot use Collections<E> or Collections<String>.

2.<String> before emptyList(used to get the empty list that is immutable) denotes that only String values can be added to it. Like:

list2.add("A");
list2.add("B");

3.The statement means that it will create a immutable empty list of type List.

You can check out this link: Difference between Java Collection and Collections


Firstly, Collections is a helper library of static methods that operate on various types of collections. If you've done any sort of C++ programming, it's very synonymous to the <algorithm> library of functions.

In this case, you're invoking the static method emptyList(), which returns an empty list of a particular type. Since these methods still require a type, but Collections' methods can refer to many types, you have to specify the type upon invocation.

In order to call a generic method, you must call it with the parameter list (i.e. <String>) before the method name, but after the dot.

List<String> someList = Collections.<String>emptyList();

참고URL : https://stackoverflow.com/questions/27683759/explain-the-syntax-of-collections-stringemptylist

반응형