【发布时间】:2014-08-29 08:35:55
【问题描述】:
我有以下HashMap:
ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();
其中使用以下键:
static final String KEY_ITEM = "item"; // parent node
static final String KEY_ID = "id";
static final String KEY_NAME = "name";
static final String KEY_TITLE = "title";
static final String KEY_COST = "cost";
static final String KEY_DESC = "description";
static final String KEY_LINK = "link";
static final String KEY_PUBDATE = "pubDate";
static final String KEY_TIME = "pubDate";
然后我使用这段代码来填充哈希图。
HashMap<String, String> map = new HashMap<String, String>();
map.put(KEY_ID, parser.getValue(e, KEY_ID));
//Get the title of the article.
map.put(KEY_NAME, parser.getValue(e, KEY_TITLE));
//Get the description of the article.
map.put(KEY_DESC, parser.getValue(e, KEY_DESC));
//Add the keys to the hashmap.
menuItems.add(map);
等等……
我有一个名为 KEY_TIME 的密钥,其中包含例如 16:30 格式的每篇文章的发布时间。
我想知道如何通过键 KEY_TIME 对这个 hashmap 进行排序,以便顶部的文章是最新的,底部的文章是最旧的。
我知道以前有人问过这个问题,但我找不到在 Hashmap 之前解释我的 ArrayList 的解决方案。
【问题讨论】:
-
请在提问前做一些研究......这里被问了很多次,我很确定谷歌会给你一些答案
-
您为什么不使用TreeMap?它已经排序了。
-
我很确定他想得到这个
ArrayList<HashMap<String, String>> menuItems(通过它的价值之一——比如hashmapItem [“Column”])而不是HashMaps本身......我很确定TreeMap 在同一 TreeMap 实例中的键排序,而不是 ArrayList 中的多个 TreeMap -
我曾尝试使用 TreeMap,但在将哈希图转换为树图时发现了一些问题。不过,我会再看看,谢谢。
-
如果您真的需要为此坚持使用 ArrayList(或一般的 List),一种选择是创建一个数据持有者类并使其通过 KEY_TIME 进行比较(而不是使用 Map)。跨度>