📌  相关文章
📜  迭代 LinkedHashSet 元素的Java程序

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

迭代 LinkedHashSet 元素的Java程序

LinkedHashSet是 HashSet 的有序版本,它在所有元素之间维护一个双向链表。当需要维护迭代顺序时,使用这个类。当遍历 HashSet 时,顺序是不可预测的,而 LinkedHashSet 允许我们按照元素插入的顺序遍历元素。当使用迭代器循环遍历 LinkedHashSet 时,元素将返回到它们插入的顺序。

迭代 LinkedHashSet 元素的不同方法:

  1. 使用 for-each 循环
  2. 使用迭代器
  3. 使用 JDK 1.8 流

方法 1:使用 for-each 循环

Java
// Java Program to Iterate LinkedHashSet Elements
 
import java.util.*;
import java.lang.*;
import java.io.*;
 
class GFG {
    // Main driver method
    public static void main(String[] args)
        throws java.lang.Exception
    {
        // Creating a LinkedHashSet
        LinkedHashSet hashSet
            = new LinkedHashSet();
 
        // Adding elements to LinkedHashSet
        // Custom inputs
        hashSet.add(1);
        hashSet.add(2);
        hashSet.add(3);
        hashSet.add(4);
        hashSet.add(5);
 
        // Iteration over HashSet
        // using for-each loop
        for (Integer element : hashSet)
 
            // Print the elements of LinkedHashSet created above
            System.out.println("Element is " + element);
    }
}


Java
// Java Program to Iterate LinkedHashSet Elements
 
import java.util.*;
import java.lang.*;
import java.io.*;
 
class GFG {
    // Main driver method
    public static void main(String[] args)
        throws java.lang.Exception
    {
        // Creating a LinkedHashSet
        LinkedHashSet hashSet
            = new LinkedHashSet();
 
        // Adding elements to LinkedHashSet
        Custom inputs hashSet.add(1);
        hashSet.add(2);
        hashSet.add(3);
        hashSet.add(4);
        hashSet.add(5);
 
        // Method returning iterator of same elements
        // as in above LinkedHashSet
        Iterator iter = hashSet.iterator();
 
        // Condition check
        // hasNext() method returns true
        // if iterator has more elements to iterate
        // else returns false (where cond violates)
        while (iter.hasNext())
 
            // Print all the elements of LinkedHashSet
            // using next() method
            System.out.println("Element is " + iter.next());
    }
}


Java
// Java Program to Iterate LinkedHashSet Elements
 
import java.util.*;
import java.lang.*;
import java.io.*;
 
class GFG {
   
    // Main driver method
    public static void main(String[] args)
        throws java.lang.Exception
    {
        // Creating a LinkedHashSet
        LinkedHashSet hashSet
            = new LinkedHashSet();
 
        // Adding elements to LinkedHashSet
        // Custom inputs
        hashSet.add(1);
        hashSet.add(2);
        hashSet.add(3);
        hashSet.add(4);
        hashSet.add(5);
 
        // Method returning iterator of same elements
        // as in above LinkedHashSet
        Iterator iter = hashSet.iterator();
 
        // Stream API used to process collection
        // of objects
 
        // Iterating using for-each
        hashSet.stream().forEach(element -> {
           
            // Print all elements of LinkedHashSet
            System.out.println("Element is  " + element);
        });
    }
}


输出
Element is 1
Element is 2
Element is 3
Element is 4
Element is 5

方法二:使用迭代器

Java

// Java Program to Iterate LinkedHashSet Elements
 
import java.util.*;
import java.lang.*;
import java.io.*;
 
class GFG {
    // Main driver method
    public static void main(String[] args)
        throws java.lang.Exception
    {
        // Creating a LinkedHashSet
        LinkedHashSet hashSet
            = new LinkedHashSet();
 
        // Adding elements to LinkedHashSet
        Custom inputs hashSet.add(1);
        hashSet.add(2);
        hashSet.add(3);
        hashSet.add(4);
        hashSet.add(5);
 
        // Method returning iterator of same elements
        // as in above LinkedHashSet
        Iterator iter = hashSet.iterator();
 
        // Condition check
        // hasNext() method returns true
        // if iterator has more elements to iterate
        // else returns false (where cond violates)
        while (iter.hasNext())
 
            // Print all the elements of LinkedHashSet
            // using next() method
            System.out.println("Element is " + iter.next());
    }
}
输出
Element is 1
Element is 2
Element is 3
Element is 4
Element is 5

方法 3:使用 JDK 1.8 流

Java

// Java Program to Iterate LinkedHashSet Elements
 
import java.util.*;
import java.lang.*;
import java.io.*;
 
class GFG {
   
    // Main driver method
    public static void main(String[] args)
        throws java.lang.Exception
    {
        // Creating a LinkedHashSet
        LinkedHashSet hashSet
            = new LinkedHashSet();
 
        // Adding elements to LinkedHashSet
        // Custom inputs
        hashSet.add(1);
        hashSet.add(2);
        hashSet.add(3);
        hashSet.add(4);
        hashSet.add(5);
 
        // Method returning iterator of same elements
        // as in above LinkedHashSet
        Iterator iter = hashSet.iterator();
 
        // Stream API used to process collection
        // of objects
 
        // Iterating using for-each
        hashSet.stream().forEach(element -> {
           
            // Print all elements of LinkedHashSet
            System.out.println("Element is  " + element);
        });
    }
}
输出
Element is  1
Element is  2
Element is  3
Element is  4
Element is  5