简体   繁体   中英

Java 8 set stream loop and compare with array elements

I have a set which has list of Obects of type TempDTO.

public class TempDTO {
   public String code;
   //getter
   //setter
}

Set<TempDTO> tempSet = service.getTempList();
tempSet has values whose code is ["01", "02", "04", "05"];
String[] lst = ["01", "02"]

I want to loop tempSet compare value with that of in lst array and I need a list of values when value doesn't match. out put expected is : ["04", "05"] I tried,

for(int i=0; i < lst.length; i++){
  String st = lst[i];
  tempSet.stream().forEach(obj -> {
  if((obj.getCode().equals(st) )){
    logger.debug(" equal " + obj.getCode());
  } else {
    logger.debug("not equal " + obj.getCode());
  }
  });    
}

You may use:

// convert array(lst) to arrayList
List<String> arrList = Arrays.asList(lst);

// now check if the values are present or not
List<String> nonDupList = tempSet.stream()
                                  .filter(i -> !arrList.contains(i.getCode()))
                                  .map(TempDTO::getCode)
                                  .collect(Collectors.toList());

which outputs:

[05, 04]

This will output all values that don't match:

Set<TempDTO> tempSet = new HashSet<>(Arrays.asList(
        new TempDTO("01"),
        new TempDTO("02"),
        new TempDTO("04"),
        new TempDTO("05")));
String[] arr = new String[] { "01", "02" };

List<String> list = Arrays.asList(arr);

List<String> output = tempSet
                        .stream()
                        .filter(temp -> !list.contains(temp.getCode()))
                        .map(temp -> temp.getCode())
                        .collect(Collectors.toList());

System.out.println(output);

Output:

[04, 05]

If you want to retrieve the TempDTO objects, leave out the .map(...) call

You have to follow the steps.

  1. Get all the list of codes like this:

     List<String> allCodesList = tempSet.stream() .map(value -> value.getCode()) .collect(Collectors.toList()) ; 
  2. You already have a second list.

  3. Check boolean result = Arrays.equals(allCodesList.toArray(),lst.toArray());

This will get you a set of all the TempDTO objects that do not have codes in your list

tempSet = tempSet.stream()
            .filter( obj -> !Arrays.stream( lst ).anyMatch( str -> str.equals( obj.getCode() ) ) )
            .collect( Collectors.toSet() );

I suggest use filter method, first create an auxiliary list to use contains method, crate a Function to get the needed value (code) then a predicate to filter values not contained in auxiliar list, finally collect values in a list

// create an auxiliar list 
List<String> auxLst = Arrays.asList(lst);

// map to code
Function<TempDTO, String> map = t -> t.getCode();
// condition
Predicate<String> lstNotcontains = code -> !auxLst.contains(code);

List<String> missingValues = tempSet.stream().map(map).filter(lstNotcontains).collect(Collectors.toList());

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