查找 TreeSet 元素索引的Java程序
与 ArrayList 或 LinkedList 等 List 类不同,TreeSet 类不允许使用索引访问元素。没有使用索引访问 TreeSet 元素的直接方法,因此查找元素的索引并不简单。
方法:主要有以下三种标准方法:
- 通过将 TreeSet 转换为 List
- 使用迭代器
- 使用 TreeSet 类的headSet()方法
方法 1:通过将 TreeSet 转换为 List
像 ArrayList 或 LinkedList 这样的 List 类提供了indexOf()方法来查找元素索引。我们可以将 TreeSet 转换为 ArrayList 然后使用indexOf() 方法。此方法返回此列表中指定元素第一次出现的索引,如果此列表不包含该元素,则返回 -1。
句法:
public int indexOf(Object o) ;
参数:该函数只有一个参数,即要在列表中搜索的元素。
返回:此方法返回给定元素在列表中第一次出现的索引,如果该元素不在列表中,则返回“-1”。
例子
Java
// Java Program to find the index of TreeSet element
// using List by converting TreeSet to a List
// Importing ArrayList, List and TreeSet classes
// from java.util package
import java.util.ArrayList;
import java.util.List;
import java.util.TreeSet;
// Class
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating a object of the TreeSet class
// Declaring object of Integer type
TreeSet treeSet = new TreeSet();
// Adding the element to the TreeSet
// Custom inputs
treeSet.add(34);
treeSet.add(23);
treeSet.add(43);
treeSet.add(41);
treeSet.add(35);
treeSet.add(33);
// Printing all the elements in the TreeSet object
System.out.println("TreeSet contains: " + treeSet);
// Printing indexes of elements using indexOf()
// method Index of element number 1
System.out.println("Index of 23: "
+ indexOf(treeSet, 23));
// Index of element number 2
System.out.println("Index of 43: "
+ indexOf(treeSet, 43));
// Index of element number 3
System.out.println("Index of 35: "
+ indexOf(treeSet, 35));
// Index of element number 4
System.out.println("Index of 55: "
+ indexOf(treeSet, 55));
}
// Method - indexOf()
private static int indexOf(TreeSet set,
Integer element)
{
// Step 1: Convert TreeSet to ArrayList or
// LinkedList
List list = new ArrayList(set);
// Step 2: Use the indexOf method of the List
return list.indexOf(element);
}
}
Java
// Java Program to find the index of the element
// in the TreeSet using Iterator
// Using an Iterator
// Importing Iterator and TreeSet class from
// java.util package
import java.util.Iterator;
import java.util.TreeSet;
// Class
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating an object of TreeSet class
// Declaring object of Integer type
TreeSet treeSet = new TreeSet();
// Adding the elements to the TreeSet
// Custom inputs
treeSet.add(34);
treeSet.add(23);
treeSet.add(43);
treeSet.add(41);
treeSet.add(35);
treeSet.add(33);
// Printing the element in the TreeSet
System.out.println("TreeSet contains: " + treeSet);
// Printing the indexes of elements in above TreeSet
// object
System.out.println("Index of 23: "
+ indexOf(treeSet, 23));
System.out.println("Index of 43: "
+ indexOf(treeSet, 43));
System.out.println("Index of 35: "
+ indexOf(treeSet, 35));
System.out.println("Index of 55: "
+ indexOf(treeSet, 55));
}
// Method - indexOf()
private static int indexOf(TreeSet set,
Integer element)
{
int index = -1;
// Get an iterator
Iterator itr = set.iterator();
Integer currentElement = null;
int currentIndex = 0;
// Condition check using hasNext() method which
// holds true till single element in List is
// remaining
while (itr.hasNext()) {
currentElement = itr.next();
// Checking if the current element equals
// the element whose index is tried to search
if (currentElement.equals(element)) {
// Return the index of the element
return currentIndex;
}
// Increment the index number
currentIndex++;
}
// Return the index -1
// if the element do not exists
return index;
}
}
Java
// Java Program to find the index of element
// in TreeSet using HeadSet
// Using the headSet() method of the TreeSet class
// Importing TreeSet class from
// java.util package
import java.util.TreeSet;
// Class
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Making the new object of TreeSet class
TreeSet treeSet = new TreeSet();
// Adding the elements to the TreeSet
treeSet.add(34);
treeSet.add(23);
treeSet.add(43);
treeSet.add(41);
treeSet.add(35);
treeSet.add(33);
// Printing the elements of the TreeSet
System.out.println("TreeSet contains: " + treeSet);
// Printing the indexes of elements
// in above TreeSet object
System.out.println("Index of 23: "
+ indexOf(treeSet, 23));
System.out.println("Index of 43: "
+ indexOf(treeSet, 43));
System.out.println("Index of 35: "
+ indexOf(treeSet, 35));
System.out.println("Index of 55: "
+ indexOf(treeSet, 55));
}
// Method - indexOf() method
private static int indexOf(TreeSet set,
Integer element)
{
int index = -1;
// If the element exists in the TreeSet
if (set.contains(element)) {
// The element index will be equal to the
// size of the headSet for the element
index = set.headSet(element).size();
}
// Return the index of the element
// Value will be -1 if the element
// do not exist in the TreeSet
return index;
}
}
TreeSet contains: [23, 33, 34, 35, 41, 43]
Index of 23: 0
Index of 43: 5
Index of 35: 3
Index of 55: -1
方法 2:使用迭代器
程序:
- 使用迭代器方法对 TreeSet 元素进行迭代器。
- 获取迭代器后,遍历元素并根据用户或自定义输入的要求搜索指定元素,如下所示,以供理解。
例子
Java
// Java Program to find the index of the element
// in the TreeSet using Iterator
// Using an Iterator
// Importing Iterator and TreeSet class from
// java.util package
import java.util.Iterator;
import java.util.TreeSet;
// Class
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating an object of TreeSet class
// Declaring object of Integer type
TreeSet treeSet = new TreeSet();
// Adding the elements to the TreeSet
// Custom inputs
treeSet.add(34);
treeSet.add(23);
treeSet.add(43);
treeSet.add(41);
treeSet.add(35);
treeSet.add(33);
// Printing the element in the TreeSet
System.out.println("TreeSet contains: " + treeSet);
// Printing the indexes of elements in above TreeSet
// object
System.out.println("Index of 23: "
+ indexOf(treeSet, 23));
System.out.println("Index of 43: "
+ indexOf(treeSet, 43));
System.out.println("Index of 35: "
+ indexOf(treeSet, 35));
System.out.println("Index of 55: "
+ indexOf(treeSet, 55));
}
// Method - indexOf()
private static int indexOf(TreeSet set,
Integer element)
{
int index = -1;
// Get an iterator
Iterator itr = set.iterator();
Integer currentElement = null;
int currentIndex = 0;
// Condition check using hasNext() method which
// holds true till single element in List is
// remaining
while (itr.hasNext()) {
currentElement = itr.next();
// Checking if the current element equals
// the element whose index is tried to search
if (currentElement.equals(element)) {
// Return the index of the element
return currentIndex;
}
// Increment the index number
currentIndex++;
}
// Return the index -1
// if the element do not exists
return index;
}
}
TreeSet contains: [23, 33, 34, 35, 41, 43]
Index of 23: 0
Index of 43: 5
Index of 35: 3
Index of 55: -1
方法:3使用TreeSet类的headSet()方法
TreeSet 类的headSet()方法返回 TreeSet 中元素小于指定元素的部分的视图。由于 TreeSet 的元素按元素的自然顺序或通过自定义比较器自动排序,因此耳机大小将等于小于或小于指定元素的元素数。如果我们将 TreeSet 元素放在 List 中,那么该数字将等于元素的索引。
插图:
If the TreeSet contains [1, 2, 3, 4] then the headset of element 3 will contain elements [1, 2]. The size of the headset will be 2 and that will be the index of element 3. Thus, if we get the size of the headset, then it will be equal to the position of the element for which we have to find the index.
例子
Java
// Java Program to find the index of element
// in TreeSet using HeadSet
// Using the headSet() method of the TreeSet class
// Importing TreeSet class from
// java.util package
import java.util.TreeSet;
// Class
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Making the new object of TreeSet class
TreeSet treeSet = new TreeSet();
// Adding the elements to the TreeSet
treeSet.add(34);
treeSet.add(23);
treeSet.add(43);
treeSet.add(41);
treeSet.add(35);
treeSet.add(33);
// Printing the elements of the TreeSet
System.out.println("TreeSet contains: " + treeSet);
// Printing the indexes of elements
// in above TreeSet object
System.out.println("Index of 23: "
+ indexOf(treeSet, 23));
System.out.println("Index of 43: "
+ indexOf(treeSet, 43));
System.out.println("Index of 35: "
+ indexOf(treeSet, 35));
System.out.println("Index of 55: "
+ indexOf(treeSet, 55));
}
// Method - indexOf() method
private static int indexOf(TreeSet set,
Integer element)
{
int index = -1;
// If the element exists in the TreeSet
if (set.contains(element)) {
// The element index will be equal to the
// size of the headSet for the element
index = set.headSet(element).size();
}
// Return the index of the element
// Value will be -1 if the element
// do not exist in the TreeSet
return index;
}
}
TreeSet contains: [23, 33, 34, 35, 41, 43]
Index of 23: 0
Index of 43: 5
Index of 35: 3
Index of 55: -1