📜  在Java中迭代枚举值

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

在Java中迭代枚举值

Java中的枚举是一种包含一组固定常量的数据类型。 Java枚举类型是一种特殊的Java类。枚举可以包含常量、方法等。

下面给出了如何声明枚举的演示:

Java
// Demonstrates the declaration of enum
public enum season {
    Spring,
    Summer,
    Monsoon,
    Autumn,
    Winter
}


Java
// Program that demonstrates iteration
// over enums using forEach()
import java.util.Arrays;
import java.util.EnumSet;
 
public class GFG {
    public enum seasons {
        Spring,
        Summer,
        Monsoon,
        Autumn,
        Winter
    }
    public static void main(String[] args)
    {
 
        // Convert enum to set and apply forEach()
        EnumSet.allOf(seasons.class)
            .forEach(season -> System.out.println(season));
 
        // Convert enum to set and apply forEach()
        Arrays.asList(seasons.values())
            .forEach(season -> System.out.println(season));
    }
}


Java
// Program that demonstrates iteration
// over enums using for loop
public class GFG {
    // enum of seasons
    public enum seasons {
        Spring,
        Summer,
        Monsoon,
        Autumn,
        Winter
    }
    public static void main(String[] args)
    {
        // iterate over enums using for loop
        for (seasons s : seasons.values()) {
            System.out.println(s);
        }
    }
}


Java
// Program that demonstrates iteration
// over enums using java.util.stream
 
import java.util.stream.Stream;
 
public class GFG {
    public enum seasons {
        Spring,
        Summer,
        Monsoon,
        Autumn,
        Winter
    }
    public static void main(String[] args)
    {
        // iteration over enum using java.util.stream
        Stream.of(seasons.values())
            .forEach(System.out::println);
    }
}


迭代枚举值:有很多方法可以迭代枚举值:

  1. 使用forEach()进行迭代
  2. 使用for 循环迭代
  3. 使用Java.util.stream进行迭代

使用 forEach() 进行迭代:forEach() 方法适用于列表或集合。对于使用 forEach() 方法,将枚举转换为列表或集合。

将枚举转换为 set 并应用 forEach 如下所示:

EnumSet.allOf(seasons.class)
            .forEach(season -> System.out.println(season));

将 enum 转换为 list 并应用 forEach 如下所示:

Arrays.asList(seasons.values()).
                forEach(season -> System.out.println(season));

下面的程序说明了使用 forEach() 对枚举的迭代:

Java

// Program that demonstrates iteration
// over enums using forEach()
import java.util.Arrays;
import java.util.EnumSet;
 
public class GFG {
    public enum seasons {
        Spring,
        Summer,
        Monsoon,
        Autumn,
        Winter
    }
    public static void main(String[] args)
    {
 
        // Convert enum to set and apply forEach()
        EnumSet.allOf(seasons.class)
            .forEach(season -> System.out.println(season));
 
        // Convert enum to set and apply forEach()
        Arrays.asList(seasons.values())
            .forEach(season -> System.out.println(season));
    }
}
输出
Spring
Summer
Monsoon
Autumn
Winter
Spring
Summer
Monsoon
Autumn
Winter

使用 for 循环迭代: Java.lang 的静态 values() 方法。 Enum 类给出了一个枚举值数组。在获得枚举值数组后,可以使用 for 循环对数组进行迭代。

下面的程序说明了使用 for 循环对枚举的迭代:

Java

// Program that demonstrates iteration
// over enums using for loop
public class GFG {
    // enum of seasons
    public enum seasons {
        Spring,
        Summer,
        Monsoon,
        Autumn,
        Winter
    }
    public static void main(String[] args)
    {
        // iterate over enums using for loop
        for (seasons s : seasons.values()) {
            System.out.println(s);
        }
    }
}
输出
Spring
Summer
Monsoon
Autumn
Winter

使用Java.util.stream 进行迭代:也可以使用Java.util.stream类对枚举进行迭代。使用Stream.of()方法创建enum.values()流,方法是将 eum.values() at parameter 传递给函数。

下面的程序说明了使用Java对枚举的迭代。实用程序流:

Java

// Program that demonstrates iteration
// over enums using java.util.stream
 
import java.util.stream.Stream;
 
public class GFG {
    public enum seasons {
        Spring,
        Summer,
        Monsoon,
        Autumn,
        Winter
    }
    public static void main(String[] args)
    {
        // iteration over enum using java.util.stream
        Stream.of(seasons.values())
            .forEach(System.out::println);
    }
}
输出
Spring
Summer
Monsoon
Autumn
Winter

参考: https: Java/javaOO/enum.html