Java 如何根据对象变量值的不同程度将对象排序/分组到列表中?

Java 如何根据对象变量值的不同程度将对象排序/分组到列表中?,java,list,dictionary,arraylist,hash,Java,List,Dictionary,Arraylist,Hash,我想弄清楚如何制作这样的结构: 基本上,该列表将包含包含“pickupTime”变量(日期类型)相等的对象的列表。这意味着(参考图片)“等日期列表1”将包含具有相同拾取时间值的所有对象,而“等日期列表2”将执行相同的操作,但是“等日期列表1”和“等日期列表2”(等等)将具有不同的值 我的用例是能够在界面上显示一个显示不同日期的顶级视图。单击/点击其中一个不同的日期将显示另一个屏幕,显示具有该日期的对象 我目前的代码是: final Set<Reservation> setToRetu

我想弄清楚如何制作这样的结构:

基本上,该列表将包含包含“pickupTime”变量(日期类型)相等的对象的列表。这意味着(参考图片)“等日期列表1”将包含具有相同拾取时间值的所有对象,而“等日期列表2”将执行相同的操作,但是“等日期列表1”和“等日期列表2”(等等)将具有不同的值

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

我目前的代码是:

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);
                    }
                }
final Set setToReturn=new HashSet();
final Set set1=新的HashSet();
对于(保留:结果){
如果(!set1.add(res.getpicku_time())){
setToReturn.add(res);
}
}

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

您是否考虑过在主列表中存储对象列表而不是哈希集?(至少在国际海事组织,可能更容易合作)

例如:(伪代码)

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

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

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();
    }
}
import java.util.ArrayList;
导入java.util.Scanner;
公共班机{
公共静态void main(字符串args[])引发异常{
ArrayList mainList=新的ArrayList();
扫描仪扫描=新扫描仪(System.in);
字符串输入=”;
//在本例中,获取用户输入以创建列表
//您可以轻松地将其更改为使用.getpickuptime()
//在这种情况下,输入将被对象替换。
而(!(input=scan.nextLine()).equals(“退出”)){
//基本情况-如果列表为空,则创建一个新对象。
if(mainList.isEmpty()){
ArrayList x=新的ArrayList();
x、 添加(输入);
主列表。添加(x);
}否则{
布尔加法=假;
//检查列表以查看是否有匹配的值
对于(ArrayList x:mainList){
//选中每个ArrayList第一个对象
如果(x.get(0).equals(input)){
x、 添加(输入);
//更改添加到true
添加=真;
//您已经添加了对象,因此退出for循环。
打破
}
}
//如果你浏览了所有的列表,但是没有匹配的列表呢?
如果(!已添加){
//列一个新的清单。
ArrayList x=新的ArrayList();
x、 添加(输入);
主列表。添加(x);
}
}
}
System.out.println(mainList.toString());
scan.close();
}
}

我会尝试一下,如果我卡住了,会告诉你的。我肯定认为列表中的列表将是最简单的,但就效率而言,我不确定。无论哪种方式,它都应该能够实现我正在尝试的目标。顺便说一下,谢谢你的回复。谢谢@Aify。我用了一个稍微修改过的版本,效果很好。
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();
    }
}