logo

자바 SortedSet 인터페이스

세트는 해당 요소에 특정 순서를 제공하는 데 사용됩니다. 요소는 자연 순서를 사용하거나 비교기를 사용하여 정렬됩니다. 정렬된 집합에 삽입되는 모든 요소는 Comparable 인터페이스를 구현해야 합니다.

세트의 반복자는 세트를 오름차순으로 순회합니다. 주문을 최대한 활용하기 위해 몇 가지 다른 작업이 제공됩니다. 모든 요소는 서로 비교 가능해야 합니다.

행동 양식

비교기() 주어진 세트의 요소를 정렬하는 데 사용되는 비교기를 반환합니다. 또한 지정된 세트가 요소의 자연스러운 순서를 사용하는 경우 null을 반환합니다.
첫 번째() 현재 세트의 첫 번째 요소를 반환합니다.
headSet(E toElement) 주어진 세트에서 요소가 toElement보다 엄격하게 작은 부분의 뷰를 반환합니다.
마지막() 지도에 존재하는 매핑의 역순 보기를 반환합니다.
분할기() 주어진 맵에서 가장 작은 키와 연관된 키-값 매핑을 반환합니다. 또한 지도가 비어 있으면 null을 반환합니다.
subSet(E fromElement, E toElement) 주어진 키보다 작거나 같은 가장 큰 키와 연관된 키-값 매핑을 반환합니다. 또한 지도가 비어 있으면 null을 반환합니다.
tailSet(E fromElement) 키가 toKey보다 엄격하게 작은 맵의 뷰를 반환합니다.

실시예 1

 import java.util.SortedSet; import java.util.TreeSet; public class JavaSortedSetExample1 { public static void main(String[] args) { SortedSet set = new TreeSet(); // Add the elements in the given set. set.add('Audi'); set.add('BMW'); set.add('Mercedes'); set.add('Baleno'); System.out.println('The list of elements is given as:'); for (Object object : set) { System.out.println(object); } //Returns the first element System.out.println('The first element is given as: ' + set.first()); //Returns the last element System.out.println('The last element is given as: ' + set.last()); //Returns a view of the portion of the given set whose elements are strictly less than the toElement. System.out.println('The respective element is given as: ' + set.headSet('Baleno')); //Returns a view of the map whose keys are strictly less than the toKey. System.out.println('The respective element is given as: ' + set.tailSet('Audi')); } } 
지금 테스트해보세요

산출:

 The list of elements is given as: Audi BMW Baleno Mercedes The first element is given as: Audi The last element is given as: Mercedes The respective element is given as: [Audi, BMW] The respective element is given as: [Audi, BMW, Baleno, Mercedes]