简体   繁体   English

在Arraylist里面的HashMap

[英]HashMap inside an Arraylist

ArrayList<HashMap<String, String>> 
arrayListRowsFirst = new ArrayList<HashMap<String, String>>();

Today when i was going through a code, this piece of code struck me for a while. 今天当我通过代码时,这段代码让我感到震惊了一段时间。 Here are some of my questions over this declaration. 以下是我对此声明的一些问题。

  1. What could be the requirement if one has to append an HashMap into an ArrayList. 如果必须将HashMap附加到ArrayList中,可能需要什么。
  2. What will happen during sorting of arraylist, how it will go, will it take long time. 在分类arraylist期间会发生什么,它将如何发展,这需要很长时间。

First off, "generic chaining" in my opinion is a poor practice. 首先,在我看来,“通用链接”是一种糟糕的做法。 I would encourage wrapping the HashMap in a class that encapsulates the data inside, allowing the logic for manipulation to be inside the class, not just strewn about everywhere. 我鼓励将HashMap包装在一个封装内部数据的类中,允许操作逻辑在类中,而不仅仅是遍布各处。

To answer #1, I could think of a number of scenarios. 要回答#1,我可以想到一些场景。 You might have languages for instance, mapping certain constants to other translations. 例如,您可能有语言,将某些常量映射到其他翻译。 The fact that it says rows first in the identifier makes me think perhaps it's some kind of matrix of data, and that the first String parameter will exist in all the entries of the list (a poor practice indeed.) Edit: I misunderstood your question, it appears. 它在标识符中首先表示行的事实让我想到它可能是某种数据矩阵,并且第一个String参数将存在于列表的所有条目中(确实很糟糕。)编辑:我误解了你的问题, 它出现。 You would add it like any other entry. 您可以像任何其他条目一样添加它。 See the others' answers for example code. 有关示例代码,请参阅其他人的答案。 :-) :-)

To answer #2, you won't be able to sort the ArrayList unless you are able to provide a comparator, at which point it's up to you how it's sorted (could be size, could be the value of a particular key, could be Math.random(), it's up to whoever writes the comparator). 要回答#2,你将无法对ArrayList进行排序,除非你能够提供一个比较器,此时它取决于你如何排序(可能是大小,可能是特定键的值,可能是Math.random(),这取决于谁写了比较器)。

There is no "special requirement" to append an HashMap to an ArrayList . 将HashMap附加到ArrayList没有“特殊要求”。

And as neither Map nor HashMap implements Comparable , so if you want to sort the ArrayList, you would have to create your own Comparator . 由于MapHashMap都没有实现Comparable ,因此如果要对ArrayList进行排序,则必须创建自己的Comparator

A sort on a List which contains Map would be exactly the same as a sort on a List wich contains anything else. 在A排序List包含Map上一个排序将是完全一样的List至极含有别的。

What do you mean about "append a HashMap into an ArrayList"? 你是什​​么意思“将HashMap附加到ArrayList中”? You add HashMaps to the ArrayList the way you add any object to a List 您可以将任何对象添加到List的方式将HashMaps添加到ArrayList

HashMap<String,String> hm = new HashMap<String,String>();
arrayListRowsFirst.add(hm);

You sort the array list like you sort any other - you would need to define a Comparator that compared two HashMaps, and use Collections.sort with that Comparator. 您可以按照其他任何方式对数组列表进行排序 - 您需要定义比较两个HashMaps的Comparator,并将Collections.sort与该Comparator一起使用。 How long it takes will depend a lot on how you're comparing the HashMaps. 需要多长时间将取决于您如何比较HashMaps。

  1. You would add HashMaps to the ArrayList like you would any other object, using the add() method. 您可以使用add()方法将HashMaps添加到ArrayList中,就像使用任何其他对象一样。 Obviously it would need to be of the correct Type, in this case a HashMap of Strings. 显然它需要是正确的Type,在这种情况下是字符串的HashMap。

  2. You would need to create a comparator so that your HashMaps would be sortable. 您需要创建一个比较器,以便您的HashMaps可以排序。

The declaration should be 声明应该是

List<Map<String, String>>

1 to append a map into the list, you just do 1,将地图附加到列表中,您就是这样做的

Map<String, String> map = new HashMap<String, String>();
list.add(map);

2 To sort the list, you would need a way to tell if one Map is "greater than", "less than", or "equal" to another Map. 2要对列表进行排序,您需要一种方法来判断一个Map是否“大于”,“小于”或“等于”另一个Map。 The could or might not take a long time depending on your needs. 根据您的需要,可能需要或可能不需要很长时间。 It doesn't have to take a long time. 它不需要很长时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM