【问题标题】:Implementation of sorting in VectorVector中排序的实现
【发布时间】:2011-08-07 21:23:37
【问题描述】:

我有一个收藏

Vector<HashMap<String, String>>

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

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

现在我将它添加到 Vector。

我想通过 hashMap 的Name 键对向量的元素进行排序。 我知道Collection.sort,我可以使用 ArrayList 和 POJO 类对其进行排序。但我不知道如何与我的adapterListView 一起使用它

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

【问题讨论】:

  • 您需要更准确地描述您的问题。您是否要对向量或哈希图的内容进行排序?你都尝试了些什么?结果应该如何存储?
  • 我不明白,一个哈希映射可以有多个键。将一个哈希映射放在另一个之前的标准是什么?
  • 一个 HashMap 可以有很多键(这就是重点),它们甚至都可以有完全相同的键,所以您需要更具体地了解如何对 Vector 进行排序
  • Vector 是一个类似于 ArrayList 的遗留类,除了同步。使用排序不是线程安全的,我怀疑你不需要它,所以使用 ArrayList。如果你需要它是线程安全的,你应该在排序之前锁定 Vector。

标签: java android sorting collections adapter


【解决方案1】:

您需要实现一个Comparator&lt;HashMap&lt;String,String&gt; &gt; 并将您的排序逻辑放在它的compare 方法中。

【讨论】:

    【解决方案2】:

    不确定我是否理解正确。这将在地图的一个键上对向量进行排序。

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

    【讨论】:

    • 就像 aioobe 说的那样,您假设哈希图有一个键“yourKey”?哈希图的意义何在?
    • 我不是。我只是假设排序是在一个特定的键上完成的。
    【解决方案3】:

    你有没有想过看看 java.util 包中的集合?

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

    因此,要对您的项目进行分类,只需将您的 HashMap 替换为 TreeMap,所有工作都将完成。

    顺便说一句,这个向量在这里做什么?他们是 Java 1.1(换句话说,15 岁)

    【讨论】:

      【解决方案4】:

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

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

      【讨论】:

        【解决方案5】:

        使用 TreeMap 的最佳(最快)方式。如果您为其提供正确的 Comperator,则 TreeMap 中的所有项目都将被排序。

        重要的问题:为什么你有一个 HashMaps 的向量?

        【讨论】:

        • 我无法更改我的数据结构。它的要求。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-08-12
        • 1970-01-01
        • 2011-03-05
        • 2014-08-27
        • 1970-01-01
        • 2019-11-20
        相关资源
        最近更新 更多