Delete element when Java traverses map

Source: Internet
Author: User

  1. Package net.nie.test;
  2. Import Java.util.HashMap;
  3. Import Java.util.Iterator;
  4. Import Java.util.Map;
  5. Public class Hashmaptest {
  6. private static Map<integer, string> map=new hashmap<integer,string> ();
  7. /** 1.HashMap class mappings are not guaranteed in order; some mappings can be explicitly guaranteed in their order: TreeMap class
  8. * 2. Map.put (Key,newval), Map.Remove (key) cannot be used to modify and delete elements during traversal of the map.
  9. * Causes concurrent modification exceptions, which can be passed through the iterator's remove ():
  10. * Removes the current iteration element from the collection that the iterator points to
  11. * To achieve the purpose of deleting elements in the access.
  12. *   */
  13. public static void Main (string[] args) {
  14. Map.put (1,"one");
  15. Map.put (2,"a");
  16. Map.put (3,"three");
  17. Map.put (4,"four");
  18. Map.put (5,"Five");
  19. Map.put (6,"Six");
  20. Map.put (7,"seven");
  21. Map.put (8,"eight");
  22. Map.put (5,"Five");
  23. Map.put (9,"Nine");
  24. Map.put (Ten);
  25. Iterator<map.entry<integer, string>> it = Map.entryset (). Iterator ();
  26. While (It.hasnext ()) {
  27. Map.entry<integer, string> entry=it.next ();
  28. int Key=entry.getkey ();
  29. if (key%2==1) {
  30. System.out.println ("Delete this:" +key+"=" +key);
  31. //map.put (Key, "odd"); Concurrentmodificationexception
  32. //map.remove (key); Concurrentmodificationexception
  33. It.remove (); //ok
  34. }
  35. }
  36. //traverse the current map; This new for loop cannot modify the map content because it does not pass through the iterator.
  37. System.out.println ("-------\n\t elements of the final map traversal:");
  38. for (Map.entry<integer, string> entry:map.entrySet ()) {
  39. int K=entry.getkey ();
  40. String V=entry.getvalue ();
  41. System.out.println (k +"=" +v);
  42. }
  43. }
  44. }

Basic type, there is no exception.

List in

(1). There is no exception when using the index traversal, but the subsequent data may be problematic;

(2). List with enhanced for loop over duration: Exception is reported java.util.ConcurrentModificationException

(except for immediate break after deletion)

(3). There is no problem with iterators iterator.remove () when iterating.

Delete element when Java traverses map

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.