Java Collection Class Traversal Delete method test and use scene record

Source: Internet
Author: User
Tags concurrentmodificationexception

Package Test0;import Java.util.list;import Java.util.map;import java.util.map.entry;import java.util.Set;import Java.util.arraylist;import Java.util.hashmap;import Java.util.hashset;import Java.util.iterator;import java.util.linkedlist;/** * Test Collection Class Traversal Delete method * is currently the basic traversal method, does not contain java8 inside the traversal way */public class Test2 {public static void main (String [] args) {ForEach (); Operatelist (); Operateset (); Operatemap ();} /** * JDK1.5, the application of new features For-each loop, universal traversal read all the methods of the collection class, but cannot delete, * For-each syntax is eventually converted to Iterator.next () by the compiler, so should be similar to iterator performance * Now when testing to a large amount of data, such as tens of thousands of data, the performance gap will come out * if the traversal is to modify the collection class, the Java.util.ConcurrentModificationException exception will be thrown * For example: * Exception in Thread "main" Java.util.ConcurrentModificationException * at Java.util.arraylist$itr.checkforcomodification (Unknown SOURCE) *at Java.util.arraylist$itr.next (Unknown source) *at Test.main (test.java:12) */public static void ForEach ( {//listarraylist<integer> arr = new arraylist<integer> (); Arr.add (1); Arr.add (2); for (Integer I:arr) {Syst Em.out.priNtln ("For each list:" + i);} sethashset<integer> hs = new hashset<integer> () Hs.add (3); Hs.add (4); for (Integer i:hs) { System.out.println ("For each set:" + i);} Maphashmap<integer, integer> HM = new Hashmap<integer, integer> (); Hm.put (5, 5); Hm.put (6, 6); for (Entry <integer, integer> Entry:hm.entrySet ()) {System.out.println ("for each map: <" + Entry.getkey () + "," + ENTRY.G Etvalue () + ">");}} /** * Operation list * LinkedList is the list form, which is used to traverse the list and delete element Method 2 is more efficient * ArrayList is an array form, the two methods are not much difference */public static void Operatelist () {fi nal int size = 10; list<integer> list = new arraylist<integer> (); for (int i = 0; i < size; i++) {list.add (3);} Traverse list and Delete element method 1for (int i = List.size ()-1; I >= 0; i--) {if (List.get (i) = = 3) {list.remove (i);}} for (int i = 0; i < size; i++) {list.add (3);} Traverse list and Delete element method 2for (Iterator<integer> it = List.iterator (); It.hasnext ();) {Integer E = It.next (); if (E.equals (3)) {It.remove ();}}} /** * Operation set */public static void Operateset () {final int size = 10; set<integer> set = new hashset<integer> (); for (int i = 0; i < size; i++) {set.add (i);} Traverse set and Delete element method for (iterator<integer> it = Set.iterator (); It.hasnext ();) {Integer E = It.next (); if (E.equals (3) | | e.equals (4)) {It.remove ();}}} /** * Operation Map, according to the requirements of the choice of traversal method, a small amount of data is not good contrast performance gap, it takes millions of data to easily compare the effect * 1.ENTRYSET traversal: Key value is required: Key value is always needed to remove the use of higher performance * 2. Keyset traversal method: Key value is required: The main use of key, a small amount of use of value when using higher performance * 3.values traversal value, this does not get key, but also can be deleted: Use the main value when using * * NOTE: * Because HashMap uses the hash algorithm to store, so the time complexity is O (1), * and treemap use red black tree storage, time complexity is O (logn), * So if you always need key value, TreeMap use EntrySet traversal method efficiency will be higher, While the HashMap performance difference is not so obvious */public static void Operatemap () {final int size = 10; Map<integer, integer> map = new Hashmap<integer, integer> (), for (int i = 0; i < size; i++) {Map.put (I, i);} 1.entrySet traversal method for (Iterator<entry<integer, integer>> it = Map.entryset (). Iterator (); It.hasnext ();) {Entry<integer, integer> e = It.next (); Integer key = E.getkey (); Integer value = E.getvalue (); if (Key.equals (3) | | key.equals (4)) {It.remove ()}} 2. KeySet traversal method for (iterator<integer> it = Map.keyset (). Iterator (); It.hasnext ();) {Integer key = It.next (); if (Key.equals (5) | | key.equals (6)) {//Integer value = Map.get (key); It.remove ();}} 3.values iterates over value, which does not get a key, but is also a for (iterator<integer> it = map.values (). Iterator (); It.hasnext ();) {Integer value = It.next (); if (Value.equals (7) | | value.equals (8)) {//Integer value = Map.get (key); It.remove ();}} System.out.println ("Operatemap:" + map);}}

  

Java Collection Class Traversal Delete method test and use scene record

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.