2011-04-18 37 views
0

排序实现我有一个集合作为在矢量

Vector<HashMap<String, String>> 

其实我用这作为android.I列表视图的列表项使用SortedMap,但我没有得到正确的结果。我认为这是因为HashMap的结构是

hashMap.add("Name","value_of_name"); 
hashMap.add("Counts","value_of_counts"); 

现在我将它添加到Vector。

我想按矢量的元素排序Name hashMap的键。 我知道Collection.sort,我可以使用ArrayList和POJO类对此进行排序。但我不知道如何与我的adapter一起使用ListView

如何对元素进行排序。有没有更好的解决方案(关于我的数据结构的收集,可以很容易地使用适配器)?

+0

你需要更加精确地描述你的问题。你想排序矢量还是hashmaps的内容?你试过什么了?结果应该如何存储? – Mat 2011-04-18 13:23:01

+4

我不明白,一个哈希映射可以有几个键。一个哈希映射放在另一个之前的标准是什么? – aioobe 2011-04-18 13:23:41

+0

一个HashMap可以有许多键(这就是整个点),它们甚至可以拥有完全相同的键,所以你需要更加具体地了解如何对Vector进行排序。 – 2011-04-18 13:24:47

回答

0

如果要对数组中的地图进行排序,请使用SortedMap实现,如TreeMapConcurrentSkipListMap。这需要一个HashMaps矢量,并返回SortedMaps的一个ArrayList(一个非矢量同步和更快的集合)。

public ArrayList<SortedMap<String, String>> sortMaps(Vector<HashMap<String, String> maps) { 
    ArrayList<TreeMap<String, String>> returnMaps = new ArrayList<TreeMap<String, String>>(); 
    for(HashMap<String, String> theMap : maps) { 
     // TreeMap is a sorted map and this will use the default String.compareTo 
     TreeMap<String, String> newMap = new TreeMap<String, String>(); 
     // put all the items from the HashMap into the TreeMap, which will autosort 
     newMap.putAll(theMap); 
     returnMaps.add(newMap); 
    } 
    return returnMaps; 
} 

要按第一项散列图的矢量(最低键,首字母顺序排列)尝试返回行之前执行以下操作:

// this sorts the vector by first keys 
    Collections.sort(returnMaps, new Comparator<SortedMap<String,String>>() { 
     public int compare(SortedMap<String,String> a, HashMap<String,String> b) { 
      return a.firstKey().compareTo(b.firstKey()); 
     } 
    }); 

或者,如果你想通过最后的关键排序(最高键,最后按字母顺序排列):

// this sorts the vector by first keys 
    Collections.sort(returnMaps, new Comparator<SortedMap<String,String>>() { 
     public int compare(SortedMap<String,String> a, HashMap<String,String> b) { 
      return a.lastKey().compareTo(b.lastKey()); 
     } 
    }); 

要返回所有键的一个有序映射(将踩在脚下的任何一式两份):

public SortedMap<String, String> singledSortedMap(Vector<HashMap<String, String> maps) { 
    // this will end up with all the values, sorted by natural string ordering 
    SortedMap<String, String> returnMap = new TreeMap<String, String>(); 
    for(HashMap<String, String> theMap : maps) { 
     returnMap.putAll(theMap); 
    } 
    return returnMap; 
} 
1

不知道我理解正确。这将在地图的一个键上对矢量进行排序。

Collections.sort(yourVector, new Comparator<HashMap<String,String>>() { 
    public int compare(HashMap<String,String> a, HashMap<String,String> b) { 
     return a.get(yourKey).compareTo(b.get(yourKey)); 
    } 
}); 
+0

就像aioobe说的那样,你假设一个关键字“yourKey”为hashmap? hashmap有什么意义? – Jmoney38 2011-04-18 13:26:07

+0

我不是。我只假定排序是在一个特定的键上完成的。 – 2011-04-18 13:29:07

2

你需要一个实现Comparator<HashMap<String,String> >,把你的排序顺序的逻辑及其compare方法内。

+0

它将如何? – 2011-04-19 05:01:14

1

您从来没有想过要看看java.util包中的集合吗?

然后你会发现Treemap已经实现了Comparable项的平衡树排序,就像String is一样。

因此,要将您的物品分类,只需用TreeMap即可取消您的HashMap,所有工作都将完成。

BTW这个矢量在这里做什么?换句话说,它们是Java 1.1(十五岁)

0

它使用TreeMap代替它的最好(最快)方式。如果您提供了正确的Comperator,则TreeMap中的所有项目将被排序。

重要的问题:为什么你有一个HashMap矢量?

+0

我无法更改我的数据结构。它的要求。 – 2011-04-18 14:05:57