简体   繁体   中英

What's the best way to populate List<Object>?

There are 100 Branch objects in List<Branch> and already set branchId (001 to 100).I want to use List<Branch> to make a report with iReport.

But now my data all in the other List List<Temp> and The data is duplicate in List<Temp> .

branchId  firstName  lastName type         number
-------------------------------------------------------------
005         Amy        Lee     home        1234567
005         Amy        Lee     office      9999999
005         Amy        Lee     cellphone   1111111
005         John       Lin     home        1231231
067         Louis      Tsai    home        0809097
....

My Question is as follows:
I need use List<Temp> convert to List<Branch> and if branchId not match in List<Branch> , just set List<User> null. How can I do?

public class Temp{

    private String branchId;
    private String firstName;
    private String lastName;
    private String type;
    private String number;

    //getter and setter here

}

Branch Bean

public class Branch {

    private String branchId;
    private List<User> user;

    //getter and setter here

}

User Bean

public class User {

    private String firstName;
    private String lastName;
    private List<Phone> phones;

    //getter and setter here

}

Phone Bean

public class Phone {

    private String type;
    private String number;

    //getter and setter here

}

Thanks a lot.

-----------update----------------

As you can see, below is what I need it look like:
There are Amy Lee and John Lin sorted under branchId 005 and a different branchId 067 will show in different page.With the same branchId, ex: 005 Amy Lee, there are 3 kinds of numbers under her. Now, report need retrieve List<Branch> . That's the reason why I need loop List<Temp> and set List<Branch> . Is there any way that I can make it?

005
-----------------------------------
Amy        Lee     home        1234567
                   office      9999999
                   cellphone   1111111
John       Lin     home        1231231

next page

067
-----------------------------------
Louis      Tsai    home        0809097

I would replace those Lists by maps if I were you, and the usage is going to be kind of like this.

List<temp> t; 
for (temp t : tempList) { 
    if ((Branch b = branchMap.get(t.getBranchID)) != null) { 
        if ((user u = b.getUsers.get((t.getFirstName() + t.getLastName()) != null) { 
            user.getPhonse.add(new Phone(t.getPhoneType(), t.getPhoneNumber())) 
        } 
    } else { 
        List<Phone> phonse = new ArrayList<Phone>();
        phonse.add(new Phone(t.getPhoneType(), t.getPhoneNumber()));

        Map<String,String> users = new hashMap<String,String>();
        users.put(t.getFirstName() + t.getLastName(), new user(t.getFirstName(), t.getLastName(), phones);

        branchMap.put(t.getBranchID, new Branch(t.getBranchID(), new hashMap<String,String>().put(t.getFirstName() + t.getLastName(), new user(t.getFrstName(), t.getLastName(), new List<Phone>)))))) 
    } 
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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