Example & Tutorial understanding programming in easy ways.

Can a null element added to a Treeset or HashSet?

A null element can be added only if the Set contains one element because when a second element is added then as per set definition a check is made to check duplicate value and comparison with null element will throw NullPointerException.

HashSet is based on HashMap and can contain null element.

Read More →