这是一小段代码,把字符串劈到MAP中, 对MAP的KEY排序,用KEY与输入值比较,记录INDEX,确认后以INDEX为KEY,取MAP值。
public static void main(String[] args) {
int v = 6;
String peifu = "1:3,5:7,6:8,9:11";
String[] sp = peifu.split(",");
Map<String, String> map = new HashMap<String, String>();
for (String s : sp) {
String[] t = s.split(":");
map.put(t[0],t[1]);
}
List<String> list = new ArrayList<String>();
list.addAll(map.keySet());
Collections.sort(list);
int index = 0;
for (String s : list) {
if(v > Integer.valueOf(s).intValue()){
index++;
}
}
System.out.println(map.get(list.get(index)));
}
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/100457.html原文链接: