简体   繁体   中英

How to select unique item from list?

I have a list for user IDs. It consists of user IDs selected from different tables, so there are duplicate IDs in it. How can I only select unique IDs from this list or possibly remove duplicates?

List < userDto > list = uC.match(dto2);
if (list.size() > 0) {
    for (int i = 0; i < list.size(); i++) {
        System.out.println(list.size());
        System.out.println("Data Found");

        userDto dto3 = new userDto();
        dto3 = uC.get(list.get(i));

        System.out.println(dto3.firstName);
    }
} else {
    System.out.println("Data not Found");
}

I edited as below it shows the same result as List..I am new to this. I don't know whats wrong.. please help

Set<userDto> list = new HashSet<userDto>(uC.match1(dto2));

    if(list.size()>0){
    for (int i = 0; i < list.size(); i++) {
        System.out.println(list.size());
        userDto dto3=new userDto();
        for (userDto s : list) {
            dto3=uC.get(s);
        }
        System.out.println(dto3.firstName);
        }
    }

add them all to a set that is what a Set does

Use set for unique values.

 Set<userDto> hSet = new HashSet<userDto>(list); 

Instead of List you can use Set as set stores unique elements. Also you can convert List to Set

 Set<userDto> list=new HashSet<userDto>(uC.match(dto2));

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