📌  相关文章
📜  如何在Java中的 LinkedHashSet 中查找元素索引?

📅  最后修改于: 2022-05-13 01:54:38.884000             🧑  作者: Mango

如何在Java中的 LinkedHashSet 中查找元素索引?

LinkedHashSet 用于存储不同的项目并获取它们在Java中插入的顺序。 LinkedHashSet 不存储基于索引的值。但是有一些方法可以在Java中找到 LinkedHashSet 中的元素索引。

方法一:(通过将LinkedHashSet转换为ArrayList)

  Java中通过将LinkedHashSet转换为ArrayList来查找LinkedHashSet中的元素索引,过程分为两部分:

1.使用构造函数将 LinkedHashSet 转换为 ArrayList。

// Convert LinkedHashSet to ArrayList using constructor
ArrayList elements = new ArrayList<>(set);

2.并使用Java中的 indexOf() 方法获取元素索引。

句法:

public int indexOf(Object o)

参数:该函数只有一个参数,即要在列表中搜索的元素。

返回:此方法返回给定元素在列表中第一次出现的索引,如果该元素不在列表中,则返回“-1”。

例子:

Java
// Java program to find the element
// index in LinkedHashSet
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // New empty HashSet
        LinkedHashSet set = new LinkedHashSet<>();
  
        // Add elements to set
        set.add(10);
        set.add(20);
        set.add(10);
        set.add(50);
        set.add(30);
        set.add(20);
        set.add(50);
  
        // Convert LinkedHashSet to ArrayList
        ArrayList elements = new ArrayList<>(set);
  
        // Print the LinkedHashSet
        System.out.println("LinkedHashSet: " + set);
  
        // Print index of all the elements
        for (Integer x : elements) {
            System.out.println("Index of " + x + ": "
                               + elements.indexOf(x));
        }
  
        // It returns -1 becouce 60 not present in
        // LinkedHashSet
        System.out.println("Index of " + 60 + ": "
                           + elements.indexOf(60));
    }
}


Java
// Java program to find the 
// element index in LinkedHashSet
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // New empty HashSet
        LinkedHashSet set = new LinkedHashSet<>();
  
        // Add elements to set
        set.add(10);
        set.add(20);
        set.add(10);
        set.add(50);
        set.add(30);
        set.add(20);
        set.add(50);
  
        // Print the LinkedHashSet
        System.out.println("LinkedHashSet: " + set);
  
        // Print index of elements
        for (Integer x : set) {
            System.out.println("Index of " + x + ": "
                               + indexOf(set, x));
        }
  
        // It returns -1 becouce it is not present in
        // LinkedHashSet
        System.out.println("Index of " + 60 + ": "
                           + indexOf(set, 60));
    }
  
    public static int indexOf(LinkedHashSet set,
                              int element)
    {
  
        // If element not present in the LinkedHashSet it
        // returns -1
        int index = -1;
  
        // get an iterator
        Iterator iterator = set.iterator();
  
        int currentIndex = 0;
        while (iterator.hasNext()) {
  
            // If element present in the LinkedHashSet
            if (iterator.next().equals(element)) {
                index = currentIndex;
                break;
            }
  
            currentIndex++;
        }
  
        // Return index of the element
        return index;
    }
}


Java
// Java program to find the 
// element index in LinkedHashSet
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // New empty HashSet
        LinkedHashSet set = new LinkedHashSet<>();
  
        // Add elements to set
        set.add(10);
        set.add(20);
        set.add(10);
        set.add(50);
        set.add(30);
        set.add(20);
        set.add(50);
  
        // Print the LinkedHashSet
        System.out.println("LinkedHashSet: " + set);
  
        // New Array
        Integer[] array = new Integer[set.size()];
  
        // Convert LinkedHashSet to Array
        array = set.toArray(array);
  
        // Print index of elements
        for (int i = 0; i < array.length; i++) {
            System.out.println("Index of " + array[i] + ":"
                               + i);
        }
  
        int element = 50;
  
        int index = -1;
  
        for (int i = 0; i < array.length; i++) {
  
            // if element is equal to array value
            // store index and come out
            if (array[i] == element) {
                index = i;
                break;
            }
        }
  
        // print index
        System.out.println("Index of " + element
                           + " is : " + index);
    }
}


