Java HashSet.add() - Syntax & Examples - Tutorial Kart
[Pages:5]Java HashSet.add() ? Syntax & Examples
Java HashSet.add() ? Examples
In this tutorial, we will learn about the Java HashSet.add() method, and learn how to use this method to add an element to this HashSet, with the help of examples.
add(element)
HashSet.add() adds the specified element to this HashSet if it is not already present.
Since HashSet stores only unique elements, if we try to add an element that is already present, add() method returns false.
If the element is not present, add() method adds the element to the HashSet and returns true.
Syntax
The syntax of add() method is
HashSet.add(E e) where
Parameter Description
e
The element that has to be added to the HashSet.
Returns The method returns boolean value of true or false.
Example 1 ? add(element)
In this example, we will create and initialize a HashSet of Strings with some elements. We will then add a new
element e to this HashSet using add() method. We will take the value of e such that it is not already present in the HashSet. add() method should add the element to the list and return true.
Java Program
import java.util.Arrays; import java.util.HashSet;
public class Example { public static void main(String[] args) { HashSet hashSet = new HashSet(Arrays.asList("a", "b", "c")); System.out.println("Original HashSet : " + hashSet);
//add element to hashSet String e = "d"; boolean result = hashSet.add(e); System.out.println("Is the new element added to the HashSet? " + result); System.out.println("HashSet after add : " + hashSet); } }
Output
Original HashSet : [a, b, c] Is the new element added to the HashSet? true HashSet after add : [a, b, c, d]
Example 2 ? add(element) ? Element already present
In this example, we will try to add an element e to the HashSet that is alredy present. add() should return a boolean value of false.
Java Program
import java.util.Arrays; import java.util.HashSet;
public class Example { public static void main(String[] args) { HashSet hashSet = new HashSet(Arrays.asList("a", "b", "c")); System.out.println("Original HashSet : " + hashSet);
//add element to hashSet String e = "a"; boolean result = hashSet.add(e); System.out.println("Is the new element added to the HashSet? " + result); System.out.println("HashSet after add : " + hashSet); } }
Output
Original HashSet : [a, b, c] Is the new element added to the HashSet? false HashSet after add : [a, b, c]
Example 3 ? add(element) ? User Defined Object
In this example, we will create a HashSet that can store elements of user defined class Car , and add elements to this HashSet.
Java Program
import java.util.HashSet;
public class Example { public static void main(String[] args) { HashSet hashSet = new HashSet(); hashSet.add(new Car(1, "Tesla")); hashSet.add(new Car(2, "BMW")); hashSet.add(new Car(3, "Toyota"));
System.out.println(hashSet); } }
class Car { int id; String name; public Car(int id, String name) { this.id = id; this.name = name; }
@Override public String toString() {
return id + "-" + name; } }
Output
[1-Tesla, 3-Toyota, 2-BMW]
Example 4 ? add(element) ? Null HashSet
In this example, we will initialize a HashSet with null. If we call add() method on this null object, the method would throw java.lang.NullPointerException .
Java Program
import java.util.HashSet;
import java.util.HashSet; public class Example {
public static void main(String[] args) { HashSet hashSet = null; hashSet.add("a");
} }
Output
Exception in thread "main" java.lang.NullPointerException at Example.main(Example.java:6)
Conclusion
In this Java Tutorial, we have learnt the syntax of Java HashSet.add() method, and also learnt how to use this method with the help of example programs.
Java HashSet Java HashSet.add() Java HashSet.clear() Java HashSet.clone() Java HashSet.contains() Java HashSet.isEmpty() Java HashSet.iterator() Java HashSet.remove() Java HashSet.size() Java HashSet.spliterator()
Java Tutorial Java Tutorial Java Introduction Java Installation IDEs for Java Development Java Datatypes Java Variable Types Java Access Modifiers
Java Access Modifiers Java Operators Java Decision Making Java Loops Java Array Java OOPs Java String Java Exception Handling Java File Operations Java Date & Time Java MySQL Java Random Java Math
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related download
- java syntax examples tutorial kart
- structural testing white box exercises github pages
- linked lists bu
- must have cheat sheets for java developers
- the list adt western university
- collections in java aau
- nullaway practical type based null safety for java
- add e e method example tutorials point
- java collections list set map stanford university
- java list adt university of pennsylvania
Related searches
- java collections tutorial with examples
- download java tutorial pdf
- core java tutorial pdf
- java programming tutorial for beginners
- java tutorial hello
- java programming tutorial pdf
- java basic tutorial pdf
- java programming language tutorial pdf
- java tutorial w3schools
- java tutorial pdf for beginners
- java programming tutorial pdf free
- java programming tutorial pdf 2019