Java 从列表中获取值的最佳编程实践
我正在用Java开发android应用程序。 我需要我的应用程序从国家代码中获取电话前缀 例如,如果国家代码是US,它应该返回前缀为“+1” 如果代码为in,则应返回“+91”,以此类推 这可以通过使用if-else块的函数实现,如下所示:Java 从列表中获取值的最佳编程实践,java,android,Java,Android,我正在用Java开发android应用程序。 我需要我的应用程序从国家代码中获取电话前缀 例如,如果国家代码是US,它应该返回前缀为“+1” 如果代码为in,则应返回“+91”,以此类推 这可以通过使用if-else块的函数实现,如下所示: String getPrefix(String iso){ String prefix = ""; if(iso.equalsIgnoreCase("AD")) prefix = "376"; else if(iso
String getPrefix(String iso){
String prefix = "";
if(iso.equalsIgnoreCase("AD")) prefix = "376";
else if(iso.equalsIgnoreCase("AE")) prefix = "971";
else if(iso.equalsIgnoreCase("AF")) prefix = "93";
else if(iso.equalsIgnoreCase("AG")) prefix = "268";
else if(iso.equalsIgnoreCase("AI")) prefix = "264";
....
....
return prefix;
}
或者我们可以有一个包含所有键值对的大向量对象,并且可以通过调用该对象上的get方法来检索前缀
我需要在程序生命周期中调用此函数一次。请告诉我实现这一点的最佳逻辑
关于。不确定是否存在这样的“编程实践”,但有一种数据结构仅适用于这种情况 不确定是否存在这样的“编程实践”,但有一种数据结构只适用于这种情况 您可以使用
枚举
public enum Code {
AD("376"),
AE("971")
...
private String prefix;
private Code(String prefix) { this.prefix = prefix; }
public String getPrefix() { return prefix; }
}
我认为前缀的数量非常有限(可能只有几百个),所以性能不会成为问题
正如建议的那样,您可以使用code#valueOf()
这将在搜索时提供更好的性能:
//get codes
Code c = Code.valueOf("AD");
// remember to check for null, if getting the code from user input
String prefix = c.getPrefix();
您必须进行基准测试,看看它是否比使用
HashMap
慢得多,但它在可读性方面做了很多工作…您可以使用enum
public enum Code {
AD("376"),
AE("971")
...
private String prefix;
private Code(String prefix) { this.prefix = prefix; }
public String getPrefix() { return prefix; }
}
我认为前缀的数量非常有限(可能只有几百个),所以性能不会成为问题
正如建议的那样,您可以使用code#valueOf()
这将在搜索时提供更好的性能:
//get codes
Code c = Code.valueOf("AD");
// remember to check for null, if getting the code from user input
String prefix = c.getPrefix();
您必须进行基准测试,看看它是否比使用HashMap慢得多,但它对可读性有很大帮助…我建议您将其存储在HashMap中:
import java.util.HashMap;
import java.util.Map;
class YourActivity {
// ....
private Map<String, String> prefixes = new HashMap<String, String>();
{
prefixes.put("AD", "376");
//... and the rest of them
}
public String getPrefix(String state) {
if(prefixes.containsKey(state)) {
return prefixes.get(state);
}
// handle the case for unkown states
return "";
}
}
import java.util.HashMap;
导入java.util.Map;
课堂活动{
// ....
私有映射前缀=new HashMap();
{
前缀。put(“AD”,“376”);
//…还有其他人
}
公共字符串getPrefix(字符串状态){
if(前缀.containsKey(状态)){
返回前缀.get(state);
}
//处理未知状态的情况
返回“”;
}
}
我建议您将其存储在HashMap中:
import java.util.HashMap;
import java.util.Map;
class YourActivity {
// ....
private Map<String, String> prefixes = new HashMap<String, String>();
{
prefixes.put("AD", "376");
//... and the rest of them
}
public String getPrefix(String state) {
if(prefixes.containsKey(state)) {
return prefixes.get(state);
}
// handle the case for unkown states
return "";
}
}
import java.util.HashMap;
导入java.util.Map;
课堂活动{
// ....
私有映射前缀=new HashMap();
{
前缀。put(“AD”,“376”);
//…还有其他人
}
公共字符串getPrefix(字符串状态){
if(前缀.containsKey(状态)){
返回前缀.get(state);
}
//处理未知状态的情况
返回“”;
}
}
我会使用一个由静态HashMap支持的独特类型,例如
public class CallingCode {
private static HashMap<String, CallingCode> callingCodes;
static {
callingCodes.put("AD", new CallingCode("AD", 376));
...
}
private final String country;
private final int code;
public CallingCode(String country, int code) {
this.country = country;
this.code = code;
}
public static CallingCode getInstance(country) {
if (country == null) {
return null;
}
return callingCodes.get(country.toUpperCase());
}
// implement equals / hashCode
}
公共类调用代码{
私有静态哈希映射调用码;
静止的{
调用代码。放置(“AD”,新调用代码(“AD”,376));
...
}
私人国家;
私有最终整数码;
公共呼叫代码(字符串国家/地区,整数代码){
这个国家=国家;
this.code=代码;
}
公共静态调用代码getInstance(国家/地区){
如果(国家==null){
返回null;
}
返回callingCodes.get(country.toUpperCase());
}
//实现equals/hashCode
}
每个调用代码都将在类加载时创建,因此您可能希望对其进行调整,使其按需实例化每个实例。我将使用一个独特的类型,由静态哈希映射支持,例如
public class CallingCode {
private static HashMap<String, CallingCode> callingCodes;
static {
callingCodes.put("AD", new CallingCode("AD", 376));
...
}
private final String country;
private final int code;
public CallingCode(String country, int code) {
this.country = country;
this.code = code;
}
public static CallingCode getInstance(country) {
if (country == null) {
return null;
}
return callingCodes.get(country.toUpperCase());
}
// implement equals / hashCode
}
公共类调用代码{
私有静态哈希映射调用码;
静止的{
调用代码。放置(“AD”,新调用代码(“AD”,376));
...
}
私人国家;
私有最终整数码;
公共呼叫代码(字符串国家/地区,整数代码){
这个国家=国家;
this.code=代码;
}
公共静态调用代码getInstance(国家/地区){
如果(国家==null){
返回null;
}
返回callingCodes.get(country.toUpperCase());
}
//实现equals/hashCode
}
每个调用代码都将在类加载时创建,因此您可能希望对其进行调整,使其按需实例化每个实例。您可以使用
HashMap
集合解决此问题:
国家代码变为键
,其前缀值变为值
您可以将所有这些名称-值对放入配置文件(.txt或xml等)中,并使用
put(key,value)
方法填充HashMap
。然后使用get(Key)
方法可以检索其值。您可以使用HashMap
集合解决此问题:
国家代码变为键
,其前缀值变为值
您可以将所有这些名称-值对放入配置文件(.txt或xml等)中,并使用
put(key,value)
方法填充HashMap
。然后使用get(Key)
方法可以检索它的值。使用带有枚举的开关语句,这是最快的解决方案
首先你必须这样做。然后您可以使用带有enum的switch语句,这是最快的解决方案
首先你必须这样做。然后你就可以了。你应该使用一个
映射,首先用前缀定义一个属性(可能是静态的):
private Map<String, String> prefixes = new HashMap<String, String>();
// fill the elements in the constructor, or in a static block
prefixes.put("AD", "376");
prefixes.put("AE", "971");
prefixes.put("AF", "93");
prefixes.put("AG", "268");
prefixes.put("AI", "264");
您应该使用映射
为此,首先定义一个带有前缀的属性(可能是静态的):
private Map<String, String> prefixes = new HashMap<String, String>();
// fill the elements in the constructor, or in a static block
prefixes.put("AD", "376");
prefixes.put("AE", "971");
prefixes.put("AF", "93");
prefixes.put("AG", "268");
prefixes.put("AI", "264");
我不能说不同解决方案的相对速度,但使用枚举似乎更易使用和维护:
public class EnumTest {
public enum DialPlan {
US("+1"),
AD("+376"),
AE("+971"),
AF("+93"),
AG("+268"),
AI("+264");
final String m_code;
private DialPlan(String code) { m_code = code; }
}
public static void main(String[] args) {
for(String arg : args) {
System.out.println(arg + ": " + DialPlan.valueOf(arg).m_code);
}
}
}
我不能说不同解决方案的相对速度,但使用枚举似乎更易使用和维护:
public class EnumTest {
public enum DialPlan {
US("+1"),
AD("+376"),
AE("+971"),
AF("+93"),
AG("+268"),
AI("+264");
final String m_code;
private DialPlan(String code) { m_code = code; }
}
public static void main(String[] args) {
for(String arg : args) {
System.out.println(arg + ": " + DialPlan.valueOf(arg).m_code);
}
}
}
很抱歉,从国家代码到电话前缀的映射不符合“庞大列表”的条件。请仅在您的列表超过数百万条时使用该术语。:)我将使用一个HashMap,带有字符串键和整数值。这个