![](/img/trans.png)
[英]How to sort Arraylist of LinkedHashMap Arraylist<LinkedHashMap<String,String>?
[英]How to sort ArrayList<LinkedHashMap<String,String>> by key?
我正在构建一个食谱搜索应用程序,该程序从网站获取食谱数据并将其显示在该应用程序的结果页面上(它使用JSON)。 我查看了网上的教程,发现ArrayList中的HashMap对于此类功能很常见,我改用LinkedHashMap,因为我对插入顺序感兴趣。
信息的结果在配方页面上很好地显示。 由于我的食谱页面可以包含20多种食谱(取决于所使用的搜索词),因此我想为用户提供一个选项,只需单击一个按钮即可使用结果列表。
这是我如何填充数组列表的一部分。
static ArrayList<LinkedHashMap<String,String>> pairs = new ArrayList<LinkedHashMap<String,String>>();
LinkedHashMap<String, String> map;
for (i=0; i<matches.length(); i++){
map = new LinkedHashMap<String, String>();
map.put("id", id);
map.put("name", name);
map.put("rating", rating);
map.put("source", source);
pairs.add(map);
}
以下是我的arraylist的示例(已打印)
{id=1518539, name=Slow Cooker Mac and Cheese, rating=4, source=Merlot Mommy}
{id=1522143, name=Crock Pot Chicken Stroganoff, rating=4, source=Diethood}
{id=1506448, name=Slow Cooker Lemon-Garlic Chicken, rating=4, source=Magic Skillet}
{id=1520809, name=Chicken Tortilla Soup, rating=4, source=How does your garden grow}
这是我的操作栏onOptionsItemSelected
方法
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.a_z:
//sort recipe name alphabetically
case R.id.z_a:
//sort recipe name reverse alphabetically
case R.id.by_rating:
//sort rating by highest values
default:
return super.onOptionsItemSelected(item);
}
}
因此,我将如何对该列表进行排序,以字母顺序为例。
正如@LouisWasserman所说,您应该拥有一个类而不是一个地图。 但是答案是,您可以将Collections.sort与自定义Comparator一起使用。 不同的比较器类别-每个模式1个。 例如,两个名字可以只链接到一个字符串比较器,比较它们的名字。
可以为您的项目解决此问题的更好解决方案是。
只需将一个班级名称命名为Student
。 仅供您参考。
import java.util.Comparator;
import java.util.Date;
public class Student implements Comparable<Student>{
private int id;
private String name;
public Student(int id, String name) {
this.id = id;
this.name = name;
}
public static final Comparator<Student> NameComparator = new Comparator<Student>(){
@Override
public int compare(Student o1, Student o2) {
return o1.name.compareTo(o2.name);
}
};
@Override
public String toString() {
return "Student{" + "id=" + id + ", name=" + 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.id != other.id) {
return false;
}
if ((this.name == null) ? (other.name != null) : !this.name.equals(other.name)) {
return false;
}
return true;
}
@Override
public int hashCode() {
int hash = 5;
hash = 47 * hash + this.id;
hash = 47 * hash + (this.name != null ? this.name.hashCode() : 0);
return hash;
}
}
现在在这里,我正在编写一个Student Class
以显示这种自定义 排序将如何执行。 结果将显示:==>
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import org.junit.Test;
import static org.junit.Assert.*;
public class StudentSortingTest {
public void testSorting(){
Student s1 = new Student(1, "Saurabh");
Student s2 = new Student(2, "Vikrant");
Student s3 = new Student(3, "Bhushan");
Student s4 = new Student(4, "Neeraj");
Student s5 = new Student(5, "Awasthi");
List<Student> obj_ListStudent = new ArrayList<Student>();
obj_ListStudent .add(s1);
obj_ListStudent .add(s2);
obj_ListStudent .add(s3);
obj_ListStudent .add(s4);
obj_ListStudent .add(s5);
//print List before Sorting.
this.printList(obj_ListStudent);
//perform customized Sorting using Collections Class.
Collections.sort(obj_ListStudent);
//print List After Sorting.
this.printList(obj_ListStudent);
}
public void printList(ArrayList<Student> list){ //Print elements of Student.
for(Student elem : list){
System.out.println(elem+" ");
}
public static void maint(String... args){ //Main Method.
StudentSortingTest obj = new StudentSortingTest();
obj.testSorting();
}
}
注意 :==>此功能可能对您进行自定义排序很有帮助,并且可能更容易实现您自己的自定义
Class
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.