Java 如何找到LinkedHashSet中的元素索引

Java 如何找到LinkedHashSet中的元素索引

LinkedHashSet是Java Collection Framework的一个类,它实现了Set接口并扩展了HashSet类。它是一种链表类型的集合类。它以插入的顺序存储和返回对象。与List或Array不同,它没有一个直接的方法来找到指定集合中元素的索引,因为它不通过索引存储其元素。在本文中,我们将找到在LinkedHashSet中找到元素索引的几种可能的方法。

在LinkedHashSet中查找元素索引的Java程序

我们将使用以下方法来查找LinkedHashSet中元素的索引:

  • 使用Iterator接口

  • 使用List

让我们依次讨论它们。

使用Iterator

Java Collection Framework的每个集合类都提供了一个’iterator()’方法,该方法返回一个迭代器对象,该对象可用于按顺序访问指定集合中的每个元素。除此之外,我们总是在循环中调用’hasNext()’方法,以便循环在此方法返回true时迭代。此外,要获取集合的元素,我们使用内置方法’next()’。

语法

Iterator<typeOfCollection> iteratorObject = collectionName.iterator();

示例1

以下示例说明了如何使用迭代器来找到元素的索引。

方法

  • 我们的第一步是导入’java.util’包,以便我们可以使用集合类。

  • 然后,我们创建一个类型为String的LinkedHashSet的集合,并使用内置的方法’add()’将一些元素存储到其中。

  • 现在,使用迭代器来找到定义的集合中指定元素的索引。

import java.util.*;
public class FindElement {
   public static void main(String[] args) {
      // Creating a LinkedHashSet
      LinkedHashSet<String> LinkHSet = new LinkedHashSet<>();
      // Appending elements to the defined LinkHSet
      LinkHSet.add("Tutorials");
      LinkHSet.add("Point");
      LinkHSet.add("Tutorix");
      LinkHSet.add("Simply");
      LinkHSet.add("Easy");
      LinkHSet.add("Learning");
      // to print all elements of LinkedHashSet
      System.out.println("List of all elements in LinkedHashSet: " + LinkHSet);
      // logic to find the index of specified elements
      int index = 1; // initialize index to -1
      int countr = 1; // initialize countr to 0
      // define an iterator for the LinkedHashSet
      Iterator<String> iterator = LinkHSet.iterator(); 
      while (iterator.hasNext()) { 
         String element = iterator.next(); // get the next element
         if (element.equals("Tutorix")) { // check if it matches the specified element
            index = countr; // update index to countr variable
            break; // exit the loop after getting the index
         }
         countr++; // incrementing the countr
      }
      System.out.println("Tutorix is present at the Index: " + countr);        
    }
}

输出

List of all elements in LinkedHashSet: [Tutorials, Point, Tutorix, Simply, Easy, Learning]
Tutorix is present at the Index: 3

使用List

List是Java Collection Framework的子接口,它以线性结构的形式存储和访问每个元素。为了使用List的功能,我们将使用实现列表接口的ArrayList类。

要在LinkedHashSet中查找元素的索引,我们首先将其转换为List,然后使用内置的名为’indexOf()’的方法,该方法返回指定元素的索引号,如果元素不存在于集合中,则返回-1。

示例2

以下示例演示了在LinkedHashSet中查找元素索引时使用List的方法。

import java.util.*;
public class FindElement {
    public static void main(String[] args) {
        // Creating a LinkedHashSet
        LinkedHashSet<Integer> LinkHSet = new LinkedHashSet<>();
        // Appending elements to the defined LinkHSet
        LinkHSet.add(56);
        LinkHSet.add(87);
        LinkHSet.add(92);
        LinkHSet.add(68);
        LinkHSet.add(46);
        LinkHSet.add(55);
        // to print all elements of LinkedHashSet
        System.out.println("List of all elements in LinkedHashSet: " + LinkHSet);
        // Converting the LinkedHashSet to ArrayList
        ArrayList<Integer> Alist = new ArrayList<>(LinkHSet);
        // to find the index of specified elements
        System.out.println( 92 + " is present at the Index: "+ Alist.indexOf(92));
        System.out.println( 102 + " is present at the Index: "+ Alist.indexOf(102));                    
    }
}

输出

List of all elements in LinkedHashSet: [56, 87, 92, 68, 46, 55]
92 is present at the Index: 2
102 is present at the Index: -1

结论

在本文中,我们首先讨论了Java Collection Framework中LinkedHashSet类,以及问题陈述,接下来,我们提出了两种方法来查找LinkedHashSet类中的元素索引。我们可以使用迭代器,也可以通过将LinkedHashSet转换为List来查找索引。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程