简体   繁体   English

在while循环用完之前对其中的地图列表进行排序(Java)

[英]Sorting a list of maps within before this while loop runs out(Java)

A database call is made and result is a bunch of rows of two string columns of type A and B . 进行数据库调用,结果是一串由两个类型为AB字符串列组成的行。 eg (x_a, y_b), (x_a, y1_b), (x2_a,y_b) 例如(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. 这个想法是要提出一个列表列表,例如{(x_a,{y_b,y1_b}), (x2_a,{y_b})} ,其中不会重复类型A的对象,并在从a提取结果时执行此操作数据库。

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. 当结果对象的最后一个type1值相同时,此方法不起作用。

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"? 有没有一种方法可以按照相同的方式(在while(res.next内)进行此操作,而无需先将数据库调用的结果存储在单独的数组中,或者在while循环之外添加额外的for循环以“修补”它?

The simple way to do this is to use a Guava / Google Collections SetMultiMap . 做到这一点的简单方法是使用Guava / Google Collections SetMultiMap This is essentially a mapping from a key (your 'A' objects) to a set of values (your 'B' objects). 本质上,这是从键(您的“ A”对象)到一组值(您的“ B”对象)的映射。

[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. 如果可以这样做,则将减少通过数据库连接发送的(冗余)数据量……假设您使用的是JDBC。

If you don't want duplicates like {x_a:[y_b, y_b]} then use a set as the value of your map: 如果您不想像{x_a:[y_b,y_b]}这样的重复项,则使用set作为地图的值:

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. 您可能只需要通过type1_type2映射即可。 Process each (x, y) in pseudo-code: 用伪代码处理每个(x,y):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM