📜  如何在Java中遍历集合对象?

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

如何在Java中遍历集合对象?

任何表示为单个单元的单个对象组称为集合 的对象。在Java中,JDK 1.2 中定义了一个名为“集合框架”的单独框架,其中包含所有集合类和接口。

Collection 接口(Java.util.Collection) 和Map 接口(Java.util.Map) 是Java集合类的两个主要“根”接口。

如何遍历集合对象?

  1. 使用增强的 For 循环
  2. 使用迭代器方法
  3. 使用简单的 For 循环
  4. 使用 forEach 方法

方法一:使用增强的 For 循环

使用的语法:

for (datatype variable : collection_used)

例子:

Java
// Java program to demonstrate the
// working of enhanced for loop to iterate
// collection objects 
  
import java.io.*;
import java.util.*;
  
class GFG {
    public static void main(String[] args)
    {
  
        // Declaring the ArrayList
        Collection gfg = new ArrayList();
  
        // Appending new elements at
        // the end of the list
        gfg.add("Abhishek Rout");
        gfg.add("Vaibhav Kamble");
        gfg.add("Anupam Kumar");
  
        // for-each loop for iterating
        // unconditionally through collection
        for (String name : gfg)
            System.out.println("Name : " + name);
    }
}


Java
// Java program to demonstrate the
// working of iterator method to iterate
// collection objects 
  
import java.io.*;
import java.util.*;
  
class GFG {
    public static void main(String[] args)
    {
  
        // Declaring the LinkedList
        LinkedList gfg = new LinkedList();
  
        // Appending new elements at
        // the end of the list
        gfg.add("Abhishek Rout");
        gfg.add("Vaibhav Kamble");
        gfg.add("Anupam Kumar");
  
        // for loop for iterating
        // conditionally through collection
        System.out.println("Using For loop");
        
        for (Iterator name = gfg.iterator();
             name.hasNext();)
            
            System.out.println("Name : " + name.next());
  
          // while loop for iterating
        // conditionally through collection
        System.out.println("\nUsing While Loop");
        
        Iterator name = gfg.iterator();
  
        while (name.hasNext())
            System.out.println("Name : " + name.next());
    }
}


Java
// Java program to demonstrate the
// working of for loop to iterate
// collection objects 
  
import java.io.*;
import java.util.*;
  
class GFG {
    public static void main(String[] args)
    {
  
        // Declaring the ArrayList
        Vector gfg = new Vector();
  
        // Appending new elements at
        // the end of the list
        gfg.add("Abhishek Rout");
        gfg.add("Vaibhav Kamble");
        gfg.add("Anupam Kumar");
  
        // for loop for iterating
        // through collection
        for (int i = 0; i < gfg.size(); i++)
            System.out.println("Name " + (i + 1) + ": "
                               + gfg.get(i));
    }
}


Java
// Java program to demonstrate the
// working of forEach method to iterate
// collection objects 
  
import java.io.*;
import java.util.*;
  
class GFG {
    public static void main(String[] args)
    {
  
        // Declaring the ArrayList
        ArrayList gfg = new ArrayList();
  
        // Appending new elements at
        // the end of the list
        gfg.add("Abhishek Rout");
        gfg.add("Vaibhav Kamble");
        gfg.add("Anupam Kumar");
  
        // forEach for iterating
        // through collection
        // with iterable variable
        System.out.println("With iterable");
        
        gfg.forEach((String name) -> {
            System.out.println("Name : " + name);
        });
  
        System.out.println("\nWithout iterable");
        gfg.forEach(System.out::println);
    }
}


输出
Name : Abhishek Rout
Name : Vaibhav Kamble
Name : Anupam Kumar

方法二:使用迭代器方法

使用的语法:

for (Iterator variable = collection.iterator(); variable.hasNext();)

例子:

Java

// Java program to demonstrate the
// working of iterator method to iterate
// collection objects 
  
import java.io.*;
import java.util.*;
  
class GFG {
    public static void main(String[] args)
    {
  
        // Declaring the LinkedList
        LinkedList gfg = new LinkedList();
  
        // Appending new elements at
        // the end of the list
        gfg.add("Abhishek Rout");
        gfg.add("Vaibhav Kamble");
        gfg.add("Anupam Kumar");
  
        // for loop for iterating
        // conditionally through collection
        System.out.println("Using For loop");
        
        for (Iterator name = gfg.iterator();
             name.hasNext();)
            
            System.out.println("Name : " + name.next());
  
          // while loop for iterating
        // conditionally through collection
        System.out.println("\nUsing While Loop");
        
        Iterator name = gfg.iterator();
  
        while (name.hasNext())
            System.out.println("Name : " + name.next());
    }
}
输出
Using For loop
Name : Abhishek Rout
Name : Vaibhav Kamble
Name : Anupam Kumar

Using While Loop
Name : Abhishek Rout
Name : Vaibhav Kamble
Name : Anupam Kumar

方法 3:使用简单的 For 循环

使用的语法:

for (int i = 0; i < collection_used.length; i++)

例子:

Java

// Java program to demonstrate the
// working of for loop to iterate
// collection objects 
  
import java.io.*;
import java.util.*;
  
class GFG {
    public static void main(String[] args)
    {
  
        // Declaring the ArrayList
        Vector gfg = new Vector();
  
        // Appending new elements at
        // the end of the list
        gfg.add("Abhishek Rout");
        gfg.add("Vaibhav Kamble");
        gfg.add("Anupam Kumar");
  
        // for loop for iterating
        // through collection
        for (int i = 0; i < gfg.size(); i++)
            System.out.println("Name " + (i + 1) + ": "
                               + gfg.get(i));
    }
}
输出
Name 1: Abhishek Rout
Name 2: Vaibhav Kamble
Name 3: Anupam Kumar

方法四:使用 forEach 方法

forEach()方法在Java 8 中可用,每个集合都有这个在内部实现迭代的方法。

使用的语法:

  • 带有可迭代变量
collection_used.forEach((data_type iterating_variable) -> { System.out.println(iterating_variable); });
  • 没有可迭代变量
collection_used.forEach(System.out::println);

例子:

Java

// Java program to demonstrate the
// working of forEach method to iterate
// collection objects 
  
import java.io.*;
import java.util.*;
  
class GFG {
    public static void main(String[] args)
    {
  
        // Declaring the ArrayList
        ArrayList gfg = new ArrayList();
  
        // Appending new elements at
        // the end of the list
        gfg.add("Abhishek Rout");
        gfg.add("Vaibhav Kamble");
        gfg.add("Anupam Kumar");
  
        // forEach for iterating
        // through collection
        // with iterable variable
        System.out.println("With iterable");
        
        gfg.forEach((String name) -> {
            System.out.println("Name : " + name);
        });
  
        System.out.println("\nWithout iterable");
        gfg.forEach(System.out::println);
    }
}
输出
With iterable
Name : Abhishek Rout
Name : Vaibhav Kamble
Name : Anupam Kumar

Without iterable
Abhishek Rout
Vaibhav Kamble
Anupam Kumar