将Ruby映射重新连接到Java映射

将Ruby映射重新连接到Java映射,java,guava,Java,Guava,我有一个Ruby结构: COUNTRIES = { 'AF' => { :country => 'Afghanistan', :alpha => 'AFG', :number => '004' }, 'AT' => { :country => 'Austria', :alpha => 'AUT', :number => '040' } } 我可以使用什么数据结构来创建相同的结构,并根据我需要的键找到适当的值?我用Guala

我有一个Ruby结构:

COUNTRIES = {
      'AF' => { :country => 'Afghanistan', :alpha => 'AFG', :number => '004' },
      'AT' => { :country => 'Austria', :alpha => 'AUT', :number => '040' }
}
我可以使用什么数据结构来创建相同的结构,并根据我需要的键找到适当的值?我用Gualava试过这个:

Table<String, String, Integer> table = HashBasedTable.create();

table.put("Austria", "AUT", 040);

但我如何在地图上标出主键呢?也许可以将表用于Hashmap?但是,如何使用一些内部键从Hashmap中获取值,您可以编写自己的类。构造函数+Getter/Setter也在那里

public class Country {
    private String country;
    private String alpha;
    private String number;
}
要存储我们类的对象,可以使用映射

Map<String, Country> table = new HashMap<>();
table.put("AF", new Country("Afghanistan", "AFG", "004");
table.put("AT", new Country("Austria", "AUT", "040");

你可以编写自己的类。构造函数+Getter/Setter也在那里

public class Country {
    private String country;
    private String alpha;
    private String number;
}
要存储我们类的对象,可以使用映射

Map<String, Country> table = new HashMap<>();
table.put("AF", new Country("Afghanistan", "AFG", "004");
table.put("AT", new Country("Austria", "AUT", "040");

用Java编写的惯用方法如下:

public class Country {
    private final String country;
    private final String alpha;
    private final String number;
    // constructor and getters
}

Map<String, Country> map = new HashMap<>();
COUNTRIES.put("AT", new Country("Austria", "AUT", "040");
// etcetera
使用Map可以实现这一点,但它更麻烦、效率更低、更脆弱1


1-如果构建地图的代码或访问地图的代码中存在键入错误,则会出现问题。例如,如果您在一个映射条目中遗漏了键入的country作为coumtry,编译器将不会选择它,并且您可能会得到意外的NPE。

用Java编写它的惯用方法如下:

public class Country {
    private final String country;
    private final String alpha;
    private final String number;
    // constructor and getters
}

Map<String, Country> map = new HashMap<>();
COUNTRIES.put("AT", new Country("Austria", "AUT", "040");
// etcetera
使用Map可以实现这一点,但它更麻烦、效率更低、更脆弱1


1-如果构建地图的代码或访问地图的代码中存在键入错误,则会出现问题。例如,如果您在一个映射条目中遗漏了键入的country作为coumtry,编译器将不会选择它,并且您可能会得到意外的NPE。

我将创建一个包含字符串country、alpha、number的唯一类;而不是将其强制放到表或地图上。在地图中创建整个地图结构太麻烦,效率也很低。你能给我举一些其他数据结构的例子吗?我会创建一个包含字符串country,alpha,number;而不是将其强制放到表或地图上。太麻烦了,在地图中创建整个地图结构效率很低。你能给我举一些其他数据结构的例子吗?有没有数据结构的解决方案,因为我想使用默认方法查找键或值?有没有数据结构的解决方案,因为我想使用默认方法查找键或值值?你能告诉我如何查询一个键吗?例如,如果我使用此方法?你能给我一个例子说明你的意思吗?如果我有键AUT,我如何查询数据结构并得到值040?这就是你的意思吗?你能告诉我如何查询一个键吗?例如,如果我使用此方法,你能给我一个例子吗你的意思是什么?如果我有AUT键,我如何查询数据结构并得到值040?这就是你的意思吗?