[英]how to sort a map by its key's under score occurrences
i have a map having我有一张地图
map.put("100_101","lll")
map.put("100_102","lll")
map.put("100_101_103","lll")
map.put("100_101_104","lll")
map.put("100","lll")
expected result is order by map key number of underscore occurrences.预期结果是按地图键下划线出现次数排序。
map.put("100","lll")
map.put("100_101","lll")
map.put("100_102","lll")
map.put("100_101_103","lll")
map.put("100_101_104","lll")
Java 7 allows underscores ("_") in the numeric literals as per the docs .根据文档, Java 7 允许在数字文字中使用下划线(“_”)。 TreeMap can be used here, the map is sorted according to the natural ordering of its keys
这里可以使用TreeMap,map按照key的自然顺序排序
TreeMap<Integer, String> map = new TreeMap<>();
map.put(100_101,"lll");
map.put(100_102,"lll");
map.put(100_101_103,"lll");
map.put(100_101_104,"lll");
map.put(100,"lll");
for(Entry<Integer,String> entry : map.entrySet()) {
Integer key = entry.getKey();
String value = entry.getValue();
System.out.println(key + " => " + value);
}
This can do the job!这可以完成工作!
A Tree map is sorted according to the natural ordering of its keys.树图根据其键的自然顺序进行排序。 So you can use
TreeMap<String>
.所以你可以使用
TreeMap<String>
。 find this link找到这个链接
If you are using HashMap<String>
then you can do following如果您使用的是
HashMap<String>
那么您可以执行以下操作
Get all the keys in sorted order as follows按如下顺序获取所有键
Set<String> sortedKeys = new TreeSet<>(map.keySet());
or或者
List<String> sortedKeys=new ArrayList<>(map.keySet());
Collections.sort(sortedKeys);
and then loop through this Set or List and get the values from Map and print.然后遍历这个 Set 或 List 并从 Map 中获取值并打印。
Use SortedMap & TreeMap使用 SortedMap 和 TreeMap
your Code Should be like that:你的代码应该是这样的:
SortedMap<String, String> map = new TreeMap<>(new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
if(o1.length()==o2.length()&&o1.contains("_")&&o1.contains("_"))
{
String temp1,temp2;
temp1=o1.substring(o1.lastIndexOf("_"));
temp2=o2.substring(o2.lastIndexOf("_"));
return temp1.compareTo(temp2);
}
else if(o1.length()>o2.length())
{
return 1;
}
else if(o1.length()<o2.length())
{
return -1;
}
return o1.compareTo(o2);
}
});
map.put("100_101","lll");
map.put("100_102","lll");
map.put("100_101_103","lll");
map.put("100_101_104","lll");
map.put("100","lll");
System.out.println(map);
see Java tutorial page for SortedMap.请参阅SortedMap 的 Java 教程页面。
Here try this as I can see you want to sort based on last Integer
of the String
ie for 100_101
you want to sort based on 101
.在这里试试这个,因为我可以看到你想根据
String
最后一个Integer
进行排序,即对于100_101
你想基于101
进行排序。 You need to implement comparator
to satisfy this.您需要实现
comparator
来满足这一点。 Here's the code:这是代码:
public class Main {
public static void main(String a[]) {
TreeMap<String, String> map = new TreeMap<String, String>(new CustomComparator());
map.put("100_101", "lll");
map.put("100_103", "lll");
map.put("100_101_102", "lll");
map.put("100_101_104", "lll");
map.put("100", "lll");
System.out.println(map);
}
}
class CustomComparator implements Comparator<String> {
@Override
public int compare(String str1, String str2) {
int a, b;
//Used this because your key may or may not contain "_"
if (str1.contains("_")) {
a = Integer.parseInt(str1.substring(str1.lastIndexOf('_') + 1));
} else {
a = Integer.parseInt(str1);
}
if (str2.contains("_")) {
b = Integer.parseInt(str2.substring(str2.lastIndexOf('_') + 1));
} else {
b = Integer.parseInt(str2);
}
// System.out.println(Integer.parseInt(str1.substring(str1.lastIndexOf('_')+1))+"---"+Integer.parseInt(str2.substring(str2.lastIndexOf('_')+1)));
return String.valueOf(a).compareTo(String.valueOf(b));
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.