How to choose the right container and its real

xiaoxiao2021-03-06  82

Selection between various lists: lists include: arraylist and linkedListarrayList: Take Array as the underlying; LinkedList: Done in a general bidirectional list, each object except for the data itself, there are two refrescence points to the forward element and The latter element.

---------- Example ------------------ Import java.util. *; Import com.bruceeckel.util. *; Public class ListPerformance {private abstract static class Tester {String name; int size; // Test quantity Tester (String name, int size) {this.name = name; this.size = size;} abstract void test (List a, int reps) } Private static test [] tests = {New Tester ("get", 300) {void test (List a, int REPS) {for (int i = 0; I

System.currenttimemillis (); tests [i] .test (A, REPS); long t2 = system.currenttimemillis (); system.out.println (":" (t2 - t1));}} public static void testArray (int REPS) {System.Out.println ("Testing Array As List"); // Can Only Do First Two Tests on An Array: for (INT I = 0; I <2; i ) {String [] sa = NEW STRING [TESTS [I] .size]; Arrays2.Fill (sa, collections2.countries.reset ()); list a = arrays.aslist (sa); system.out.print (tests [i] .name); Long t1 = system.currenttimemillis (); tests [i] .test (a, reps); long t2 = system.currenttimemillis (); system.out.println (":" (t2 - t1));}} PUBLIC Static void main (string [] args) {int REPS = 50000; // OR, Choose the number of reviews // via the command line: if (args.length> 0) REPS = INTEGER.PARSEINT (Args [0]) System.out.println (REPS "Repetitions"); TestArray (Reps); Test (New ArrayList (), Test (New LinkedList (), REPS); TEST (New Vector (), REPS);}} ///: ~ ------------------------------- ------ operation results are as follows: Container type Don't achieve iterative release Removal Array 1430 3850 NA NA ARRAYLIST 3070 12200 500 46850LINKEDLIST 16320 9110 110 60Vector 4890 16250 550 46850 ------ {transfer from " Think in java "} -------------- From the above results, the advantage of LinkedList is that the insertion and removal, it is more fast than ArrayList, so you can To make an installation and removal operation at the LIST Center, use LinkedList, otherwise use ArrayList. Because the ARRAYLIST is still very advantageous if the data is taken, if you are processed, use Array. Vector is often compatible with old procedures, there is no use outside of this. You should try to avoid using him.

转载请注明原文地址:https://www.9cbs.com/read-121414.html

New Post(0)