Non-null Judgment of Java objects and collections

Source: Internet
Author: User

Self-summary, what is not in place, please correct the supplement, thank you!

Objective: To make the procedure more rigorous

Object validation is NOT NULL: if (null! = obj)

List validation is not empty: if (null! = List && list.size () > 0)

Map validation is not empty: if (null! = Map && map.size () > 0)

Well, nonsense not much to say, on the code

Entity class student (random one)

 Packagecom.core.test; Public classStudent {Private intID; PrivateString name;  Public intgetId () {returnID; }     Public voidSetId (intID) { This. ID =ID; }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }}

Test class Collectiontest

 Packagecom.core.test;Importjava.util.ArrayList;ImportJava.util.HashMap;Importjava.util.List;ImportJava.util.Map; Public classCollectiontest { Public Static voidMain (string[] args) {Student stu=NewStudent (); System.out.println ("Student Object--" +Stu); List<Student> stulist =NewArraylist<student>(); if(NULL! = stulist && stulist.size () > 0) {System.out.println ("Student list is not empty"); } Else{System.out.println ("Student list is empty"); } System.out.println ("Student list-->" +stulist); //map<string,object> Map = null;map<string, object> map =NewHashmap<string, object>(); //map.put ("Stu", Stu);        if(NULL! = Map && map.size () > 0) {System.out.println ("Map is not empty"); } Else{System.out.println ("Map is empty"); } System.out.println ("Map Collection--" +map); }}

Results:

Student objects--[email protected]student list is empty student list--[]map is empty map collection -->{}

Summary: The set determines non-null except to verify if NULL, and also to verify that size is greater than 0

Non-null Judgment of Java objects and collections

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.