Java中基于两个整数查找特定枚举的最快方法

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java中基于两个整数查找特定枚举的最快方法相关的知识,希望对你有一定的参考价值。

我有以下三个枚举:

PlayerType

public enum PlayerType 
    NONE (' '),
    BLACK ('b'),
    WHITE ('w');

    private final char _symbol;

    PlayerType(char symbol) 
        _symbol = symbol;
    

    public char symbol()  return _symbol; 

PieceType

public enum PieceType 
    NONE (' '),
    PAWN ('p'),
    KNIGHT ('n'),
    BISHOP ('b'),
    ROOK ('r'),
    QUEEN ('q'),
    KING ('k');

    private final char _symbol;

    PieceType(char symbol) 
        _symbol = symbol;
    

    public char symbol()  return _symbol; 

PlayerPiece

public enum PlayerPiece 
    NONE (PlayerType.NONE, PieceType.NONE),
    BLACK_PAWN (PlayerType.BLACK, PieceType.PAWN),
    BLACK_KNIGHT (PlayerType.BLACK, PieceType.KNIGHT),
    BLACK_BISHOP (PlayerType.BLACK, PieceType.BISHOP),
    BLACK_ROOK (PlayerType.BLACK, PieceType.ROOK),
    BLACK_QUEEN (PlayerType.BLACK, PieceType.QUEEN),
    BLACK_KING (PlayerType.BLACK, PieceType.KING),
    WHITE_PAWN (PlayerType.WHITE, PieceType.PAWN),
    WHITE_KNIGHT (PlayerType.WHITE, PieceType.KNIGHT),
    WHITE_BISHOP (PlayerType.WHITE, PieceType.BISHOP),
    WHITE_ROOK (PlayerType.WHITE, PieceType.ROOK),
    WHITE_QUEEN (PlayerType.WHITE, PieceType.QUEEN),
    WHITE_KING (PlayerType.WHITE, PieceType.KING);

    private final PlayerType _playerType;
    private final PieceType _pieceType;
    private final char[] _symbol;

    private PlayerPiece(
      final PlayerType playerType,
      final PieceType pieceType) 
        _playerType = playerType;
        _pieceType = pieceType;
        _symbol = new char[] playerType.symbol(), pieceType.symbol();
    

    public final PlayerType playerType()  return _playerType; 
    public final PieceType pieceType()  return _pieceType; 
    public final String symbol()  return String.valueOf(_symbol); 

类似...的查找

int playerTypeOrdinal = PlayerType.WHITE.ordinal();
int pieceTypeOrdinal = PieceType.QUEEN.ordinal();
PlayerPiece playerPiece = Lookup(playerTypeOrdinal, pieceTypeOrdinal)

...应该尽快返回WHITE_QUEEN PlayerPiece枚举。我可以使用for循环,但是我不确定这是最快的方法。我想使用静态地图,但不确定如何使用两个序数作为键。

任何建议如何有效地做到这一点?

来自接受解答的解决方案

即使是交错数组也可以,但是,如果我不使它们平行,则有可能抛出ArrayIndexOutOfBoundsException:

public class PlayerPieceLookup 

    private static final PlayerPiece[][] PLAYER_PIECES = new PlayerPiece[][] 
        
            PlayerPiece.NONE,
            PlayerPiece.NONE,
            PlayerPiece.NONE,
            PlayerPiece.NONE,
            PlayerPiece.NONE,
            PlayerPiece.NONE,
            PlayerPiece.NONE,
        ,
        
            PlayerPiece.NONE,
            PlayerPiece.BLACK_PAWN, 
            PlayerPiece.BLACK_KNIGHT, 
            PlayerPiece.BLACK_BISHOP, 
            PlayerPiece.BLACK_ROOK, 
            PlayerPiece.BLACK_QUEEN, 
            PlayerPiece.BLACK_KING
        ,
        
            PlayerPiece.NONE,
            PlayerPiece.WHITE_PAWN, 
            PlayerPiece.WHITE_KNIGHT,
            PlayerPiece.WHITE_BISHOP, 
            PlayerPiece.WHITE_ROOK, 
            PlayerPiece.WHITE_QUEEN, 
            PlayerPiece.WHITE_KING
               
    ;  

    public static void main(String[] args) 
        for(PlayerType player : PlayerType.values()) 
            for(PieceType piece : PieceType.values()) 
                System.out.println("[" + player + "][" + piece + "][" + PLAYER_PIECES[player.ordinal()][piece.ordinal()] + "]");        
            
        
    
 
答案

2D阵列可以非常有效地完成此操作。只需填充一个数组(例如,在一个静态块中),使arr[m][n]是第m个播放器,第n个播放器。查找将是两个数组加载(因为Java 2d数组是arrays-of-arrays)。

另一答案

请勿使用ordinal。这不是它的目的。本质上,这是一个内部实现细节。

这也不是类型安全的(如果您以错误的方式弄错了两个序号该怎么办?

直接使用枚举值从EnumMap中查找。

Map<PlayerType, Map<PieceType, PlayerPiece>> map = new EnumMap<>(PlayerType.class);
for (PlayerPiece p : PlayerPiece.values()) 
  map.computeIfAbsent(p.playerType(), k -> new EnumMap<>(PieceType.class))
      .put(p.pieceType(), p);

然后使用:查找片段:

map.get(playerType).get(pieceType)

以上是关于Java中基于两个整数查找特定枚举的最快方法的主要内容,如果未能解决你的问题,请参考以下文章

在 C# 中查找两个集合的补集的最快方法

给定数组和一个常量,从数组中找到两个数之和等于常量,如何做最快

在 Java 中随机化整数数组的最快方法

获取列表中最小元素少于特定元素的最快方法

检查两个整数是不是在 0 的同一侧的最快方法

使用java编写两个方法min和max在链表中查找最大值和最小值,但输入列表是整数数组