Java 访问映射中的最后一个条目

Java 访问映射中的最后一个条目,java,maps,hashmap,Java,Maps,Hashmap,如何将特定HashMap条目移动到最后一个位置 例如,我有如下HashMap值: HashMap<String,Integer> map = new HashMap<String,Integer>(); map= {Not-Specified 1, test 2, testtest 3}; HashMap map=newhashmap(); map={未指定1,测试2,测试3}; “未指定”可以出现在任何位置。它可能出现在地图的第一或中间。但我想将“未指定”移动到最

如何将特定HashMap条目移动到最后一个位置

例如,我有如下HashMap值:

HashMap<String,Integer> map = new HashMap<String,Integer>();

map= {Not-Specified 1, test 2, testtest 3};
HashMap map=newhashmap();
map={未指定1,测试2,测试3};
“未指定”可以出现在任何位置。它可能出现在地图的第一或中间。但我想将“未指定”移动到最后一个位置

我该怎么做?提前感谢。

HashMap没有“最后位置”,因为它没有排序


您可以使用其他实现
java.util.SortedMap
Map
,最常用的是
TreeMap

用一句话回答您的问题:

默认情况下,地图没有最后一个条目,它不是合同的一部分。


还有一个旁注:根据接口而不是实现类编写代码是一种很好的做法(请参见第8章第52项:通过接口引用对象)

所以你的声明应该是:

Map<String,Integer> map = new HashMap<String,Integer>();
输出:

最后一个键:未指定,最后一个值:1

使用HashMap的解决方案: 如果您必须依赖HashMaps,仍然有一个解决方案,使用a)上述比较器的修改版本,b)a使用Map的初始化,c)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());

}
final Map=new HashMap();
map.put(“test”,Integer.valueOf(2));
map.put(“未指定”,Integer.valueOf(1));
map.put(“testtest”,Integer.valueOf(3));
最后名单条目=
新的ArrayList(map.entrySet());
Collections.sort(条目,新的Comparator(){
公共整数比较键(最终字符串o1,最终字符串o2){
int结果;
如果(“未指定”。等于(o1)){
结果=1;
}否则,如果(“未指定”。等于(o2)){
结果=-1;
}否则{
结果=o1。与(o2)相比;
}
返回结果;
}
@凌驾
公共整数比较(最终输入o1,
最终条目(o2){
返回这个.compareKeys(o1.getKey(),o2.getKey());
}
});
最后一项=
entries.get(entries.size()-1);
System.out.println(“最后一个键:+lastEntry.getKey()+”,最后一个值:”
+getValue());
}
输出:

最后一个键:未指定,最后一个值:1


SortedMap
是逻辑/最佳选择,但是另一种选择是使用
LinkedHashMap
,它维护两种订单模式,最近添加的最后一种模式和最近访问的最后一种模式。有关更多详细信息,请参阅Javadocs。

move对于hashmap没有意义,因为它是一个字典,其中包含一个基于键的bucketing哈希代码,然后是一个通过equals解析的冲突哈希代码的链表。
使用树映射进行排序,然后传入自定义比较器

当使用数字作为键时,我想您也可以尝试以下方法:

        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
Map Map=newhashmap();
地图放置(4L,“第一”);
地图放置(6L,“第二”);
地图放置(11L,“最后”);
长lastKey=0;
//您输入了地图条目
对于(Map.Entry:Map.entrySet()){
lastKey=entry.getKey();
}
System.out.println(lastKey);//11

在这种情况下,最后使用的键通常是已知的,所以它可以用于访问最后一个值(插入一个值):

class PostIndexData{
字符串\u办公室\u名称;
布尔函数;
公共PostIndexData(字符串名称,布尔值){
_办公室名称=名称;
_isGov=gov;
}
}
//-----------------------
类KgpData{
字符串_postIndex;
PostIndexData_PostIndexData;
公共KgpData(字符串postIndex、PostIndexData PostIndexData){
_postIndex=postIndex;
_postIndexData=postIndexData;;
}
}
公共班级办公室{
私有HashMap_postIndexMap=新HashMap();
私有HashMap_kgpMap=新HashMap();
...
私有void addOffice(字符串kgp、字符串postIndex、字符串officeName、布尔gov){
if(_postinexmap.get(postIndex)==null){
_postinexmap.put(postIndex,新PostIndexData(officeName,gov));
}
_kgpMap.put(kgp,新KgpData(postIndex,_postIndexMap.get(postIndex));
}
查找数组中缺少的所有元素
int[]数组={3,5,7,8,2,1,32,5,7,9,30,5};
TreeMap map=newtreemap();

对于(int i=0;我能解释一下,为什么要这样做吗?也许使用HashMap不是解决问题的最佳方法。另请参阅,您不能“传入自定义比较器”创建后使用树映射。您只能将其用作构造函数参数。为什么实现可比较的接口然后使用树映射不是您的首选?您似乎比我更了解映射,所以wondering@nixxbb这是我的第一个选择,这就是为什么它直接出现在字面回答之后哦,我明白了,你在说话ng关于Comparable vs Comparator。在OP的例子中,键是字符串,但他需要自定义顺序。这就是为什么。否则我会同意你的观点哦,我明白了。做得很好。谢谢你的回答,我从你的回答中学到了很多…另请看Hi Mad Calum,记住尽量保持你的示例简洁,以便任何人都能阅读问题n/答案很容易理解。粘贴代码而不做太多解释通常不是一个好主意:-)感谢您的贡献。查找缺少阵列中的所有元素这看起来与所问的问题没有多大关系。
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());
    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());

}
        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
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)) );
        }
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);
        }