java - 如何根据对象变量值的独特性将对象排序/分组到列表中?

标签 java list dictionary arraylist hash

我正在尝试弄清楚如何制作这样的结构:/image/MOgZm.jpg

基本上,该列表将保存包含“pickupTime”变量(日期类型)相等的对象的列表。这意味着(引用图片)“相等日期列表 1”将包含具有相等 PickupTime 值的所有对象,而“相等日期列表 2”将执行相同操作,但是“相等日期列表 1”和“相等日期列表 2”(依此类推)将具有不同的值。

我的用例是能够在界面上显示显示不同日期的顶级 View 。单击/点击这些不同日期之一将显示另一个屏幕,其中显示具有该日期的对象。

我当前的代码是:

final Set<Reservation> setToReturn = new HashSet<Reservation>();
final Set<Date> set1 = new HashSet<Date>();

                for (Reservation res : result) {
                    if (!set1.add(res.getPickup_time())) {
                        setToReturn.add(res);
                    }
                }

这可以很好地创建一个仅包含不同日期的单个列表,但是它不会将具有相同日期的对象排序到它们自己的组中,我觉得这有点超出了我目前的知识范围。有人可以帮我吗?

最佳答案

您是否考虑过在主列表中存储对象列表,而不是哈希集? (至少在我看来可能更容易合作)

例如:(伪代码)

get the pickuptime of the object
check to see in the main list if there is a list containing objects with that pickup time
if (above true) {
    add it to said list
} else {
    make a new list with that object
}

如果您需要新对象的名称,可以将日期转换为字符串。

这是我编写的一个快速程序来演示这个概念。它以字符串的形式接受用户输入,在程序结束时您将看到结构是如何打印出来的。

import java.util.ArrayList;
import java.util.Scanner;

public class Main {
    public static void main(String args[]) throws Exception {
        ArrayList<ArrayList> mainList = new ArrayList<ArrayList>();

        Scanner scan = new Scanner(System.in);
        String input = "";

        // in this example, get user input to make a list with
        // you can easily change this to use .getpickuptime()
        // in this case, input would be replaced by the object.
        while (!(input = scan.nextLine()).equals("quit")) {
            // base case - if the list is empty make a new object.
            if (mainList.isEmpty()) {
                ArrayList<String> x = new ArrayList<String>();
                x.add(input);
                mainList.add(x);
            } else {
                boolean added = false;
                // check the lists to see if there's a matching value
                for (ArrayList x : mainList) {
                    // check each arraylists first object 
                    if (x.get(0).equals(input)) { 
                        x.add(input);

                        // change added to true
                        added = true;

                        // you've added the object so quit the for loop.
                        break;
                    }
                }
                // what if you went through all the lists and there was no matching list?
                if (!added) {
                    // make a new list.
                    ArrayList<String> x = new ArrayList<String>();
                    x.add(input);
                    mainList.add(x);
                }
            }
        }

        System.out.println(mainList.toString());

        scan.close();
    }
}

关于java - 如何根据对象变量值的独特性将对象排序/分组到列表中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28951322/

相关文章:

java - 如何通过 JSON API 实现复杂的条件批量部分更新?

java - 当我尝试设置 clickListener 时,应用程序崩溃

c# - 按列表中的计数区分列表项

java - 替代列表数据结构

android - 在 Android 上实现消息访问配置文件(接收器端)?

java - 在Android中部署SimpleNLG,NoClassDefFoundError

list - 连接 (++) 列表而不重叠

python - 从列表创建字典时丢失 python 代码中的元素?

c# - Json Serialize Dictionary<Enum, Int32> 的问题

java - Tomcat:使用强密码的 TLSv1.2 不起作用