如何移动一个特定的哈希Map条目到最后的位置?例如,我有这样的HashMap值:
HashMap<String,Integer> map = new HashMap<String,Integer>(); map= {Not-Specified 1, test 2, testtest 3};
"未指定"可以出现在任何位置。它可以出现在Map的第一个或中间。但我想将"未指定"移动到最后一个位置。我该怎么做呢?
mhd8tkvw1#
用一句话回答你的问题:
还有一点:最好是针对接口而不是实现类进行编码(参见Effective Java by Joshua Bloch,第8章,第52项:* * 通过接口引用对象**)。因此,您的声明应为:
Map<String,Integer> map = new HashMap<String,Integer>();
(AllMap共享公共契约,因此客户机不需要知道它是什么类型的Map,除非他指定具有扩展契约的子接口)。
有一个子接口SortedMap,它用基于顺序的查找方法扩展了map接口,还有一个子接口NavigableMap,它进一步扩展了map接口,这个接口的标准实现TreeMap允许你按照自然顺序(如果它们实现了Comparable接口)或者按照提供的Comparator对条目进行排序。您可以通过lastEntry方法访问最后一个条目:
NavigableMap<String,Integer> map = new TreeMap<String, Integer>(); // add some entries Entry<String, Integer> lastEntry = map.lastEntry();
LinkedHashMap也是一个特例,它是一个HashMap实现,存储键插入的顺序。但是没有接口来支持这个功能,也没有直接的方法来访问最后一个键。你只能通过一些技巧来实现,比如在两个键之间使用一个List:
Map<String,String> map = new LinkedHashMap<String, Integer>(); // add some entries List<Entry<String,Integer>> entryList = new ArrayList<Map.Entry<String, Integer>>(map.entrySet()); Entry<String, Integer> lastEntry = entryList.get(entryList.size()-1);
由于您不控制插入顺序,因此应该使用NavigableMap接口,即编写一个比较器,将Not-Specified条目放在最后。下面是一个例子:
Not-Specified
final NavigableMap<String,Integer> map = new TreeMap<String, Integer>(new Comparator<String>() { public int compare(final String o1, final String o2) { int result; if("Not-Specified".equals(o1)) { result=1; } else if("Not-Specified".equals(o2)) { result=-1; } else { result =o1.compareTo(o2); } return result; } }); map.put("test", Integer.valueOf(2)); map.put("Not-Specified", Integer.valueOf(1)); map.put("testtest", Integer.valueOf(3)); final Entry<String, Integer> lastEntry = map.lastEntry(); System.out.println("Last key: "+lastEntry.getKey() + ", last value: "+lastEntry.getValue());
输出:最后一个键:未指定,最后一个值:1
如果你必须依赖于HashMaps,仍然有一个解决方案,使用a)上述比较器的修改版本,b)用Map的entrySet初始化的List,以及c)Collections.sort()helper方法:
final Map<String, Integer> map = new HashMap<String, Integer>(); map.put("test", Integer.valueOf(2)); map.put("Not-Specified", Integer.valueOf(1)); map.put("testtest", Integer.valueOf(3)); final List<Entry<String, Integer>> entries = new ArrayList<Entry<String, Integer>>(map.entrySet()); Collections.sort(entries, new Comparator<Entry<String, Integer>>(){ public int compareKeys(final String o1, final String o2){ int result; if("Not-Specified".equals(o1)){ result = 1; } else if("Not-Specified".equals(o2)){ result = -1; } else{ result = o1.compareTo(o2); } return result; } @Override public int compare(final Entry<String, Integer> o1, final Entry<String, Integer> o2){ return this.compareKeys(o1.getKey(), o2.getKey()); } }); final Entry<String, Integer> lastEntry = entries.get(entries.size() - 1); System.out.println("Last key: " + lastEntry.getKey() + ", last value: " + lastEntry.getValue()); }
tjjdgumg2#
HashMap没有 “最后位置”,因为它没有排序。您可以使用其他实现java.util.SortedMap的Map,最流行的是TreeMap。
java.util.SortedMap
Map
TreeMap
zsbz8rwp3#
SortedMap是逻辑上的最佳选择,但是另一个选择是使用LinkedHashMap,它维护两种排序模式,最近添加的放在最后,最近访问的放在最后。
SortedMap
LinkedHashMap
nwwlzxa74#
当使用数字作为密钥时,我想您还可以尝试以下方法:
Map<Long, String> map = new HashMap<>(); map.put(4L, "The First"); map.put(6L, "The Second"); map.put(11L, "The Last"); long lastKey = 0; //you entered Map<Long, String> entry for (Map.Entry<Long, String> entry : map.entrySet()) { lastKey = entry.getKey(); } System.out.println(lastKey); // 11
e0bqpujr5#
move对于散列表没有意义,因为它是一个字典,其中包含用于基于键进行分组的散列码,然后是一个链表,其中包含通过equals解析的冲突散列码。请对排序的Map使用TreeMap,然后传入自定义比较器。
lnxxn5zx6#
在这种情况下,最后使用的键通常是已知的,因此它可以用于访问最后一个值(与1一起插入):
class PostIndexData { String _office_name; Boolean _isGov; public PostIndexData(String name, Boolean gov) { _office_name = name; _isGov = gov; } } //----------------------- class KgpData { String _postIndex; PostIndexData _postIndexData; public KgpData(String postIndex, PostIndexData postIndexData) { _postIndex = postIndex; _postIndexData = postIndexData;; } } public class Office2ASMPro { private HashMap<String,PostIndexData> _postIndexMap = new HashMap<>(); private HashMap<String,KgpData> _kgpMap = new HashMap<>(); ... private void addOffice(String kgp, String postIndex, String officeName, Boolean gov) { if (_postIndexMap.get(postIndex) == null) { _postIndexMap.put(postIndex, new PostIndexData(officeName, gov)); } _kgpMap.put( kgp, new KgpData(postIndex, _postIndexMap.get(postIndex)) ); }
qeeaahzv7#
Find missing all elements from array int[] array = {3,5,7,8,2,1,32,5,7,9,30,5}; TreeMap<Integer, Integer> map = new TreeMap<>(); for(int i=0;i<array.length;i++) { map.put(array[i], 1); } int maxSize = map.lastKey(); for(int j=0;j<maxSize;j++) { if(null == map.get(j)) System.out.println("Missing `enter code here`No:"+j); }
7条答案
按热度按时间mhd8tkvw1#
用一句话回答你的问题:
还有一点:最好是针对接口而不是实现类进行编码(参见Effective Java by Joshua Bloch,第8章,第52项:* * 通过接口引用对象**)。
因此,您的声明应为:
(AllMap共享公共契约,因此客户机不需要知道它是什么类型的Map,除非他指定具有扩展契约的子接口)。
可能的解决方案
分类Map:
有一个子接口SortedMap,它用基于顺序的查找方法扩展了map接口,还有一个子接口NavigableMap,它进一步扩展了map接口,这个接口的标准实现TreeMap允许你按照自然顺序(如果它们实现了Comparable接口)或者按照提供的Comparator对条目进行排序。
您可以通过lastEntry方法访问最后一个条目:
链接Map:
LinkedHashMap也是一个特例,它是一个HashMap实现,存储键插入的顺序。但是没有接口来支持这个功能,也没有直接的方法来访问最后一个键。你只能通过一些技巧来实现,比如在两个键之间使用一个List:
正确的解决方案:
由于您不控制插入顺序,因此应该使用NavigableMap接口,即编写一个比较器,将
Not-Specified
条目放在最后。下面是一个例子:
输出:
最后一个键:未指定,最后一个值:1
使用HashMap的解决方案:
如果你必须依赖于HashMaps,仍然有一个解决方案,使用a)上述比较器的修改版本,b)用Map的entrySet初始化的List,以及c)Collections.sort()helper方法:
输出:
最后一个键:未指定,最后一个值:1
tjjdgumg2#
HashMap没有 “最后位置”,因为它没有排序。
您可以使用其他实现
java.util.SortedMap
的Map
,最流行的是TreeMap
。zsbz8rwp3#
SortedMap
是逻辑上的最佳选择,但是另一个选择是使用LinkedHashMap
,它维护两种排序模式,最近添加的放在最后,最近访问的放在最后。nwwlzxa74#
当使用数字作为密钥时,我想您还可以尝试以下方法:
e0bqpujr5#
move对于散列表没有意义,因为它是一个字典,其中包含用于基于键进行分组的散列码,然后是一个链表,其中包含通过equals解析的冲突散列码。请对排序的Map使用TreeMap,然后传入自定义比较器。
lnxxn5zx6#
在这种情况下,最后使用的键通常是已知的,因此它可以用于访问最后一个值(与1一起插入):
qeeaahzv7#