Java determines whether the various types of data are empty

Source: Internet
Author: User

1, determine whether the list is empty (Map, set with the list)

if null && list.size () = =0ifnull &&! List.isEmpty ()) {}list! =null: Determine if the presence of list,null means that the list does not point to anything, and if you invoke its method at this time, a null pointer exception appears. List.isEmpty (): Determine if there are elements in  the list list.size (): Judging the list has a few elements so the best way to determine if there are elements in the list is:ifnull &&! List.isEmpty ()) {  //list exists and contains elements }

2. Determine if the string type data is empty

Directly with if (S.equals (")), if(!s.isempty ()),if(s.length () >0) to judge: Ignoring the case of s NULL, s pointing to an indeterminate object, Unable to invoke a method that determines the Sting object (null; (2) "". Equals (str); (3) str.length <= 0; 4) Str.isempty ();

3. Determine if the date type data is empty

// instantiation of if (date==null) {System.out.println ("date is empty");} Else {System.out.println ("date is not empty");}

Java determines whether the various types of data are empty

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.