解决switch……case不能匹配字符串的方法
2016-03-22 23:51
579 查看
以前写代码的时候,没有考虑到效率的问题。if……else if……else if 写了老长。如果数据量特别大的话,其实会影响到程序的效率。因为,if语句是从上到下一个个判断的,直到条件为真才退出。而switch……case 则不是从上到下进行验证的,因此效率要比if else高。
可惜的是,switch case 不支持字符串,着实让我伤脑筋。今天在网上查了些资料,得到一些提示,拿出来晒晒。
if……else例子:
public void ifElse(){
String name="haha";
if(name.equals("yes")){
System.out.println("is yes");
}
else if(name.equals("hello")){
System.out.println("is hello");
}
else if(name.equals("in")){
System.out.println("is in");
}
else if(name.equals("haha")){
System.out.println("is haha");
}
}
代码难看,如果if 再长一点,就难受了。
用switch case 实现:
public void switchCaseStr() {
Map<String,Integer> map=new HashMap<String, Integer>();
map.put("hello", 1);
map.put("haha", 2);
map.put("yes", 3);
map.put("in", 4);
String str="hello";
switch(map.get(str))
{
case 3:
System.out.println("yes");
break;
case 1:
System.out.println("hello");
break;
case 2:
System.out.println("haha");
break;
case 4:
System.out.println("in");
break;
default:
System.out.println("default");
}
}
不仅从效率上有提高,而且代码也更好看。当然,switch case 还可以嵌套使用。
public void switchCaseStr(){
Map<String,Integer> map=new HashMap<String, Integer>();
map.put("hello", 1);
map.put("haha", 2);
map.put("yes", 3);
map.put("in", 4);
String str="hello";
switch(map.get(str))
{
case 3:
System.out.println("yes");
break;
case 1:
System.out.println("hello");
String st="in";
switch(map.get(st))
{
case 2:
System.out.println("haha");
break;
case 4:
System.out.println("in");
break;
}
break;
default:
System.out.println("default");
}
}
当然,也可以有其它的方法实现。本人喜欢用hashmap,效率应该比 List好一点吧,个人觉得。
可惜的是,switch case 不支持字符串,着实让我伤脑筋。今天在网上查了些资料,得到一些提示,拿出来晒晒。
if……else例子:
public void ifElse(){
String name="haha";
if(name.equals("yes")){
System.out.println("is yes");
}
else if(name.equals("hello")){
System.out.println("is hello");
}
else if(name.equals("in")){
System.out.println("is in");
}
else if(name.equals("haha")){
System.out.println("is haha");
}
}
代码难看,如果if 再长一点,就难受了。
用switch case 实现:
public void switchCaseStr() {
Map<String,Integer> map=new HashMap<String, Integer>();
map.put("hello", 1);
map.put("haha", 2);
map.put("yes", 3);
map.put("in", 4);
String str="hello";
switch(map.get(str))
{
case 3:
System.out.println("yes");
break;
case 1:
System.out.println("hello");
break;
case 2:
System.out.println("haha");
break;
case 4:
System.out.println("in");
break;
default:
System.out.println("default");
}
}
不仅从效率上有提高,而且代码也更好看。当然,switch case 还可以嵌套使用。
public void switchCaseStr(){
Map<String,Integer> map=new HashMap<String, Integer>();
map.put("hello", 1);
map.put("haha", 2);
map.put("yes", 3);
map.put("in", 4);
String str="hello";
switch(map.get(str))
{
case 3:
System.out.println("yes");
break;
case 1:
System.out.println("hello");
String st="in";
switch(map.get(st))
{
case 2:
System.out.println("haha");
break;
case 4:
System.out.println("in");
break;
}
break;
default:
System.out.println("default");
}
}
当然,也可以有其它的方法实现。本人喜欢用hashmap,效率应该比 List好一点吧,个人觉得。
相关文章推荐
- java jdbc线程池的使用
- 解析json数据
- XCode所有版本
- Homework 4
- 自动展示收起广告功能
- [LinkedList和数组]——自定义Map升级版
- python之类-django进阶
- Fragment的startActivityForResult详细解决方案
- 结对项目 - 词频统计
- CALayer动画实践(二):CAReplicatorLayer的用法
- leetcode014 Longest Common Prefix
- Netty系列之Netty百万级推送服务设计要点
- Android中常见IPC方法总结
- 206. Reverse Linked List
- 神坑,mac 10.11.2 安装cocoapods
- 因Window服务器自动更新并重启导致WebSphere服务停止服务故障一例
- 如何每日增量加载数据到Hive分区表
- hihoCoder 1032 最长回文子串
- 写一个程序,用于分析一个字符串中各个单词出现的频率,并将单词和它出现的频率输出显示。
- 表单