如何对hashmap进行排序将arraylist包含为按键的值?还要对arraylist进行排序?
Hashmap<String, ArrayList<Events>> h = new HashMap<>();
ArrayList<Events> a = new ArrayList<>();
a.add(new Events("1", "name1", "Address1"));
a.add(new Events("2", "name2", "Address2"));
h.put("10-12-2014", a);
h.put("08-11-2014", a1);
... ...
现在我想基于密钥进行排序。 并且还需要根据ID对每个键的ArrayList进行排序。
答案 0 :(得分:2)
使用TreeMap
代替HashMap
,ArrayList
会根据密钥的自然顺序自动排序。
要对Collections#sort(List, Comparator)
进行排序,请使用静态Events
方法,指定比较器以对列表中的Events
个对象进行排序。例如,假设您要按名为id
的{{1}}构造函数的第一个参数进行排序,那么您可以按如下方式调用sort
:
Map<String, List<Events>> h = new HashMap<>();
List<Events> a = new ArrayList<>();
a.add(new Events("1", "name1", "Address1"));
a.add(new Events("2", "name2", "Address2"));
Collections.sort(a, new Comparator<Events>() {
@Override
public int compare(Events o1, Events o2) {
return o1.getId().compareTo(o2.getId());
}
});
h.put("10-12-2014", a);
... // similarly for list a1
Collections.sort(a1, new Comparator<Events>() {
@Override
public int compare(Events o1, Events o2) {
return o1.getId().compareTo(o2.getId());
}
});
h.put("08-11-2014", a1);
请注意,最好将接口而不是实现声明列表和映射变量,即h
声明为Map
而不是HashMap
,{{1类型为a
。
或者,考虑将List
元素放在TreeSet
而不是Events
中,让ArrayList
类实现Events
接口:
Comparable
答案 1 :(得分:1)
要对ArrayList进行排序,让Events实现Comparable(只需比较compareTo函数中的两个ID),然后按Collections.sort(a)排序;
我不确定我是否理解正确的HashMap问题,但正如manouti所说,我会使用TreeMap(HashMap通常根据定义未排序)
答案 2 :(得分:0)
aas manouti建议,使用TreeMap
轻松排序keys
对于值,只需循环遍历它们
for(Entry<String,List> entry : h.entrySet()){
ArrayList<Event> a = entry.getValue();
Collections.sort(entry.getValue());
h.put(entry.getKey(), a); //replaces the old value with its sorted value
}
答案 3 :(得分:0)
一些简单的例子:
TreeMap<String,ArrayList<String>> list = new TreeMap<>();
ArrayList<String> l1 = new ArrayList<>();
l1.add("hallo");
l1.add("hello");
l1.add("yoow");
ArrayList<String> l2 = new ArrayList<>();
l2.add("byee");
l2.add("yooo");
l2.add("daag");
list.put("h", l1);
list.put("e", l2);
for (String s : list.keySet()){
Collections.sort(list.get(s));
System.out.println(list.get(s));
}
这个例子是使用字符串但是如果你在对象中实现Comparable接口,它应该像这个例子一样工作:-)
答案 4 :(得分:0)
在Java Collections中使用自定义排序。尝试为您的POJO实现Comparator或Comparable接口,并调用Collection的本机排序方法。对于您的查询,我编写以下代码用于排序自定义arraylist以获取更多详细信息,请使用解决方案中的链接: -
public class student {
private String name = "";
private int age = 0;
public void setAge(int age) {
this.age = age;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public String getName() {
return name;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final student other = (student) obj;
if ((this.name == null) ? (other.name != null) : !this.name.equals(other.name)) {
return false;
}
if (this.age != other.age) {
return false;
}
return true;
}
@Override
public int hashCode() {
int hash = 7;
hash = 59 * hash + (this.name != null ? this.name.hashCode() : 0);
hash = 59 * hash + this.age;
return hash;
}
}
player class :-
package pojocomparator.mypack;
import java.io.Serializable;
import java.util.Comparator;
public class player implements Comparator<player>{
private int age = 0;
private String name = "";
private student stud = null;
public player()
{
}
public void setAge(int age) {
this.age = age;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public String getName() {
return name;
}
public void setStud(student stud) {
this.stud = stud;
}
public student getStud() {
return stud;
}
/*
* compare method of Comparator<E> interface used for comparing object of classes.
* particularly called by "sort" method of Collections interface
* @return int
* @param Class objects for comparison
*/
@Override
public int compare(player o1, player o2) {
return o1.age<o2.age ? -1 :
o1.age == o2.age ? 0 : 1;
/*
* If first object is smaller than second then -1 is returned
* If first object is equal to second then 0 is returned
* else 1 is returned.
*/
}
}
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class CallAll {
public static void main(String... q)
{
//Generic List of Player class type
List<player> playerList = new ArrayList<player>();
player p1 = new player();
p1.setAge(21);
p1.setName("vaibhav");
player p2 = new player();
p2.setAge(18);
p2.setName("anvesh");
player p3 = new player();
p3.setAge(28);
p3.setName("rohit");
playerList.add(p1);
playerList.add(p2);
playerList.add(p3);
/* Below statement is responsible for sorting playerList. This statement
* internally calls compare method of Comaparator interface
*/
Collections.sort(playerList, new player());
for(int i=0; i<playerList.size(); i++)
{
System.out.println(playerList.get(i).getAge());
}
// inserting student type object into player type object
student s1 = new student();
s1.setName("mickey mouse");
s1.setAge(15);
p3.setStud(s1);
// retrieval of student type data from player type data
System.out.println("Student name : "+p3.getStud().getName());
System.out.println("Student age : "+p3.getStud().getAge());
}
}
给出输出: -
运行:
18
21
28
学生姓名:米老鼠
学生年龄:15岁
建立成功(总时间:6秒)