有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!


共 (2) 个答案

  1. # 1 楼答案

    如果要按插入顺序检索元素,请使用LinkedHashMap

    LinkedHashMap

    Hash table and linked list implementation of the Map interface, with predictable iteration order. This implementation differs from HashMap in that it maintains a doubly-linked list running through all of its entries. This linked list defines the iteration ordering, which is normally the order in which keys were inserted into the map (insertion-order). Note that insertion order is not affected if a key is re-inserted into the map. (A key k is reinserted into a map m if m.put(k, v) is invoked when m.containsKey(k) would return true immediately prior to the invocation.)

  2. # 2 楼答案

    如果您真正想知道的是映射中包含的已排序键集中c键的索引,那么您可以使用

    int index = map.headMap("c").size(); 
    

    请注意,它与插入顺序没有任何关系,树映射不会维护插入顺序