noobie
noobie

Reputation: 31

How to check java Character List there is any value besides C or N

I have List<Character> and i'm trying need to figure out how to check weather there is any value besides C or N in the list

String sql = "SELECT distinct(CODE_STATUS_TYPE) FROM AMEQPS_REWQUEST.PSFDSS_CR_TB " +
             " WHERE  NBR_CR_KEY != :nbrCrKey";


Query q = em.createNativeQuery(sql).setParameter("nbrCrKey", cdId);
List<Character> codeStatus = (ArrayList<Character>) q.getResultList();

// THIS WILL RETURN ANYTHING W,R,D,C,N 

Would if block like this be efficient? Thank you Is this proper data check?

if(!codeStatus.contains('C')&&!codeStatus.contains('N')){

}

Upvotes: 2

Views: 70

Answers (2)

Arvind Kumar Avinash
Arvind Kumar Avinash

Reputation: 79435

Using Stream, you can do it as follows:

import java.util.Arrays;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        List<Character> codeStatus = Arrays.asList(new Character[] { 'C','N'});
        System.out.println(!codeStatus.stream().allMatch(c->c.equals('N') || c.equals('C')));
    }
}

Output:

false

Another run: import java.util.Arrays; import java.util.List;

public class Main {
    public static void main(String[] args) {
        List<Character> codeStatus = Arrays.asList(new Character[] { 'C','N','T'});
        System.out.println(!codeStatus.stream().allMatch(c->c.equals('N') || c.equals('C')));
    }
}

Output:

true

If you do not want to use Stream, you can do it as follows:

import java.util.Arrays;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        List<Character> codeStatus = Arrays.asList(new Character[] { 'C','N','T'});
        if (codeStatus.size()==0){
            System.out.println("The list does not contain any element");
        } else if (codeStatus.size()==1) {
            if(codeStatus.contains('C') || codeStatus.contains('N')) {
                System.out.println("Either 'C' or 'N' exists");
            } else {
                System.out.println("Neither 'N' nor 'C' exists");
            }            
        } else if(codeStatus.size()==2) {
            if (codeStatus.contains('C') && codeStatus.contains('N')) {
                System.out.println("Only 'C' and 'N' exists");
            } else if((codeStatus.contains('C') || codeStatus.contains('N'))){
                System.out.println("Other than 'C' or 'N', some other character(s) exist(s)");
            }
        }else if (codeStatus.size()>2) {
            if((codeStatus.contains('C') && codeStatus.contains('N'))) {
                System.out.println("Other than 'C' and 'N', some other character(s) exist(s)");
            }else if((codeStatus.contains('C') || codeStatus.contains('N'))){
                System.out.println("Other than 'C' or 'N', some other character(s) exist(s)");
            } else {
                System.out.println("Neither 'N' nor 'C' exists");
            }           
        }
    }
}

Output:

Other than 'C' and 'N', some other character(s) exist(s)

Upvotes: 1

Ryuzaki L
Ryuzaki L

Reputation: 40078

In the above approach you are iterate the List twice and checking it has C or N, but by using stream and allMatch you can complete in one iteration

codeStatus.stream().allMatch(c->c.equals('N') || c.equals('C'));  

Upvotes: 2

Related Questions