📌  相关文章
📜  Java程序从LinkedHashSet中按索引获取元素

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

Java程序从LinkedHashSet中按索引获取元素

LinkedHashSet 是Java中一个类似于 HashSet 的预定义类。不像 HashSet 在 LinkedHashSet 中插入顺序是保留的。为了从Java中的 LinkedHashSet 中按索引获取元素,我们有多种方法。

插图:

Input      : 2, 3, 4, 2, 7;
Processing : index = 4;
Output     : Element at index 4 is : 7

方法:

  1. 一种使用迭代计数方法的幼稚方法
  2. 将 LinkedHashSet 转换为数组
  3. 将 LinkedHashSet 转换为列表

方法 1:使用迭代方法进行索引计数并获取给定索引处的元素的朴素方法。

算法

  1. 使用迭代器遍历我们的 LinkedHashSet。
  2. 启动出索引指针 currentindex = 0
  3. 使用 while 循环开始迭代,如果当前索引变得等于给定索引,则打印元素。
Pseudo Code: 
Iterator it = LHS.iterator();
while(it.hasNext()) {}

执行:

示例 1

Java
// Java Program to Get Elements by Index from LinkedHashSet
// Using iteration count method
 
// Importing generic java libraries
import java.io.*;
import java.util.*;
 
// Main class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
 
    {
        // Adding elements to LinkedHashSet
        // Custom inputs
        LinkedHashSet LHS = new LinkedHashSet<>();
        LHS.add(2);
        LHS.add(3);
        LHS.add(4);
        LHS.add(2);
        LHS.add(7);
 
        // Custom index chosen to get the element
        // present at that index
        int index = 4;
 
        Iterator it = LHS.iterator();
 
        // Assigning initial values
        int currIndex = 0;
        Integer CurrentElement = null;
 
        // Condition check using hasNext(), whick
        // returns true if another token as input
        while (it.hasNext()) {
 
            // next element using iterator is
            // assigned to variable
            CurrentElement = it.next();
 
            // Variable condition check
            if (currIndex == index - 1) {
                System.out.println("Element at index "
                                   + index + " is : "
                                   + CurrentElement);
                break;
            }
 
            // If condition fails, so
            // Incrementing current index
            currIndex++;
        }
    }
}


Java
// Java Program to Get Elements by Index from LinkedHashSet
// By converting LinkedHashSet to Array
 
// Importing generic java libraries
import java.io.*;
import java.util.*;
 
// Main class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Creating a LinkedHashSet
        LinkedHashSet LHS = new LinkedHashSet<>();
 
        // Adding elements() to LinkedHashSet
        LHS.add(2);
        LHS.add(3);
        LHS.add(4);
        LHS.add(2);
        LHS.add(7);
 
        // Custom index chosen from LinkedHashSet
        int index = 4;
 
        // Converting LnkedHashMap to Array
        Integer[] LHSArray = new Integer[LHS.size()];
        LHSArray = LHS.toArray(LHSArray);
 
        // Printing desired value at index in array,
        // chosen above index from LinkedHashap
        System.out.println("Element at index " + index
                           + " is : "
                           + LHSArray[index - 1]);
    }
}


Java
// Java Program to Get Elements by Index from LinkedHashSet
// By converting LinkedHashSet to List
 
// Importing java generic libraries
import java.util.*;
import java.io.*;
 
// Main class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Creating a LinkedHashSet
        LinkedHashSet LHS = new LinkedHashSet<>();
 
        // Adding elements to LinkedHashSet
        LHS.add(2);
        LHS.add(3);
        LHS.add(4);
        LHS.add(2);
        LHS.add(7);
 
        // Custom index chosen to retrieve value
        int index = 4;
 
        // Converting LinkedHashSet to List
        Iterator it = LHS.iterator();
 
        // Assigning initial values
        int currIndex = 0;
        Integer CurrentElement = null;
 
        // Condition check using hasNext(), whick
        // returns true if another token as input
        while (it.hasNext()) {
            CurrentElement = it.next();
 
            if (currIndex == index - 1) {
 
                // Printing desired value at index in array,
                // chosen above index from LinkedHashap
                System.out.println("Element at index "
                                   + index + " is : "
                                   + CurrentElement);
                break;
            }
 
            // Incrementing the current index
            currIndex++;
        }
    }
}


输出
Element at index 4 is : 7

方法 2: LinkedHashSet 转换为 Array,通过它可以访问给定索引处的元素。

算法:

  1. 使用toArray()方法将给定的 LinkedHashSet 转换为数组。
  2. 访问数组中给定索引上的元素。
Pseudo Code:
Integer[] LHSArray = new Integer[LHS.size()];
LHSArray = LHS.toArray(LHSArray);

例子

Java

// Java Program to Get Elements by Index from LinkedHashSet
// By converting LinkedHashSet to Array
 
// Importing generic java libraries
import java.io.*;
import java.util.*;
 
// Main class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Creating a LinkedHashSet
        LinkedHashSet LHS = new LinkedHashSet<>();
 
        // Adding elements() to LinkedHashSet
        LHS.add(2);
        LHS.add(3);
        LHS.add(4);
        LHS.add(2);
        LHS.add(7);
 
        // Custom index chosen from LinkedHashSet
        int index = 4;
 
        // Converting LnkedHashMap to Array
        Integer[] LHSArray = new Integer[LHS.size()];
        LHSArray = LHS.toArray(LHSArray);
 
        // Printing desired value at index in array,
        // chosen above index from LinkedHashap
        System.out.println("Element at index " + index
                           + " is : "
                           + LHSArray[index - 1]);
    }
}
输出
Element at index 4 is : 7

方法三:将 LinkedHashSet 转为 List 以获取给定索引处的所需元素。

算法

  1. 将我们的 LinkedHashMap 像 ArrayList 一样转换为 List。
  2. 使用get() 方法获取给定索引中的元素。
Pseudo Code : List LHSList =new ArrayList<>(LHS);
              where LHS is name of our LinkedHashSet

实施

Java

// Java Program to Get Elements by Index from LinkedHashSet
// By converting LinkedHashSet to List
 
// Importing java generic libraries
import java.util.*;
import java.io.*;
 
// Main class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Creating a LinkedHashSet
        LinkedHashSet LHS = new LinkedHashSet<>();
 
        // Adding elements to LinkedHashSet
        LHS.add(2);
        LHS.add(3);
        LHS.add(4);
        LHS.add(2);
        LHS.add(7);
 
        // Custom index chosen to retrieve value
        int index = 4;
 
        // Converting LinkedHashSet to List
        Iterator it = LHS.iterator();
 
        // Assigning initial values
        int currIndex = 0;
        Integer CurrentElement = null;
 
        // Condition check using hasNext(), whick
        // returns true if another token as input
        while (it.hasNext()) {
            CurrentElement = it.next();
 
            if (currIndex == index - 1) {
 
                // Printing desired value at index in array,
                // chosen above index from LinkedHashap
                System.out.println("Element at index "
                                   + index + " is : "
                                   + CurrentElement);
                break;
            }
 
            // Incrementing the current index
            currIndex++;
        }
    }
}
输出
Element at index 4 is : 7