输出
LinkedHashSet: [10, 20, 50, 30]
Index of 10: 0
Index of 20: 1
Index of 50: 2
Index of 30: 3
Index of 60: -1

方法二:(使用 Iterator 或增强 for 循环

要在Java中查找元素索引,我们可以创建一个新的用户定义函数(indexOf),该函数返回给定的元素索引。我们的函数迭代 LinkedHashSet 并返回给定元素的索引。

注意:如果 LinkedHashSet 中不存在该元素,则返回 -1。

例子:

Java

// Java program to find the 
// element index in LinkedHashSet
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // New empty HashSet
        LinkedHashSet set = new LinkedHashSet<>();
  
        // Add elements to set
        set.add(10);
        set.add(20);
        set.add(10);
        set.add(50);
        set.add(30);
        set.add(20);
        set.add(50);
  
        // Print the LinkedHashSet
        System.out.println("LinkedHashSet: " + set);
  
        // Print index of elements
        for (Integer x : set) {
            System.out.println("Index of " + x + ": "
                               + indexOf(set, x));
        }
  
        // It returns -1 becouce it is not present in
        // LinkedHashSet
        System.out.println("Index of " + 60 + ": "
                           + indexOf(set, 60));
    }
  
    public static int indexOf(LinkedHashSet set,
                              int element)
    {
  
        // If element not present in the LinkedHashSet it
        // returns -1
        int index = -1;
  
        // get an iterator
        Iterator iterator = set.iterator();
  
        int currentIndex = 0;
        while (iterator.hasNext()) {
  
            // If element present in the LinkedHashSet
            if (iterator.next().equals(element)) {
                index = currentIndex;
                break;
            }
  
            currentIndex++;
        }
  
        // Return index of the element
        return index;
    }
}
输出
LinkedHashSet: [10, 20, 50, 30]
Index of 10: 0
Index of 20: 1
Index of 50: 2
Index of 30: 3
Index of 60: -1

方法三:(Using an Array) Java中使用Array查找LinkedHashSet中的元素索引,过程分为两部分:

1.使用 toArray() 方法将 LinkedHashSet 转换为数组。

// New Array

Integer[] array = new Integer[set.size()];

// Convert set to Array using toArray
        
array = set.toArray(array);

2.使用索引查找元素。

句法:

Object[] arr = LinkedHashSet.toArray()

参数:该方法不带任何参数。

返回值:该方法返回一个包含类似于 LinkedHashSet 元素的数组。

例子:

Java

// Java program to find the 
// element index in LinkedHashSet
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // New empty HashSet
        LinkedHashSet set = new LinkedHashSet<>();
  
        // Add elements to set
        set.add(10);
        set.add(20);
        set.add(10);
        set.add(50);
        set.add(30);
        set.add(20);
        set.add(50);
  
        // Print the LinkedHashSet
        System.out.println("LinkedHashSet: " + set);
  
        // New Array
        Integer[] array = new Integer[set.size()];
  
        // Convert LinkedHashSet to Array
        array = set.toArray(array);
  
        // Print index of elements
        for (int i = 0; i < array.length; i++) {
            System.out.println("Index of " + array[i] + ":"
                               + i);
        }
  
        int element = 50;
  
        int index = -1;
  
        for (int i = 0; i < array.length; i++) {
  
            // if element is equal to array value
            // store index and come out
            if (array[i] == element) {
                index = i;
                break;
            }
        }
  
        // print index
        System.out.println("Index of " + element
                           + " is : " + index);
    }
}
输出
LinkedHashSet: [10, 20, 50, 30]
Index of 10:0
Index of 20:1
Index of 50:2
Index of 30:3
Index of 50 is : 2