Discover The mysteries of Set set = Hashmap.keyset ()

Source: Internet
Author: User
Tags set set

There are still a lot of things to look out for between set and HashMap:

hashmap<string,string> map = new hashmap<string,string> ();
Map.put ("1", "AA");
Map.put ("2", "BB");
Map.put ("3", "AA");

set<string> set = Map.keyset ();

1. The keyset () method of map only returns a set instance, so when you delete an object from Key1, the other will also be affected.

2. For set cannot be add operation, otherwise will report "Java.lang.UnsupportedOperationException". The reasons are as follows:

Public set<k> Keyset () returns the Set view of the keys contained in this map. The collection is supported by mappings, so changes to the mappings are also reflected in the collection and vice versa. The collection supports the removal of elements by removing the corresponding mapping from the mapping through the Iterator.remove, Set.remove, RemoveAll, Retainall, and clear operations. It does not support add or addall operations.

3.

Set<string> set= Map.keyset ();

for (String Key:set) {

Map.Remove (key);

}

This code reports this exception: Exception in thread "main" java.util.ConcurrentModificationException
At Java.util.hashmap$hashiterator.nextentry (Unknown Source)
At Java.util.hashmap$keyiterator.next (Unknown Source)
At Com.nan.an.jing.TestSet.main (testset.java:23)

The reason: Because after deleting a entry, because the Ketset is incorrect, the problem occurs in the For loop.


The correct method is:

string[] keyset = Map.keyset (). ToArray (new string[0]);

for (String Key:keyset) {

Map.Remove (key);

} or

object[] keyset = Map.keyset (). ToArray ();
for (Object Key:keyset) {
Map.Remove ((String) key);
}


NOTES: Code instances

Package com.nan.an.jing;
Import java.util.ArrayList;
Import Java.util.HashMap;
Import Java.util.Iterator;
Import java.util.List;
Import Java.util.Map;
Import Java.util.Map.Entry;

Import Java.util.Set; public class Testset {public static void main (string[] args) {hashmap<string,string> map = new Hashmap<str
	 Ing,string> ();
	 Map.put ("1", "AA");
	 Map.put ("2", "BB");
	 
	 Map.put ("3", "AA");
	 
	 Map.Remove ("1");
	 Must be converted to array, or string[] keyset = Map.keyset (). ToArray (new string[0]);
	 object[] keyset = Map.keyset (). ToArray ();
	 for (Object key:keyset) {map.remove ((String) key);
	 
	 } System.out.println (map);
	 Set only one instance, set Delete, map also follow delete set<string> Set = Map.keyset ();
	 Set.remove ("1");
	 SYSTEM.OUT.PRINTLN (set);
 
	 SYSTEM.OUT.PRINTLN (map);
     The iterator method deletes the map for (iterator<string> iterator = Set.iterator (); Iterator.hasnext ();)
         {Iterator.next ();
     Iterator.remove ();
	 
	 } System.out.println (map); Adopt itErator method obtains map information set<entry<string, string>> set0 = Map.entryset (); For (iterator<map.entry<string, string>> iterator = Set0.iterator (); Iterator.hasnext ();) {Map.Entry<
		 String, string> mm = Iterator.next ();
		 String key1 = Mm.getkey ();
	 String value = Mm.getvalue ();
	 
	  } System.out.println (SET0);
	 Use list to add element hashmap<string,string> Map1 = new hashmap<string,string> ();
	 Map1.put ("1", "AA");
		
	 Map1.put ("2", "BB");
	 set<string> Set1 = Map1.keyset ();	 
	 list<string> ll = new arraylist<string> ();
	 for (String Ss:set1) {ll.add (ss);
	 } ll.add ("CC");
	 System.out.println (LL);
System.out.println (MAP1);
 }
}


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.