3:11 pm - Sunday November 23, 6453

Fetch all keys from a LinkedHashMap Set keySet()

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 set<k>keyset() returns a view of the elements present in the set. Incase of removing an element from the set,keyset.remove(k) can be used.After removing the particular key the remaining elements in the set can be displayed.

Example Program


import java.util.LinkedHashMap;

import java.util.Set;

public class LinkedHashMapKeySet

{

	public static void main(String[] args) {

		LinkedHashMap lhm = new LinkedHashMap();

		lhm.put("1", "Add");

		lhm.put("2", "Sub");

		lhm.put("3", "Mul");

		Set keySet = lhm.keySet();

		System.out.println("keySet contains : " + keySet);

		keySet.remove("2");

		System.out.println("After removing '2':" + keySet);

		System.out.println("linkedHashMap contains : " + lhm);

	}

}


Output

    keySet contains : [1, 2, 3]

    After removing ‘2’:[1, 3]

     linkedHashMap contains : {1=Add, 3=Mul}

BACK

Filed in: Java Examples

Comments are closed.