枚举类
今天复习枚举类时,印象不是很深刻,通过查找资料,重新整理了一下枚举类具体的使用。由于懒的原因(就像java中的语法糖的出现),直接上传代码,注释直接放在代码中,不再进行排版,谅解。
枚举值的定义
package com.example.enumpackage;
/**
* @author lqh
* @date 2020/6/9
*/
public class EnumCreateTest {
/**
* 枚举类
* 类的对象只有有限个,确定的
*当我们定义一组常量时,强烈建议使用枚举类
* 枚举类定义
* 方式一:jdk1.5之后提供enum
* 方式二:jdk1.5之前,自定义枚举类
* 定义的枚举类默认继承ENUM类
*
* ENUM类中的常用方法
* values() :返回枚举类类型的对象数组
* valueof() :返回带指定名称的指定枚举类型的枚举常量。,要求字符串必须是枚举类对象
* toString() :返回枚举常量的名称
*
*/
public static void main(String[] args) {
//自定义
Season season=Season.SPRING;
System.out.println(season);
//ENUM
Season1 season1=Season1.SPRING;
System.out.println(season1.toString());
System.out.println("_______________________");
Season1[] values = season1.values();
for (int i=0;i<values.length;i++){
System.out.println(values[i]);
}
System.out.println("_______________________");
Season1 winter = season1.valueOf("WINTER");
System.out.println(winter);
}
}
//使用enum定义
enum Season1{
//提供当前枚举类的多个对象
SPRING("SPRING","SPRING..."),
SUMMER("SUMMER","SUMMER..."),
AUTUMN("AUTUMN","AUTUMN..."),
WINTER("WINTER","WINTER...");
//声明Season对象的属性
private final String seasonName;
private final String seasonDesc;
//1.私有化构造器(防止被外部调用)
private Season1(String seasonName,String seasonDesc){
this.seasonName=seasonName;
this.seasonDesc=seasonDesc;
}
}
//自定义枚举类
class Season{
//声明Season对象的属性
private final String seasonName;
private final String seasonDesc;
public String getSeasonName() {
return seasonName;
}
public String getSeasonDesc() {
return seasonDesc;
}
@Override
public String toString() {
return "Season{" +
"seasonName='" + seasonName + '\'' +
", seasonDesc='" + seasonDesc + '\'' +
'}';
}
//1.私有化构造器
private Season(String seasonName,String seasonDesc){
this.seasonName=seasonName;
this.seasonDesc=seasonDesc;
}
//提供当前枚举类的多个对象(public static final)修饰
public static final Season SPRING=new Season("SPRING","SPRING...");
public static final Season SUMMER=new Season("SUMMER","SUMMER...");
public static final Season AUTUMN=new Season("AUTUMN","AUTUMN...");
public static final Season WINTER=new Season("WINTER","WINTER...");
}
枚举值的使用
package com.example.enumpackage;
import org.junit.jupiter.api.Test;
/**
* @author lqh
* @date 2020/6/9
*/
public class EnumUseTest {
SignalEnum color=SignalEnum.RED;
@Test
public void change(){
switch (color){
case RED:
color= SignalEnum.GREEN;
break;
case GREEN:
color=SignalEnum.RED;
break;
case YELLOW:
color=SignalEnum.YELLOW;
break;
default:
color=null;
}
System.out.println(color.toString());
//枚举类的比较使用==还是equals?
/* public final boolean equals(Object other) {
return this==other;
}*/
/**
* 源码中equals方法是使用的“==”进行比较的
* **但是重点并不是重写equals方法的原因,实际上枚举的赋值不是new出来的,直接指向的引用地址
* (枚举元素的构造函数呗私有化,无法直接new一个元素)。
*/
System.out.println(color.equals(SignalEnum.GREEN)); //true
System.out.println(color==SignalEnum.GREEN); //true
}
}
interface Inter{
void print();
}
enum SignalEnum implements Inter{
GREEN,YELLOW,RED;
@Override
public void print(){
System.out.println("重写方法。。。");
}
}
枚举值的map
package com.example.enumpackage;
import org.junit.jupiter.api.Test;
import java.util.*;
/**
* @author lqh
* @date 2020/6/9
*/
public class EnumMapTest {
@Test
public void enumTest(){
List<Clothes> list = new ArrayList<>();
list.add(new Clothes("C001",Color.BLUE));
list.add(new Clothes("C002",Color.YELLOW));
list.add(new Clothes("C003",Color.RED));
list.add(new Clothes("C004",Color.GREEN));
list.add(new Clothes("C005",Color.BLUE));
list.add(new Clothes("C006",Color.BLUE));
list.add(new Clothes("C007",Color.RED));
list.add(new Clothes("C008",Color.YELLOW));
list.add(new Clothes("C009",Color.YELLOW));
list.add(new Clothes("C010",Color.GREEN));
//方案1:使用HashMap
Long hashStartTime=System.nanoTime();
Map<String,Integer> map = new HashMap<>();
for (Clothes clothes:list){
String colorName=clothes.getColor().name();
Integer count = map.get(colorName);
if(count!=null){
map.put(colorName,count+1);
}else {
map.put(colorName,1);
}
}
System.out.println(map.toString());
System.out.println("执行时间"+(System.nanoTime()-hashStartTime));
System.out.println("-----------------------------------------");
//方案2:使用EnumMap
/**
* EnumMap要求其Key必须为Enum类型,且不能为空
* EnumMap使用数组来存放与枚举类型对应的值,数组是一段连续的内存空间,效率高。
*/
Long enumStartTime=System.nanoTime();
Map<Color,Integer> enumMap=new EnumMap<>(Color.class);
for (Clothes clothes:list){
Color color=clothes.getColor();
Integer count = enumMap.get(color);
if(count!=null){
enumMap.put(color,count+1);
}else {
enumMap.put(color,1);
}
}
System.out.println(enumMap.toString());
System.out.println("执行时间"+(System.nanoTime()-enumStartTime));
}
}
/**
* 打印结果
* {RED=2, BLUE=3, YELLOW=3, GREEN=2}
* 执行时间178900
* -----------------------------------------
* {GREEN=2, RED=2, BLUE=3, YELLOW=3}
* 执行时间1350300
*/
enum Color{
GREEN,RED,BLUE,YELLOW
}
class Clothes{
public String id;
public Color color;
public Clothes(String id,Color color){
this.id=id;
this.color=color;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public Color getColor() {
return color;
}
public void setColor(Color color) {
this.color = color;
}
}
枚举值的的特点
- 无法继承其他类(已经默认继承Enum)
- 枚举类是线程安全的
- 枚举类型是类型安全的(typesafe)
尊重 原创, 参考 地址: (http://blog.youkuaiyun.com/javazejian)