admin管理员组文章数量:1487745
字符串处理,map使用 key value迭代器使用,switch使用
字符串处理,map使用 key value迭代器使用,switch使用
代码语言:javascript代码运行次数:0运行复制package com.example.core.mydemo.java2;
import java.util.*;
public class SwitchTest {
public static void main(String[] args) {
//switch使用
/**
* output:
* convertCouponType1=A
* convertCouponType2=B
* convertCouponType3=C
* convertCouponType4=B
*/
System.out.println("convertCouponType1=" + convertCouponType("1"));
System.out.println("convertCouponType2=" + convertCouponType("2"));
System.out.println("convertCouponType3=" + convertCouponType("3"));
System.out.println("convertCouponType4=" + convertCouponType("4"));
//字符串处理
String s1 = "上海+aaa+1";
String s2 = "北京+aaa+1";
String s3 = "深圳+ccc+3";
List<String> list = new ArrayList<String>();
list.add(s1);
list.add(s2);
list.add(s3);
int idx0 = s1.indexOf("+");
/**
* output:
* idx0=2
* idx1=+aaa+1
* idx2=上海
*/
System.out.println("idx0=" + idx0);
System.out.println("idx1=" + s1.substring(idx0));
System.out.println("idx2=" + s1.substring(0,idx0));
/**
* 解析的字符串,后面不变的是 key,前面城市的是value
*/
Map<String,String> map = new HashMap<>();
for (String ss : list) {
int idx = ss.indexOf("+");
if(map.containsKey(ss.substring(idx))){
map.put(ss.substring(idx), map.get(ss.substring(idx)) + "/" + ss.substring(0, idx));
}else{
map.put(ss.substring(idx), ss.substring(0, idx));
}
}
/**
* output:
* map={+ccc+3=深圳, +aaa+1=上海/北京}
*/
System.out.println("map=" + map);
//map判断是否存在key
// map.put("aaa","上海");
// if(map.containsKey("aaa")) {
// map.put("aaa", map.get("aaa")+"/"+"北京");
// }
/**
* 数据还原
*/
List<String> handleList = new ArrayList<>();
Iterator iter = map.keySet().iterator();
while(iter.hasNext()){
String key = (String)iter.next();
//还原:将value 城市 + key 后面不变的字符
/**
* output:
* test=深圳+ccc+3
* test=上海/北京+aaa+1
*/
System.out.println("test=" + map.get(key) + key);
handleList.add(map.get(key) + key);
}
/**
* handleList=[深圳+ccc+3, 上海/北京+aaa+1]
*/
System.out.println("handleList=" + handleList);
}
private static String convertCouponType(String couponTypeParam) {
switch (couponTypeParam){
case "1":
return "A";
case "2":
return "B";
case "3":
return "C";
}
return "B";
}
}
代码语言:javascript代码运行次数:0运行复制package com.example.core.mydemo.java;
import java.util.*;
/**
* output:
* map2={carPlatNum2=车B6991B2, CarPlatNum=车B6991B, carPlatNum=车B6991B}
* entity1=车B6991B2
* entity1=车B6991B
* entity1=车B6991B
* entity2=车B6991B2
* entity2=车B6991B
* entity2=车B6991B
*/
public class MapTest {
public static void main(String[] args) {
Map map = new HashMap();
map.put("carPlatNum","车B6991B");
map.put("CarPlatNum","车B6991B");
Map map2 = new HashMap();
map2.put("carPlatNum2","车B6991B2");
map2.put("CarPlatNum","车B6991B2");
// map.putAll(map2);
// //output: map={CarPlatNum=车B6991B2, carPlatNum2=车B6991B2, carPlatNum=车B6991B}
// System.out.println("map=" + map);
map2.putAll(map);
//output: map2={carPlatNum2=车B6991B2, CarPlatNum=车B6991B, carPlatNum=车B6991B}
System.out.println("map2=" + map2);
Set set = map2.keySet();
Iterator<String> iterator = set.iterator();
while(iterator != null && iterator.hasNext()) {
String key = iterator.next();
System.out.println("entity1=" + map2.get(key));
}
//根据值遍历
Collection set2 = map2.values();
Iterator<String> iterator2 = set2.iterator();
while(iterator2 != null && iterator2.hasNext()) {
String entity = iterator2.next();
System.out.println("entity2=" + entity);
}
}
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。 原始发表:2024-07-02,如有侵权请联系 cloudcommunity@tencent 删除mapstringsystem字符串key本文标签: 字符串处理map使用 key value迭代器使用switch使用
版权声明:本文标题:字符串处理,map使用 key value迭代器使用,switch使用 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/shuma/1754969939a3181860.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论