{"id":306,"date":"2010-08-16T19:01:00","date_gmt":"2010-08-16T19:01:00","guid":{"rendered":"http:\/\/www.javacodegeeks.com\/2012\/10\/java-best-practices-vector-vs-arraylist-vs-hashset.html"},"modified":"2019-12-09T21:45:53","modified_gmt":"2019-12-09T19:45:53","slug":"java-best-practices-vector-arraylist","status":"publish","type":"post","link":"https:\/\/www.javacodegeeks.com\/2010\/08\/java-best-practices-vector-arraylist.html","title":{"rendered":"Java Best Practices \u2013 Vector vs ArrayList vs HashSet"},"content":{"rendered":"<div dir=\"ltr\" style=\"text-align: left;\">\n<p>Continuing our series of articles concerning proposed practices while working with the Java programming language, we are going to perform a performance comparison between the three probably most used <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/util\/Collection.html\">Collection<\/a> implementation classes. To make things more realistic we are going to test against a multi\u2013threading environment so as to discuss and demonstrate how to utilize <a href=\"http:\/\/download-llnw.oracle.com\/javase\/1.4.2\/docs\/api\/java\/util\/Vector.html\">Vector<\/a>, <a href=\"http:\/\/download-llnw.oracle.com\/javase\/1.4.2\/docs\/api\/java\/util\/ArrayList.html\">ArrayList<\/a> and\/or <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/util\/HashSet.html\">HashSet<\/a> for high performance applications.<\/p>\n<p>All discussed topics are based on use cases derived from the development of mission critical, ultra high performance production systems for the telecommunication industry.<\/p>\n<p>Prior reading each section of this article it is highly recommended that you consult the relevant Java API documentation for detailed information and code samples.<\/p>\n<p>All tests are performed against a Sony Vaio with the following characteristics :<\/p>\n<ul>\n<li>System : openSUSE 11.1 (x86_64)<\/li>\n<li>Processor (CPU) : Intel(R) Core(TM)2 Duo CPU T6670 @ 2.20GHz<\/li>\n<li>Processor Speed : 1,200.00 MHz<\/li>\n<li>Total memory (RAM) : 2.8 GB<\/li>\n<li>Java : OpenJDK 1.6.0_0 64-Bit<\/li>\n<\/ul>\n<p>The following test configuration is applied :<\/p>\n<ul>\n<li>Concurrent worker Threads : 50<\/li>\n<li>Test repeats per worker Thread : 100<\/li>\n<li>Overall test runs : 100<\/li>\n<\/ul>\n<p><span style=\"font-size: large;\"><strong>Vector vs ArrayList vs HashSet<\/strong><\/span><\/p>\n<p>One of the most common tasks a Java developer has to implement is storing and retrieving objects from <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/util\/Collection.html\">Collections<\/a>. The Java programming language provides a handful of <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/util\/Collection.html\">Collection<\/a> implementation classes with both overlapping and unique characteristics. <a href=\"http:\/\/download-llnw.oracle.com\/javase\/1.4.2\/docs\/api\/java\/util\/Vector.html\">Vector<\/a>, <a href=\"http:\/\/download-llnw.oracle.com\/javase\/1.4.2\/docs\/api\/java\/util\/ArrayList.html\">ArrayList<\/a> and <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/util\/HashSet.html\">HashSet<\/a> are probably the most frequently used.<\/p>\n<p>Nevertheless working with <a href=\"http:\/\/www.javacodegeeks.com\/2013\/02\/40-java-collections-interview-questions-and-answers.html\">Collection implementation classes<\/a>, especially in a multi\u2013threading environment, can by tricky. The majority of them does not provide synchronized access by default. As a consequence, altering the internal structure of a <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/util\/Collection.html\">Collection<\/a> (inserting or retracting elements) in a concurrent manner will certainly lead to errors.<\/p>\n<p>The case scenario that will be discussed here is having multiple <a href=\"http:\/\/download.oracle.com\/javase\/1.3\/docs\/api\/java\/lang\/Thread.html\">Threads<\/a> inserting, retracting and iterating through elements of every <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/util\/Collection.html\">Collection<\/a> implementation class mentioned above. We are going to demonstrate how to properly utilize the aforementioned collection implementation classes in a multi\u2013threading environment and provide relevant performance comparison charts so as to show which one performs better in every test case.<\/p>\n<p>To make a fare comparison we will assume that no NULL elements are allowed and that we do not mind the ordering of elements in the <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/util\/Collection.html\">Collections<\/a>. Furthermore since <a href=\"http:\/\/download-llnw.oracle.com\/javase\/1.4.2\/docs\/api\/java\/util\/Vector.html\">Vector<\/a> is the only <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/util\/Collection.html\">Collection<\/a> implementation class of our test group that provides synchronized access by default, synchronization for <a href=\"http:\/\/download-llnw.oracle.com\/javase\/1.4.2\/docs\/api\/java\/util\/ArrayList.html\">ArrayList<\/a> and <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/util\/HashSet.html\">HashSet<\/a> <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/util\/Collection.html\">Collection<\/a> implementation classes will be achieved using <i>Collections.synchronizedList<\/i> and <i>Collections.synchronizedSet<\/i> static methods. These methods provide a \u201cwrapped\u201d synchronized instance of a designated <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/util\/Collection.html\">Collection<\/a> implementation class as shown below :<\/p>\n<ul>\n<li>List syncList = Collections.synchronizedList(new ArrayList());<\/li>\n<li>Set syncSet = Collections.synchronizedSet(new HashSet());<\/li>\n<\/ul>\n<p><strong>Test case #1 \u2013 Adding elements in a collection<\/strong><\/p>\n<p>For the first test case we are going to have multiple <a href=\"http:\/\/download.oracle.com\/javase\/1.3\/docs\/api\/java\/lang\/Thread.html\">Threads<\/a> adding String elements in each <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/util\/Collection.html\">Collection<\/a> implementation class. In order to maintain uniqueness among String elements, we will construct them as shown below :<\/p>\n<ul>\n<li>A static first part e.g. \u201chelloWorld\u201d<\/li>\n<li>The worker Thread id, remember that we have 50 worker Threads running concurrently<\/li>\n<li>The worker Thread test repeat number, remember that each worker thread performs 100 test repeats for each test run<\/li>\n<\/ul>\n<p>For every test run each worker Thread will insert 100 String elements, as shown below :<\/p>\n<ul>\n<li>For the first test repeat\n<ul>\n<li>Worker Thread #1 will insert the String element : \u201chelloWorld-1-1\u201d<\/li>\n<li>Worker Thread #2 will insert the String element : \u201chelloWorld-2-1\u201d<\/li>\n<li>Worker Thread #3 will insert the String element : \u201chelloWorld-3-1\u201d etc &#8230;<\/li>\n<\/ul>\n<\/li>\n<li>For the second test repeat\n<ul>\n<li>Worker Thread #1 will insert the String element : \u201chelloWorld-1-2\u201d<\/li>\n<li>Worker Thread #2 will insert the String element : \u201chelloWorld-2-2\u201d<\/li>\n<li>Worker Thread #3 will insert the String element : \u201chelloWorld-3-2\u201d etc &#8230;<\/li>\n<\/ul>\n<\/li>\n<li>etc \u2026<\/li>\n<\/ul>\n<p>At the end of each test run every <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/util\/Collection.html\">Collection<\/a> implementation class will be populated with 5000 distinct String elements.<\/p>\n<p>Below we present a performance comparison chart between the three aforementioned <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/util\/Collection.html\">Collection<\/a> implementation classes<\/p>\n<div class=\"separator\" style=\"clear: both; text-align: center;\"><a href=\"http:\/\/1.bp.blogspot.com\/_tWwHCKnIbjs\/TGlYhz5TsjI\/AAAAAAAAABA\/2FQJCIJH3fc\/s1600\/chart1.png\"><img decoding=\"async\" src=\"http:\/\/1.bp.blogspot.com\/_tWwHCKnIbjs\/TGlYhz5TsjI\/AAAAAAAAABA\/2FQJCIJH3fc\/s400\/chart1.png\" alt=\"\" width=\"400\" height=\"208\" border=\"0\"><\/a><\/div>\n<p>The horizontal axis represents the number of test runs and the vertical axis the average transactions per second (TPS) for each test run. Thus higher values are better. As you can see <a href=\"http:\/\/download-llnw.oracle.com\/javase\/1.4.2\/docs\/api\/java\/util\/Vector.html\">Vector<\/a> and <a href=\"http:\/\/download-llnw.oracle.com\/javase\/1.4.2\/docs\/api\/java\/util\/ArrayList.html\">ArrayList<\/a> <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/util\/Collection.html\">Collection<\/a> implementation classes performed almost identically when adding elements to them. On the other hand the <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/util\/HashSet.html\">HashSet<\/a> <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/util\/Collection.html\">Collection<\/a> implementation class presented a slightly inferior performance mainly due to the more complex internal structure and the hash generation mechanism.<\/p>\n<p><span style=\"font-size: small;\"><strong>Test case #2 \u2013 Removing elements from a collection<\/strong><\/span><\/p>\n<p>For the second test case we are going to have multiple <a href=\"http:\/\/download.oracle.com\/javase\/1.3\/docs\/api\/java\/lang\/Thread.html\">Threads<\/a> removing String elements from each <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/util\/Collection.html\">Collection<\/a> implementation class. All collection implementation classes will be pre\u2013populated with the String elements from the previous test case. For removing elements we will be utilizing a shared <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/util\/Iterator.html\">Iterator<\/a> instance among all worker <a href=\"http:\/\/download.oracle.com\/javase\/1.3\/docs\/api\/java\/lang\/Thread.html\">Threads<\/a> for each <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/util\/Collection.html\">Collection<\/a> implementation class. Synchronized access to the <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/util\/Iterator.html\">Iterator<\/a> instance will also be implemented. Every worker <a href=\"http:\/\/download.oracle.com\/javase\/1.3\/docs\/api\/java\/lang\/Thread.html\">Thread<\/a> will be removing the next available element of the <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/util\/Collection.html\">Collection<\/a> implementation class, issuing the \u201cnext()\u201d and \u201cremove()\u201d <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/util\/Iterator.html\">Iterator<\/a> operations (to avoid <a href=\"https:\/\/examples.javacodegeeks.com\/java-basics\/exceptions\/java-util-concurrentmodificationexception-how-to-handle-concurrent-modification-exception\/\">ConcurrentModificationException<\/a>). Below is the performance comparison chart for the aforementioned test case.<div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<div class=\"separator\" style=\"clear: both; text-align: center;\"><a href=\"http:\/\/3.bp.blogspot.com\/_tWwHCKnIbjs\/TGlZRQAyzsI\/AAAAAAAAABE\/tXh9U4-nDLA\/s1600\/chart2.png\"><img decoding=\"async\" src=\"http:\/\/3.bp.blogspot.com\/_tWwHCKnIbjs\/TGlZRQAyzsI\/AAAAAAAAABE\/tXh9U4-nDLA\/s400\/chart2.png\" alt=\"\" width=\"400\" height=\"210\" border=\"0\"><\/a><\/div>\n<p>The horizontal axis represents the number of test runs and the vertical axis the average transactions per second (TPS) for each test run. Thus higher values are better. Again both <a href=\"http:\/\/download-llnw.oracle.com\/javase\/1.4.2\/docs\/api\/java\/util\/Vector.html\">Vector<\/a> and <a href=\"http:\/\/download-llnw.oracle.com\/javase\/1.4.2\/docs\/api\/java\/util\/ArrayList.html\">ArrayList<\/a> <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/util\/Collection.html\">Collection<\/a> implementation classes performed almost identically when removing String elements from them. On the other hand the <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/util\/HashSet.html\">HashSet<\/a> <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/util\/Collection.html\">Collection<\/a> implementation class outperformed <a href=\"http:\/\/download-llnw.oracle.com\/javase\/1.4.2\/docs\/api\/java\/util\/Vector.html\">Vector<\/a> and <a href=\"http:\/\/download-llnw.oracle.com\/javase\/1.4.2\/docs\/api\/java\/util\/ArrayList.html\">ArrayList<\/a> by far, resulting in 678000 TPS on average.<\/p>\n<p>At this point we must pinpoint that by using the \u201cremove(0)\u201d method of <a href=\"http:\/\/download-llnw.oracle.com\/javase\/1.4.2\/docs\/api\/java\/util\/Vector.html\">Vector<\/a> and <a href=\"http:\/\/download-llnw.oracle.com\/javase\/1.4.2\/docs\/api\/java\/util\/ArrayList.html\">ArrayList<\/a> <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/util\/Collection.html\">Collection<\/a> implementation classes to remove String elements, we have achieved slightly better performance results compared to utilizing a synchronized shared <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/util\/Iterator.html\">Iterator<\/a> instance. The reason that we have demonstrated the synchronized shared <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/util\/Iterator.html\">Iterator<\/a> instance \u201cnext()\u201d and \u201cremove()\u201d operations approach is to maintain a fare comparison between the three <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/util\/Collection.html\">Collection<\/a> implementation classes.<\/p>\n<p><strong>IMPORTANT NOTICE<\/strong><\/p>\n<p>Our test case scenario dictates that we remove the first element from each <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/util\/Collection.html\">Collection<\/a> implementation class. Nevertheless we must pinpoint that this applies as the worst case scenario for <a href=\"http:\/\/download-llnw.oracle.com\/javase\/1.4.2\/docs\/api\/java\/util\/Vector.html\">Vector<\/a> and <a href=\"http:\/\/download-llnw.oracle.com\/javase\/1.4.2\/docs\/api\/java\/util\/ArrayList.html\">ArrayList<\/a> <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/util\/Collection.html\">Collection<\/a> implementation classes. As one of our readers, James Watson, successfully commented on a relevant <a href=\"http:\/\/www.theserverside.com\/news\/thread.tss?thread_id=60728#338158\">post<\/a> from <a href=\"http:\/\/www.theserverside.com\/\">TheServerSide<\/a><\/p>\n<p>\u201c<i>The reason is that on ArrayList and Vecrtor, the remove() method will result in a System.arraycopy() call if any element except the last element is removed (the last element being the element with index: size \u2013 1). Removing the first element means the entire rest of the array is copied which is an O(n) operation. Since the test removes all the elements in the List, the full test becomes O(n^2) (slow.)<\/i><\/p>\n<p><i>HashSet remove does not do any such array copies so it&#8217;s remove is O(1) or constant time. For the full test it then is O(n) (fast.). If the tests were rewritten to remove the last element from the ArrayList and Vector, you would likely similar performance to the HashSet.<\/i>\u201d<\/p>\n<p>For that reason we will conduct this test again, removing the last element from <a href=\"http:\/\/download-llnw.oracle.com\/javase\/1.4.2\/docs\/api\/java\/util\/Vector.html\">Vector<\/a> and <a href=\"http:\/\/download-llnw.oracle.com\/javase\/1.4.2\/docs\/api\/java\/util\/ArrayList.html\">ArrayList<\/a> <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/util\/Collection.html\">Collection<\/a> implementation classes since we presume that the order of elements in the <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/util\/Collection.html\">Collections<\/a> is of no importance. The performance results are show below.<\/p>\n<div class=\"separator\" style=\"clear: both; text-align: center;\"><a href=\"http:\/\/1.bp.blogspot.com\/_tWwHCKnIbjs\/TG1apGegxRI\/AAAAAAAAABQ\/iR5YcLTxs9c\/s1600\/chart5.png\"><img decoding=\"async\" src=\"http:\/\/1.bp.blogspot.com\/_tWwHCKnIbjs\/TG1apGegxRI\/AAAAAAAAABQ\/iR5YcLTxs9c\/s400\/chart5.png\" alt=\"\" width=\"400\" height=\"188\" border=\"0\"><\/a><\/div>\n<p>The horizontal axis represents the number of test runs and the vertical axis the average transactions per second (TPS) for each test run. Thus higher values are better. As expected all <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/util\/Collection.html\">Collection<\/a> implementation classes performed almost identically when removing String elements from them.<\/p>\n<p><strong>Test case #3 \u2013 Iterators<\/strong><\/p>\n<p>For the third test case we are going to have multiple worker <a href=\"http:\/\/download.oracle.com\/javase\/1.3\/docs\/api\/java\/lang\/Thread.html\">Threads<\/a> iterating over the elements of each <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/util\/Collection.html\">Collection<\/a> implementation class. Every worker <a href=\"http:\/\/download.oracle.com\/javase\/1.3\/docs\/api\/java\/lang\/Thread.html\">Thread<\/a> will be using the <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/util\/Collection.html\">Collection<\/a> \u201citerator()\u201d operation to retrieve a reference to an <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/util\/Iterator.html\">Iterator<\/a> instance and iterate through all the available <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/util\/Collection.html\">Collection<\/a> elements using the <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/util\/Iterator.html\">Iterator<\/a> \u201cnext()\u201d operation. All <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/util\/Collection.html\">Collection<\/a> implementation classes will be pre\u2013populated with the String values from the first test case. Below is the performance comparison chart for the aforementioned test case.<\/p>\n<div class=\"separator\" style=\"clear: both; text-align: center;\"><a href=\"http:\/\/3.bp.blogspot.com\/_tWwHCKnIbjs\/TGlZyMDC_tI\/AAAAAAAAABI\/H_0DGBRmO1w\/s1600\/chart3.png\"><img decoding=\"async\" src=\"http:\/\/3.bp.blogspot.com\/_tWwHCKnIbjs\/TGlZyMDC_tI\/AAAAAAAAABI\/H_0DGBRmO1w\/s400\/chart3.png\" alt=\"\" width=\"400\" height=\"210\" border=\"0\"><\/a><\/div>\n<p>The horizontal axis represents the number of test runs and the vertical axis the average transactions per second (TPS) for each test run. Thus higher values are better. Both <a href=\"http:\/\/download-llnw.oracle.com\/javase\/1.4.2\/docs\/api\/java\/util\/Vector.html\">Vector<\/a> and <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/util\/HashSet.html\">HashSet<\/a> <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/util\/Collection.html\">Collection<\/a> implementation classes performed poorly compared to the <a href=\"http:\/\/download-llnw.oracle.com\/javase\/1.4.2\/docs\/api\/java\/util\/ArrayList.html\">ArrayList<\/a> <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/util\/Collection.html\">Collection<\/a> implementation class. <a href=\"http:\/\/download-llnw.oracle.com\/javase\/1.4.2\/docs\/api\/java\/util\/Vector.html\">Vector<\/a> scored 68 TPS on average, while <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/util\/HashSet.html\">HashSet<\/a> scored 9200 TPS on average. On the other hand the <a href=\"http:\/\/download-llnw.oracle.com\/javase\/1.4.2\/docs\/api\/java\/util\/ArrayList.html\">ArrayList<\/a> outperformed <a href=\"http:\/\/download-llnw.oracle.com\/javase\/1.4.2\/docs\/api\/java\/util\/Vector.html\">Vector<\/a> and <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/util\/HashSet.html\">HashSet<\/a> by far, resulting in 421000 TPS on average.<\/p>\n<p><strong>Test case #4 \u2013 Adding and removing elements<\/strong><\/p>\n<p>For our final test case we are going to implement the combination of test case #1 and test case #2 scenarios. One group of worker <a href=\"http:\/\/download.oracle.com\/javase\/1.3\/docs\/api\/java\/lang\/Thread.html\">Threads<\/a> is going to insert String elements to every <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/util\/Collection.html\">Collection<\/a> implementation class, whereas another group of worker <a href=\"http:\/\/download.oracle.com\/javase\/1.3\/docs\/api\/java\/lang\/Thread.html\">Threads<\/a> is going to retract the String elements from them.<\/p>\n<p>In the combined (adding and retracting elements) operation the synchronized shared <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/util\/Iterator.html\">Iterator<\/a> instance approach for removing elements cannot be used. Adding and removing elements concurrently from a single <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/util\/Collection.html\">Collection<\/a> eliminates the use of a shared <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/util\/Iterator.html\">Iterator<\/a> instance due to the fact that the internal structure of the <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/util\/Collection.html\">Collection<\/a> is constantly changing. For <a href=\"http:\/\/download-llnw.oracle.com\/javase\/1.4.2\/docs\/api\/java\/util\/Vector.html\">Vector<\/a> and <a href=\"http:\/\/download-llnw.oracle.com\/javase\/1.4.2\/docs\/api\/java\/util\/ArrayList.html\">ArrayList<\/a> <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/util\/Collection.html\">Collection<\/a> implementation classes the aforementioned limitation can be bypassed by using the \u201cremove(0)\u201d operation, which retracts the first element from a <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/util\/Collection.html\">Collection<\/a> internal storage. Unfortunately the <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/util\/HashSet.html\">HashSet<\/a> <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/util\/Collection.html\">Collection<\/a> implementation class does not provide such functionality. Our proposed way for achieving maximum performance results for the combined operation using the <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/util\/HashSet.html\">HashSet<\/a> <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/util\/Collection.html\">Collection<\/a> implementation class is the following :<\/p>\n<ul>\n<li>Use two distinct HashSets, one for adding elements and the other for retracting<\/li>\n<li>Implement a \u201ccontroller\u201d <a href=\"http:\/\/download.oracle.com\/javase\/1.3\/docs\/api\/java\/lang\/Thread.html\">Thread<\/a> that will swap the contents of the aforementioned <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/util\/HashSet.html\">HashSet<\/a> classes when the \u201cretracting\u201d <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/util\/HashSet.html\">HashSet<\/a> is empty. The \u201ccontroller\u201d <a href=\"http:\/\/download.oracle.com\/javase\/1.3\/docs\/api\/java\/lang\/Thread.html\">Thread<\/a> can be implemented as a regular <a href=\"http:\/\/download-llnw.oracle.com\/javase\/1.4.2\/docs\/api\/java\/util\/TimerTask.html\">TimerTask<\/a> that can check the contents of the \u201cretracting\u201d <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/util\/HashSet.html\">HashSet<\/a> at regular time intervals and perform the swap if needed<\/li>\n<li>A shared <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/util\/Iterator.html\">Iterator<\/a> instance for the \u201cretracting\u201d <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/util\/HashSet.html\">HashSet<\/a> should be created upon swapping the two HashSets<\/li>\n<li>All worker <a href=\"http:\/\/download.oracle.com\/javase\/1.3\/docs\/api\/java\/lang\/Thread.html\">Threads<\/a> that retract elements should wait for the \u201cretracting\u201d <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/util\/HashSet.html\">HashSet<\/a> to be filled with elements and be notified after the swap<\/li>\n<\/ul>\n<p>What follows is a code snippet displaying the proposed implementation :<\/p>\n<p>Global Declarations :<\/p>\n<pre class=\"brush: java\">Set&lt;string&gt; s = new HashSet&lt;string&gt;(); \/\/ The HashSet for retracting elements\nIterator&lt;string&gt; sIt = s.iterator(); \/\/ The shared Iterator for retracting elements\nSet&lt;string&gt; sAdd = new HashSet&lt;string&gt;(); \/\/ The HashSet for adding new elements\nBoolean read = Boolean.FALSE; \/\/ Helper Object for external synchronization and wait \u2013 notify functionality when retracting elements from the \u201cs\u201d HashSet\nBoolean write = Boolean.FALSE; \/\/ Helper Object for external synchronization when writing elements to the \u201csAdd\u201d HashSet\n<\/pre>\n<p>Code for adding elements to the \u201csAdd\u201d <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/util\/HashSet.html\">HashSet<\/a> :<\/p>\n<pre class=\"brush: java\">synchronized(write) {\n sAdd.add(\u201chelloWorld\u201d + \"-\" + threadId + \"-\" + count);\n}\n<\/pre>\n<p>Code for retracting elements from the \u201cs\u201d <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/util\/HashSet.html\">HashSet<\/a> :<\/p>\n<pre class=\"brush: java\">while (true) {\n synchronized (read) {\n  try {\n   sIt.next();\n   sIt.remove();\n   break;\n  } catch (NoSuchElementException e) {\n   read.wait();\n  }\n }\n}\n<\/pre>\n<p>The \u201ccontroller\u201d class code :<\/p>\n<pre class=\"brush: java\">public class Controller  extends TimerTask {\n \n public void run() {\n  try {\n   performSwap();\n  } catch (Exception ex) {\n   ex.printStackTrace();\n  }\n }\n  \n private void performSwap() throws Exception {\n  synchronized(read) {\n   if(s.isEmpty()) {\n    synchronized(write) {\n     if(!sAdd.isEmpty()) {\n      Set&lt;string&gt; tmpSet;\n      tmpSet = s;\n      s = sAdd;\n      sAdd = tmpSet;\n      sIt = s.iterator();\n      read.notifyAll();\n     }\n    }\n   }\n  }\n }\n\n}\n<\/pre>\n<p>Finally, the code to schedule the \u201ccontroller\u201d <a href=\"http:\/\/download-llnw.oracle.com\/javase\/1.4.2\/docs\/api\/java\/util\/TimerTask.html\">TimerTask<\/a><\/p>\n<pre class=\"brush: java\">Timer timer = new Timer();\ntimer.scheduleAtFixedRate(new Controller(), 0, 1);\n<\/pre>\n<p>We should start the \u201ccontroller\u201d task prior starting the worker <a href=\"http:\/\/download.oracle.com\/javase\/1.3\/docs\/api\/java\/lang\/Thread.html\">Threads<\/a> that write to and read from the relevant HashSets.<\/p>\n<p>Keep in mind that for the <a href=\"http:\/\/download-llnw.oracle.com\/javase\/1.4.2\/docs\/api\/java\/util\/Vector.html\">Vector<\/a> and <a href=\"http:\/\/download-llnw.oracle.com\/javase\/1.4.2\/docs\/api\/java\/util\/ArrayList.html\">ArrayList<\/a> <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/util\/Collection.html\">Collection<\/a> implementation classes we have used the \u201cremove(0)\u201d operation to retract elements. Below are the code snippets for the aforementioned <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/util\/Collection.html\">Collection<\/a> implementation classes :<\/p>\n<pre class=\"brush: java\">while (true) {\n try {\n  vector.remove(0);\n  break;\n } catch (ArrayIndexOutOfBoundsException e) {\n }\n}\n\nwhile (true) {\n try {\n  syncList.remove(0);\n  break;\n } catch (IndexOutOfBoundsException e) {\n }\n}\n<\/pre>\n<p>Below is the performance comparison chart for the addition part of the aforementioned test case.<\/p>\n<div class=\"separator\" style=\"clear: both; text-align: center;\"><a href=\"http:\/\/4.bp.blogspot.com\/_tWwHCKnIbjs\/TJpkgONZ_zI\/AAAAAAAAACY\/BO1PM3Cd6mg\/s1600\/chart1.png\"><img decoding=\"async\" src=\"http:\/\/4.bp.blogspot.com\/_tWwHCKnIbjs\/TJpkgONZ_zI\/AAAAAAAAACY\/BO1PM3Cd6mg\/s400\/chart1.png\" alt=\"\" width=\"400\" height=\"210\" border=\"0\"><\/a><\/div>\n<p>Following is the performance comparison chart for the retraction part of the aforementioned test case.<\/p>\n<div class=\"separator\" style=\"clear: both; text-align: center;\"><a href=\"http:\/\/2.bp.blogspot.com\/_tWwHCKnIbjs\/TJpkz3FG6rI\/AAAAAAAAACc\/O0jehQa7mnE\/s1600\/chart2.png\"><img decoding=\"async\" src=\"http:\/\/2.bp.blogspot.com\/_tWwHCKnIbjs\/TJpkz3FG6rI\/AAAAAAAAACc\/O0jehQa7mnE\/s400\/chart2.png\" alt=\"\" width=\"400\" height=\"210\" border=\"0\"><\/a><\/div>\n<div class=\"separator\" style=\"clear: both; text-align: center;\"><\/div>\n<p>The horizontal axis represents the number of test runs and the vertical axis the average transactions per second (TPS) for each test run. Thus higher values are better. Both <a href=\"http:\/\/download-llnw.oracle.com\/javase\/1.4.2\/docs\/api\/java\/util\/Vector.html\">Vector<\/a> and <a href=\"http:\/\/download-llnw.oracle.com\/javase\/1.4.2\/docs\/api\/java\/util\/ArrayList.html\">ArrayList<\/a> <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/util\/Collection.html\">Collection<\/a> implementation classes performed almost identically when adding and retracting elements from them. On the other hand for the element addition test case our proposed implementation, with the \u201cadding\u201d and \u201cretracting\u201d <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/util\/HashSet.html\">HashSet<\/a> pair, performed slightly inferiorly compared to <a href=\"http:\/\/download-llnw.oracle.com\/javase\/1.4.2\/docs\/api\/java\/util\/Vector.html\">Vector<\/a> and <a href=\"http:\/\/download-llnw.oracle.com\/javase\/1.4.2\/docs\/api\/java\/util\/ArrayList.html\">ArrayList<\/a> implementations. Nevertheless for the element retraction test case our \u201cadding\u201d and \u201cretracting\u201d <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/util\/HashSet.html\">HashSet<\/a> pair implementation outperformed both <a href=\"http:\/\/download-llnw.oracle.com\/javase\/1.4.2\/docs\/api\/java\/util\/Vector.html\">Vector<\/a> and <a href=\"http:\/\/download-llnw.oracle.com\/javase\/1.4.2\/docs\/api\/java\/util\/ArrayList.html\">ArrayList<\/a> implementations by far scoring 6000 TPS on average.<\/p>\n<p>Happy coding<\/p>\n<p>Justin<\/p>\n<div style=\"margin: 0px;\"><strong><i>Related Articles :<\/i><\/strong><\/div>\n<ul>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2010\/07\/java-best-practices-dateformat-in.html\">Java Best Practices \u2013 DateFormat in a Multithreading Environment<\/a><\/li>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2010\/07\/java-best-practices-high-performance.html\">Java Best Practices \u2013 High performance Serialization<\/a><\/li>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2010\/09\/string-performance-exact-string.html\">Java Best Practices \u2013 String performance and Exact String Matching<\/a><\/li>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2010\/09\/java-best-practices-queue-battle-and.html\">Java Best Practices \u2013 Queue battle and the Linked ConcurrentHashMap<\/a><\/li>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2010\/11\/java-best-practices-char-to-byte-and.html\">Java Best Practices \u2013 Char to Byte and Byte to Char conversions<\/a><\/li>\n<\/ul>\n<div style=\"margin: 0px;\"><strong><i>Related Snippets :<\/i><\/strong><\/div>\n<ul style=\"text-align: left;\">\n<li><a href=\"https:\/\/examples.javacodegeeks.com\/core-java\/util\/arraylist\/arraylist-size-example\">ArrayList size example<\/a><\/li>\n<li><a href=\"https:\/\/examples.javacodegeeks.com\/core-java\/util\/arraylist\/insert-all-elements-of-collection-to-specific-arraylist-index\">Insert all elements of Collection to specific ArrayList index<\/a><\/li>\n<li><a href=\"https:\/\/examples.javacodegeeks.com\/core-java\/util\/hashset\/check-for-element-existence-in-hashset-example\">Check for element existence in HashSet example<\/a><\/li>\n<li><a href=\"https:\/\/examples.javacodegeeks.com\/core-java\/util\/hashset\/hashset-iterator-example\">HashSet Iterator example<\/a><\/li>\n<li><a href=\"https:\/\/examples.javacodegeeks.com\/java-basics\/java-map-example\/\">Java Map Example<\/a><\/li>\n<li><a href=\"https:\/\/examples.javacodegeeks.com\/core-java\/util\/vector\/vector-enumeration-example\">Add element to specified index of Vector example<\/a><\/li>\n<\/ul>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Continuing our series of articles concerning proposed practices while working with the Java programming language, we are going to perform a performance comparison between the three probably most used Collection implementation classes. To make things more realistic we are going to test against a multi\u2013threading environment so as to discuss and demonstrate how to utilize &hellip;<\/p>\n","protected":false},"author":4,"featured_media":148,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[80,79,66,81],"class_list":["post-306","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-core-java","tag-arraylist","tag-hashset","tag-java-best-practices","tag-vector"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Java Best Practices \u2013 Vector vs ArrayList vs HashSet - Java Code Geeks<\/title>\n<meta name=\"description\" content=\"Continuing our series of articles concerning proposed practices while working with the Java programming language, we are going to perform a performance\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.javacodegeeks.com\/2010\/08\/java-best-practices-vector-arraylist.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Java Best Practices \u2013 Vector vs ArrayList vs HashSet - Java Code Geeks\" \/>\n<meta property=\"og:description\" content=\"Continuing our series of articles concerning proposed practices while working with the Java programming language, we are going to perform a performance\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.javacodegeeks.com\/2010\/08\/java-best-practices-vector-arraylist.html\" \/>\n<meta property=\"og:site_name\" content=\"Java Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/javacodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2010-08-16T19:01:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-12-09T19:45:53+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Byron Kiourtzoglou\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:site\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Byron Kiourtzoglou\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"10 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2010\\\/08\\\/java-best-practices-vector-arraylist.html#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2010\\\/08\\\/java-best-practices-vector-arraylist.html\"},\"author\":{\"name\":\"Byron Kiourtzoglou\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/9c0c8d27141b068173953202dd9aebeb\"},\"headline\":\"Java Best Practices \u2013 Vector vs ArrayList vs HashSet\",\"datePublished\":\"2010-08-16T19:01:00+00:00\",\"dateModified\":\"2019-12-09T19:45:53+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2010\\\/08\\\/java-best-practices-vector-arraylist.html\"},\"wordCount\":1923,\"commentCount\":6,\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2010\\\/08\\\/java-best-practices-vector-arraylist.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/java-logo.jpg\",\"keywords\":[\"ArrayList\",\"HashSet\",\"Java Best Practices\",\"Vector\"],\"articleSection\":[\"Core Java\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2010\\\/08\\\/java-best-practices-vector-arraylist.html#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2010\\\/08\\\/java-best-practices-vector-arraylist.html\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2010\\\/08\\\/java-best-practices-vector-arraylist.html\",\"name\":\"Java Best Practices \u2013 Vector vs ArrayList vs HashSet - Java Code Geeks\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2010\\\/08\\\/java-best-practices-vector-arraylist.html#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2010\\\/08\\\/java-best-practices-vector-arraylist.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/java-logo.jpg\",\"datePublished\":\"2010-08-16T19:01:00+00:00\",\"dateModified\":\"2019-12-09T19:45:53+00:00\",\"description\":\"Continuing our series of articles concerning proposed practices while working with the Java programming language, we are going to perform a performance\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2010\\\/08\\\/java-best-practices-vector-arraylist.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2010\\\/08\\\/java-best-practices-vector-arraylist.html\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2010\\\/08\\\/java-best-practices-vector-arraylist.html#primaryimage\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/java-logo.jpg\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/java-logo.jpg\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2010\\\/08\\\/java-best-practices-vector-arraylist.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/java\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Core Java\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/java\\\/core-java\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Java Best Practices \u2013 Vector vs ArrayList vs HashSet\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"name\":\"Java Code Geeks\",\"description\":\"Java Developers Resource Center\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"alternateName\":\"JCG\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.javacodegeeks.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/javacodegeeks\",\"https:\\\/\\\/x.com\\\/javacodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/9c0c8d27141b068173953202dd9aebeb\",\"name\":\"Byron Kiourtzoglou\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/45058c722c80f088c409be43e0f4be20dad81d55a7c9aefd75bab2e4253ae4e6?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/45058c722c80f088c409be43e0f4be20dad81d55a7c9aefd75bab2e4253ae4e6?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/45058c722c80f088c409be43e0f4be20dad81d55a7c9aefd75bab2e4253ae4e6?s=96&d=mm&r=g\",\"caption\":\"Byron Kiourtzoglou\"},\"description\":\"Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications\\\/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.\",\"sameAs\":[\"https:\\\/\\\/www.pivotalgamers.com\\\/\",\"https:\\\/\\\/www.linkedin.com\\\/in\\\/byron-kiourtzoglou-530ab222\"],\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/author\\\/byron-kiourtzoglou\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Java Best Practices \u2013 Vector vs ArrayList vs HashSet - Java Code Geeks","description":"Continuing our series of articles concerning proposed practices while working with the Java programming language, we are going to perform a performance","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.javacodegeeks.com\/2010\/08\/java-best-practices-vector-arraylist.html","og_locale":"en_US","og_type":"article","og_title":"Java Best Practices \u2013 Vector vs ArrayList vs HashSet - Java Code Geeks","og_description":"Continuing our series of articles concerning proposed practices while working with the Java programming language, we are going to perform a performance","og_url":"https:\/\/www.javacodegeeks.com\/2010\/08\/java-best-practices-vector-arraylist.html","og_site_name":"Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_published_time":"2010-08-16T19:01:00+00:00","article_modified_time":"2019-12-09T19:45:53+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg","type":"image\/jpeg"}],"author":"Byron Kiourtzoglou","twitter_card":"summary_large_image","twitter_creator":"@javacodegeeks","twitter_site":"@javacodegeeks","twitter_misc":{"Written by":"Byron Kiourtzoglou","Est. reading time":"10 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.javacodegeeks.com\/2010\/08\/java-best-practices-vector-arraylist.html#article","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/2010\/08\/java-best-practices-vector-arraylist.html"},"author":{"name":"Byron Kiourtzoglou","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/9c0c8d27141b068173953202dd9aebeb"},"headline":"Java Best Practices \u2013 Vector vs ArrayList vs HashSet","datePublished":"2010-08-16T19:01:00+00:00","dateModified":"2019-12-09T19:45:53+00:00","mainEntityOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2010\/08\/java-best-practices-vector-arraylist.html"},"wordCount":1923,"commentCount":6,"publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2010\/08\/java-best-practices-vector-arraylist.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg","keywords":["ArrayList","HashSet","Java Best Practices","Vector"],"articleSection":["Core Java"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.javacodegeeks.com\/2010\/08\/java-best-practices-vector-arraylist.html#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.javacodegeeks.com\/2010\/08\/java-best-practices-vector-arraylist.html","url":"https:\/\/www.javacodegeeks.com\/2010\/08\/java-best-practices-vector-arraylist.html","name":"Java Best Practices \u2013 Vector vs ArrayList vs HashSet - Java Code Geeks","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2010\/08\/java-best-practices-vector-arraylist.html#primaryimage"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2010\/08\/java-best-practices-vector-arraylist.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg","datePublished":"2010-08-16T19:01:00+00:00","dateModified":"2019-12-09T19:45:53+00:00","description":"Continuing our series of articles concerning proposed practices while working with the Java programming language, we are going to perform a performance","breadcrumb":{"@id":"https:\/\/www.javacodegeeks.com\/2010\/08\/java-best-practices-vector-arraylist.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.javacodegeeks.com\/2010\/08\/java-best-practices-vector-arraylist.html"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/2010\/08\/java-best-practices-vector-arraylist.html#primaryimage","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/www.javacodegeeks.com\/2010\/08\/java-best-practices-vector-arraylist.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.javacodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"Java","item":"https:\/\/www.javacodegeeks.com\/category\/java"},{"@type":"ListItem","position":3,"name":"Core Java","item":"https:\/\/www.javacodegeeks.com\/category\/java\/core-java"},{"@type":"ListItem","position":4,"name":"Java Best Practices \u2013 Vector vs ArrayList vs HashSet"}]},{"@type":"WebSite","@id":"https:\/\/www.javacodegeeks.com\/#website","url":"https:\/\/www.javacodegeeks.com\/","name":"Java Code Geeks","description":"Java Developers Resource Center","publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.javacodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.javacodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/www.javacodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/javacodegeeks","https:\/\/x.com\/javacodegeeks"]},{"@type":"Person","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/9c0c8d27141b068173953202dd9aebeb","name":"Byron Kiourtzoglou","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/45058c722c80f088c409be43e0f4be20dad81d55a7c9aefd75bab2e4253ae4e6?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/45058c722c80f088c409be43e0f4be20dad81d55a7c9aefd75bab2e4253ae4e6?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/45058c722c80f088c409be43e0f4be20dad81d55a7c9aefd75bab2e4253ae4e6?s=96&d=mm&r=g","caption":"Byron Kiourtzoglou"},"description":"Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications\/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.","sameAs":["https:\/\/www.pivotalgamers.com\/","https:\/\/www.linkedin.com\/in\/byron-kiourtzoglou-530ab222"],"url":"https:\/\/www.javacodegeeks.com\/author\/byron-kiourtzoglou"}]}},"_links":{"self":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/306","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/comments?post=306"}],"version-history":[{"count":0,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/306\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media\/148"}],"wp:attachment":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media?parent=306"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=306"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=306"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}