-2

I am relatively new to java and I would need help on Lists. My requirement is that, I will create a list which stores the user info. I am using List of Strings to achieve this. Right now I am getting the output like [a,b,c,d,e,f,g,h]. This contains 2 records from "A to D" one record and from "e to h" another. Instead of this, I need the output as [a,b,c,d] [e,f,g,h]. Also, I need to use the highlighted separate outputs to insert into User object for which I need to iterate through the output list and add each of them to new User object say for ex: user1 should have [a,b,c,d] and user 2 have [e,f,g,h] and so on.. Can anyone please let me know how do I achieve this.

My code snippet:

User testUser = null;
List<String> userList = new ArrayList<String>();
UserImpl u = new UserImpl();
String userCommonName = null, userEmail = null, userCanonicalName = ull, 
        userPrincipalType = null;

while (pit.hasNext()) {

    testUser = (User) (pit.next());
    userCommonName = testUser.getCommonName();
    userEmail = testUser.getEmail();
    userCanonicalName = testUser.getCanonicalName();
    userPrincipalType = testUser.getPrincipalType();

    userList.add(userCanonicalName);
    userList.add(userCommonName);
    userList.add(userPrincipalType);
    userList.add(userEmail);
    System.out.println(userList);// On printing this I am getting the
                                 // above output which I need to split
                                 // into separate records.
}

The separate records which is in userList has to be inserted back to another User object.

Iterator pit1 = userList.iterator();
while (pit1.hasNext()){
    u.setCanonicalName(userCanonicalName);
    u.setCommonName(userCommonName);
    u.setEmail(userEmail);
    u.setPrincipalType(userPrincipalType);  
}

I am bit lost here. Any help much appreciated!

Duncan Jones
  • 67,400
  • 29
  • 193
  • 254
user1194310
  • 129
  • 1
  • 4
  • 14

3 Answers3

2

I have no idea why you want to do this, but it seems you need to have a list of lists of strings:

List<List<String>> userList = new ArrayList<List<String>>();

// ....

while (pit.hasNext()) {

    // ...

    List<String> innerList = new ArrayList<String>();
    innerList.add(userCanonicalName);
    innerList.add(userCommonName);
    innerList.add(userPrincipalType);
    innerList.add(userEmail);
    userList.add(innerList);

This means you can later iterate across each list in turn and rebuild your User class again.

Duncan Jones
  • 67,400
  • 29
  • 193
  • 254
  • Hi Duncan, The above code works for me! Now I need to iterate through the **userList** and insert each record in the userList into a user object. Following is the example.. UserImpl u = new UserImpl(); u.setDomainName("Domain"); u.setCanonicalName("wblue"); u.setPrincipalType("USER"); u.setEmail("sample@g.com"); But I don't know how to loop through each item in the userList. Can you help with this? – user1194310 Sep 02 '14 at 10:14
2

Just take

List<String> userList = new ArrayList<String>();

inside the 1st loop.

Ninad Pingale
  • 6,801
  • 5
  • 32
  • 55
1

Consider using a Map instead of a List. Something along the lines of the following listing. You will need to adapt it and hopefully understand my idea :)

public class myClass {
    public final static String CANONICAL_NAME = "canonical_name";
    public final static String COMMON_NAME = "common_name";
    public final static String PRINCIPAL_NAME = "principal_name";
    public final static String EMAIL = "email";
    Map<String, String> userInfo = new HashMap<>();
    ...
    public void myMethod() {
        ...
        userInfo.put(CANONICAL_NAME,userCanonicalName);
        userInfo.put(COMMON_NAME,userCommonName);
        userInfo.put(PRINCIPAL_NAME, userPrincipalType);
        userInfo.put(EMAIL, userEmail);
    }
}
Christian Kullmann
  • 558
  • 1
  • 7
  • 21