컬렉션의 구문을 설명하십시오.emptyList ()
방금 일반 프로그래밍, List<E>
인터페이스 및 에 대해 공부 ArrayList
했으므로 아래 설명을 이해할 수 있습니다.
ArrayList<String> list = new ArrayList<String>();
그러나 나는 웹 서핑을하면서 내가 본 다음 진술을 이해하지 못한다.
List<String> list2 = Collections.<String>emptyList();
- 무엇입니까
Collections
? 왜Collections<E>
그렇지Collections<String>
않습니까? <String>
메소드 이름 앞에 왜 배치emptyList
됩니까?
( emptyList<String>()
일반에 맞지 않습니까?)
- 이 진술은 무엇을 의미합니까?
이 줄은 제네릭 형식 매개 변수로 정적 메서드를 호출하여 빈 문자열 목록을 만듭니다.
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
있습니다 String
및 B
입니다 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
'developer tip' 카테고리의 다른 글
템플릿에 Django 양식 필드의 값을 어떻게 표시합니까? (0) | 2020.11.03 |
---|---|
++ 연산자에 관한 C와 C ++의 차이점 (0) | 2020.11.02 |
react-router를 사용하여 다른 경로로 리디렉션하는 방법은 무엇입니까? (0) | 2020.11.02 |
GraphViz에 비해 너무 큰 무 방향 그래프 시각화? (0) | 2020.11.02 |
CSS-ID 내에서 클래스를 선택하는 구문 (0) | 2020.11.02 |