【问题标题】:Java Get last key from HashmapJava从Hashmap中获取最后一个键
【发布时间】:2019-05-20 14:55:57
【问题描述】:

之前如果我的问题是经典的,我会道歉。但是因为我刚学安卓的Java编程,所以对命令不是很懂。

我目前正在制作一个代码部分如下的应用程序:

Map<Integer, CompTypes> mapCompTypes = new java.util.HashMap();

class CompTypes
{
    int androidId;
    AndroidViewComponent component;
    String text;
    String type;
    CompTypes(int androidId, AndroidViewComponent component, String type, String text)
    {
        this.androidId = androidId;
        this.component = component;
        this.type = type;
        this.text = text;
    }
}

public void SendMessage(HVArrangement container, String message, String dateTime, int fontSize, int fontColor, int alignment, int startid)
{
    int id = Integer.parseInt(ChatBox.this.GetLastId());
    TextBox comp = new TextBox(vertical);
    comp.getView().setId(id);
    comp.Text(message);
    comp.FontSize(fontSize);
    comp.TextColor(fontColor);
    comp.MultiLine(true);
    comp.Enabled(false);
    comp.RequestFocus();
    mapCompTypes.put(Integer.valueOf(id), new CompTypes(id, comp, compType, comp.Text()));

    int id2 = id + 1;
    Label dt = new Label(vertical);
    dt.getView().setId(id2);        
    ((TextView)dt.getView()).setText(android.text.Html.fromHtml(datetime));
    dt.HTMLFormat(true);
    dt.Text(datetime);
    dt.getView().setOnClickListener(this);
    dt.getView().setOnLongClickListener(this);
    mapCompTypes.put(Integer.valueOf(id2), new CompTypes(id2, dt, compType, dt.Text()));
}


public String GetLastId()
{
    // how to get last id?
    //return ids;
}

目前我无法从 id 获取值作为 Hashmap 的最后一个 id。 我在这里阅读了许多问题和答案,“地图没有最后一个条目,这不是他们合同的一部分。” 还有其他方法可以获取最后一个 id 吗?

【问题讨论】:

  • A HashMap 没有“last”键,无论是按插入顺序还是按排序顺序。 “最后一个”到底是什么意思?
  • 这将帮助您找出您的需求:stackoverflow.com/questions/3527216/…?
  • 既然要获取最后一个id,就用"LinkedHashMap" (docs.oracle.com/javase/6/docs/api/java/util/LinkedHashMap.html) 和HashMap一模一样,只不过当你遍历它的时候,它会呈现在插入顺序。在这种情况下,您可以获得最后一个 id。
  • 为什么要使用HashMap 而不是ArrayList

标签: java android hashmap


【解决方案1】:

HaspMap 不保留顺序。因此,不可能通过最后、第一个或中间的位置获取任何元素。

您可以做的一件事是在插入HashMap 时将key 保存在某个变量中,然后访问该变量以获取最后一个对象的键。保修单也可以找LinkedHashMap

【讨论】:

  • 请注意,LinkedHashMaps 有 2 种排序模式——FIFO 和 LRU。使用时请确保它在 FIFO 中。
【解决方案2】:

HashMap 不保持顺序。 如果要保持插入顺序,请使用 LinkedHashMap。可以这样使用。

 private static String getLastId() {
        Map<Integer, String> map = new LinkedHashMap<>();
        map.put(1, "A");
        map.put(2, "B");
        map.put(3, "C");
        String lastObject = "";
        for (Map.Entry<Integer, String> entry : map.entrySet()) {
            lastObject = entry.getValue();
        }
        return lastObject;
    }

如果您将地图键作为自然顺序。然后您可以使用 TreeMap 或将比较器传递给 TreeMap。这是一个可以做到这一点的 sn-p。

private static String getLastId() {
    Map<Integer, String> map = new TreeMap<Integer,String>();
    map.put(1, "A");
    map.put(2, "B");
    map.put(3, "C");
    return ((TreeMap<Integer, String>) map).lastEntry().getValue();
}

在此处查看LinkedHashMapTreeMap 的文档。

【讨论】:

    【解决方案3】:

    我可以看到您增加id 作为地图的键。因此,要获得您的last id,您有两种选择:

    • 使用LinkedHashMap,然后获取所有密钥并获取最后一个
    • 使用TreeMap逆序比较器,然后得到第一个键(这个键值最高,即最后一个)。

    Map<Integer, CompTypes> mapCompTypes = new TreeMap<>(Comparator.reverseOrder());
    
    final class CompTypes {
        private final int androidId;
        private final AndroidViewComponent component;
        private final String text;
        private final String type;
    
        CompTypes(AndroidViewComponent component, String type)  {
            this.androidId = component.getView().getId();
            this.component = component;
            this.type = type;
            this.text = component.Text();
        }
    }
    
    public void SendMessage(HVArrangement container, String message, String datetime, int fontSize, int fontColor, int alignment, int startid) {
        int id = Integer.parseInt(ChatBox.this.GetLastId());
        mapCompTypes.put(id, new CompTypes(createCompTypes(createTextBox(id, message, fontSize, fontColor)), compType));
        mapCompTypes.put(id + 1, new CompTypes(createLabel(id, datetime)));
    }
    
    private CompTypes createCompTypes(AndroidViewComponent component) {
        return new CompTypes(component, compType)
    }
    
    private TextBox createTextBox(int id, String message, int fontSize, int fontColor) {
        TextBox textBox = new TextBox(vertical);
        textBox.getView().setId(id);
        textBox.Text(message);
        textBox.FontSize(fontSize);
        textBox.TextColor(fontColor);
        textBox.MultiLine(true);
        textBox.Enabled(false);
        textBox.RequestFocus();
        return textBox;
    }
    
    private Label createLabel(int id, String datetime) {
        Label label = new Label(vertical);
        label.getView().setId(id);        
        ((TextView)dt.getView()).setText(android.text.Html.fromHtml(datetime));
        label.HTMLFormat(true);
        label.Text(datetime);
        label.getView().setOnClickListener(this);
        label.getView().setOnLongClickListener(this);
        return label;
    }
    
    public String GetLastId() {
        int lastId = mapCompTypes.isEmpty() ? -1 : mapCompTypes.keySet().iterator().next();
        //return ids;
    }
    

    【讨论】:

      【解决方案4】:

      我建议你使用 TreeMap 代替你可以使用 lastEntry() 方法的地方。

      Map<Integer, CompTypes> mapCompTypes = new TreeMap();
      mapCompTypes.lastEntry()
      

      【讨论】:

      • 不保证HM,只保证LHM
      猜你喜欢
      • 2010-11-25
      • 2016-03-08
      • 2023-04-09
      • 2014-09-21
      • 2014-12-01
      • 1970-01-01
      • 2016-03-09
      • 1970-01-01
      相关资源
      最近更新 更多