2

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());
  }
  });    
}
2
  • Can you share what you have tried so far? Commented May 17, 2019 at 17:25
  • I edited the post showing which I tried Commented May 17, 2019 at 17:30

5 Answers 5

3

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

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

Comments

2

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]

Comments

1

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());

2 Comments

I don;t want to retain that is there in lst. I want to get negation of lst
[1] This code won't compile. It should be Arrays.equals(allCodesList.toArray(), lst) [2] The output should be [04, 05] and not a boolean value.
0

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() );

Comments

0

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());

Comments

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.