复习使用~java枚举类的使用(接口,自定义,enum,map)

枚举类

  今天复习枚举类时,印象不是很深刻,通过查找资料,重新整理了一下枚举类具体的使用。由于懒的原因(就像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)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值