3:06 pm - Friday September 26, 3180

Check whether element present in LinkedHashSet using boolean contains(Object o)

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044

BACK

                          The method public boolean contains(Object o) checks whether the specified element is present in the linkedhashset .Returns true if the element is present,otherwise returns false.

 Example Program


import java.util.LinkedHashSet;

public class LinkedHashSetContains {

	public static void main(String[] args) {

		LinkedHashSet lh = new LinkedHashSet();

		lh.add("A");

		lh.add("B");

		lh.add("C");

		lh.add("D");

		System.out.println("The set is:" + lh);

		boolean val = lh.contains("B");

		if (val == true) 
		{

			System.out.println("Element 'B' is contained in the set");

		 }

		else 
		{

			System.out.println("Element 'B' is not contained in the set");

		 }

		boolean val1 = lh.contains("F");

		if (val1 == true) 
		{

			System.out.println("Element 'F' is contained in the set");

		 }

		else 
		{

			System.out.println("Element 'F' is not contained in the set");

		 }

	}

}

Output

   The set is: [A, B, C, D]

   Element ‘B’ is contained in the set

   Element ‘F’ is not contained in the set

 

BACK

Filed in: Java Examples

Comments are closed.