在 while 循环结束之前对地图列表进行排序(Java)

发布于 2024-11-07 07:12:03 字数 1826 浏览 0 评论 0 原文

进行数据库调用,结果是由 AB 类型的两个字符串列组成的一堆行。例如 (x_a, y_b), (x_a, y1_b), (x2_a,y_b)

这个想法是想出一个像 {(x_a,{y_b,y1_b}) 这样的地图列表, (x2_a,{y_b})} 其中类型 A 的对象不重复,并且在从数据库中提取结果时执行此操作。

这是我尝试过的:

int i =0;
            List<String> type2 = new ArrayList<String>();
            Map<String,List<String>> type1_type2 = new HashMap<String,List<String>>();
            List<Map> list_type1_type2 = new ArrayList<Map>();

            String [] type1Array = new String[100];
            String [] type2Array = new String[100];
            int trackStart = 0;
while (res.next()){


                String type1 = res.getString(1);
                String type2 = res.getString(2);
                type1Array[i]=type1;
                type2Array[i] = type2;


                if(i>0 && !type1Array[i].equals(type2Array[i-1])){
                    int trackStop = i;
                    for(int j = trackStart; j<trackStop;j++){
                        type2.add(type2Array[j]);
                    }
                    type1_type2.put(type1Array[i-1], type2);
                    list_type1_type2.add(type1_type2);

                //debugging stuff   
                 String x = list_type1_type2.toString();
         System.out.println(x);
System.out.println(" printing because "+ type1Array[i]+" is not equal to " + type1Array[i-1]);
        type2 = new ArrayList<String>();
     type1_type2 = new HashMap<String,List<String>>();
                     trackStart=i;
                     }

                     i++;


                }

当结果对象的最后一个 type1 值相同时,此方法不起作用。

有没有一种方法可以以同样的精神(在 while(res.next) 内)执行此操作,而无需首先将数据库调用的结果存储在单独的数组中或在 while 循环之外添加额外的 for 循环来“修补它”?

A database call is made and result is a bunch of rows of two string columns of type A and B. e.g. (x_a, y_b), (x_a, y1_b), (x2_a,y_b)

The idea is to come up with a list of maps like {(x_a,{y_b,y1_b}), (x2_a,{y_b})} where the objects of type A are not repeated and to do this while pulling the results from a database.

Here's what I tried:

int i =0;
            List<String> type2 = new ArrayList<String>();
            Map<String,List<String>> type1_type2 = new HashMap<String,List<String>>();
            List<Map> list_type1_type2 = new ArrayList<Map>();

            String [] type1Array = new String[100];
            String [] type2Array = new String[100];
            int trackStart = 0;
while (res.next()){


                String type1 = res.getString(1);
                String type2 = res.getString(2);
                type1Array[i]=type1;
                type2Array[i] = type2;


                if(i>0 && !type1Array[i].equals(type2Array[i-1])){
                    int trackStop = i;
                    for(int j = trackStart; j<trackStop;j++){
                        type2.add(type2Array[j]);
                    }
                    type1_type2.put(type1Array[i-1], type2);
                    list_type1_type2.add(type1_type2);

                //debugging stuff   
                 String x = list_type1_type2.toString();
         System.out.println(x);
System.out.println(" printing because "+ type1Array[i]+" is not equal to " + type1Array[i-1]);
        type2 = new ArrayList<String>();
     type1_type2 = new HashMap<String,List<String>>();
                     trackStart=i;
                     }

                     i++;


                }

This method does not work when the last type1 values of the result object are the same.

Is there a way to do this in the same spirit (within the while(res.next)) without first storing the results of the database call in separate arrays or adding an extra for loop outside the while loop to "patch it up"?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

欲拥i 2024-11-14 07:12:03

最简单的方法是使用 Guava / Google Collections SetMultiMap。这本质上是从键(您的“A”对象)到一组值(您的“B”对象)的映射。

[我不会尝试为你编写代码。你当前的代码太可怕了,无法阅读......除非你付钱给我:-)]

但是,更好的主意是让数据库进行整理。如果您能做到这一点,您将减少通过数据库连接发送的(冗余)数据量……假设您使用的是 JDBC。

The simple way to do this is to use a Guava / Google Collections SetMultiMap. This is essentially a mapping from a key (your 'A' objects) to a set of values (your 'B' objects).

[I'm not going to try to code it for you. Your current code is too horrible to read ... unless you were paying me :-) ]

However, a better idea would be to get the database to do the collation. If you can do that, you will reduce the amount of (redundant) data that gets send across the database connection ... assuming that you are using JDBC.

过潦 2024-11-14 07:12:03

如果您不想要像 {x_a:[y_b, y_b]} 这样的重复项,请使用集合作为地图的值:

Map<String,Set<String>> type1_type2;

我不知道其他各种列表和数组的用途。您可能只需使用 type1_type2 映射即可。用伪代码处理每个 (x, y):

Set s = type1_type2.get(x)
if s == null:
    s = new Set()
    type1_type2.put(x, s)
s.add(y)

If you don't want duplicates like {x_a:[y_b, y_b]} then use a set as the value of your map:

Map<String,Set<String>> type1_type2;

I don't know what the other various list and arrays are for. You can probably just get by with the type1_type2 map. Process each (x, y) in pseudo-code:

Set s = type1_type2.get(x)
if s == null:
    s = new Set()
    type1_type2.put(x, s)
s.add(y)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文