Java 实现参数的数据结构和查询
我想创建以下数据结构:Java 实现参数的数据结构和查询,java,data-structures,Java,Data Structures,我想创建以下数据结构: Map<String, Country> table = new HashMap<>(); table.put("AF", new Country("Afghanistan", "AFG", "004")); // 300 more class Country { private String country; private String alpha3; private String num
Map<String, Country> table = new HashMap<>();
table.put("AF", new Country("Afghanistan", "AFG", "004"));
// 300 more
class Country {
private String country;
private String alpha3;
private String number;
public Country(String country, String alpha3, String number) {
this.country = country;
this.alpha3 = alpha3;
this.number = number;
}
}
Map table=newhashmap();
表1.put(“AF”,新国家(“阿富汗”、“AFG”、“004”);
//300多
阶级国家{
私人国家;
私有字符串alpha3;
私有字符串编号;
公共国家(字符串国家、字符串字母3、字符串编号){
这个国家=国家;
这1.alpha3=alpha3;
这个数字=数字;
}
}
第二种方式:
Map<String, Country> table = new HashMap<>();
Table<String, String, Integer> table2 = HashBasedTable.create();
table.put("AF", HashBasedTable.create().put("Afghanistan", "AFG", "004"));
// 300 more
Map table=newhashmap();
table2=HashBasedTable.create();
table.put(“AF”,HashBasedTable.create().put(“Afghanistan”,“AFG”,“004”);
//300多
我想根据发送的键将值搜索到数据结构值中:
- 来自国家/地区名称的数字代码
- 来自国家名称的国家代码
- 来自国家名称的字母3代码
在第二个数据结构中实现此搜索的基本方法是什么?
HashMap
包含一个名为forEach的方法(BiConsumernew Country
这不会编译,您需要将地图声明为:map
(智能指针更好)什么是<代码> HASBASEDATABOS/CODE?这是来自GuavaLabueCudio> COM.Cuff.Cuth.Hyth.HasBaseDeab[</Cord] >抱歉,我认为是C++。
//Search a HashMap of employee ID/name records by name, for their ID number.
public int getID(HashMap<Integer, String> employees, String name) {
//Iterate through every key-value pair, performing the lambda's operation on it.
employees.forEach((Integer j, String k) -> {
if(k.equals(name)) return Integer.valueOf(j); //If the value matches the name, return the ID
}
return -1;
}