0

Using JDBC, I have managed to run a query on a database and receive a result set (rs). Using this information, I hope to generate a nested array list.

// Created Array List
public static ArrayList<ArrayList<SessionRecord>> tempSessionOrg = new ArrayList<ArrayList<SessionRecord>>();

The inner list needs to be grouped by the information returned from the first column. And this is all I've got thus far:

while(rs.next()) { 
          SessionRecord temp = new SessionRecord(rs.getString("SessionID"),rs.getString("NetworkAddress"),rs.getString("EventType"),rs.getString("Time"),rs.getString("Name"),rs.getString("SessionType"),rs.getString("ProcessType")); 
      }

I've already written a very similar program, with the exception that it places the result set into a single ArrayList without nesting. Unfortunatley, this analagous piece of code hasn't really helped me come up with a solution.

while(rs.next()) {
    dbSession.add(new SessionRecord(rs.getString("name"),rs.getString("ParticipantName"),rs.getString("GuestLoggedOnUsername"),rs.getString("GuestMachineName"),rs.getString("inicio"),rs.getString("diferencia")));
}

Any suggestions?

EDIT:

At this point, I have the following two blocks one code.

One:

public static ArrayList<SessionRecord> singleSessionRecords = new ArrayList<SessionRecord>();
public static ArrayList<ArrayList<SessionRecord>> tempSessionOrg = new ArrayList<ArrayList<SessionRecord>>();

Two:

while(rs.next()) { 
          singleSessionRecords.add(new SessionRecord(rs.getString("SessionID"),rs.getString("NetworkAddress"),rs.getString("EventType"),rs.getString("Time"),rs.getString("Name"),rs.getString("SessionType"),rs.getString("ProcessType"))); 
      }

      Map<String, List<SessionRecord>> byID = singleSessionRecords.stream().collect(Collectors.groupingBy(SessionRecord::SessionID));
      tempSessionOrg.add((ArrayList<SessionRecord>) Map.values());

I'm receiving a type mismatch error for the Map line and that I can't make a static reference to a non-static method in the final line. The later of the two is easy enough of a fix for me, but I'm not sure how to implement the Map properly.

1
  • Use a Map<String, List<SessionRecord>> where the key is the first column value, and the value is a list of all the records having this first column values. Once the map is created, creating a List<List<SessionRecord>> is a piece of cake: new ArrayList<>(map.values()) Commented Sep 9, 2014 at 21:10

1 Answer 1

1

Are you using Java 8? If so this could easily be achieved by this code :

Map<String, List<SessionRecord>> byName
         = temp.stream()
                    .collect(Collectors.groupingBy(SessionRecord::name));

In this example I'm grouping the sessionRecords by name, you can easily change this to fit your grouping needs.

Sign up to request clarification or add additional context in comments.

4 Comments

When I modify your example to group by SessionID (literally only changing "name" to "SessionID" in this case), I get a type mismatch error. It says it can't convert Map<Object,List<SessionRecord>> to Map<String,List<SessionRecord>>. Do you have any suggestions on how to correct my statement?
What type is SessionID? Change the map to suite that type for example: Map<SessionID, List<SessionRecord>> OR Map<?, List<SessionRecord>>
SessionID is a String
Well, its probably not. Check if the line String x = sessionRecord.sessionId; complies. You probably declared sessionId as an object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.