say*_*aha 4 java string collections treemap
我有一个很大的列表并将其放入树形图中.
然后我想把"ALL"放在列表的最顶端,但是在"ALL"之前有"AAA"的东西
编辑:我想要对所有其他输入进行排序
List -> List
----- -----
AAA AAA
BBB ALL
CCC BBB
CCC
Run Code Online (Sandbox Code Playgroud)
我可以使用arrayList或其他东西,但我想知道是否有办法控制这种情况.
一种选择是构建一个自定义比较器,始终在其他所有内容之前对单词"ALL"进行排序:
TreeMap<String, T> myMap = new TreeMap<String, T>(new Comparator<String>() {
public int compare(String lhs, String rhs) {
/* See which of the inputs, if any, are ALL. */
bool oneAll = lhs.equals("ALL");
bool twoAll = rhs.equals("ALL");
/* If both are ALL or neither are ALL, just do a normal comparison. */
if (oneAll == twoAll) {
return lhs.compareTo(rhs);
}
/* Otherwise, exactly one of them is ALL. Determine which one it is and
* react accordingly.
*/
else if (oneAll) {
return -1;
} else {
return +1;
}
}
});
Run Code Online (Sandbox Code Playgroud)
这将按升序排序所有内容,但优先"ALL"于其他所有内容.
希望这可以帮助!
| 归档时间: |
|
| 查看次数: |
977 次 |
| 最近记录: |