Java () ----- Java Set details (1): Specify the initial capacity for the set, java ----- java

Source: Internet
Author: User

Java () ----- Java Set details (1): Specify the initial capacity for the set, java ----- java

Collections are widely used in Java programming. They are like the sea, the sea, and the sea, the universal container can also be infinitely larger (if conditions permit ). When the volume of this sea and container changes to a very large value, its initial capacity will become very important, because mining and resizing require a lot of human and material resources. In the same way, the initial capacity of Collection is also very important. Therefore, for known scenarios, specify the initial capacity for the set.

public static void main(String[] args) {        StudentVO student = null;        long begin1 = System.currentTimeMillis();        List<StudentVO> list1 = new ArrayList<>();        for(int i = 0 ; i < 1000000; i++){            student = new StudentVO(i,"chenssy_"+i,i);            list1.add(student);        }        long end1 = System.currentTimeMillis();        System.out.println("list1 time:" + (end1 - begin1));                long begin2 = System.currentTimeMillis();        List<StudentVO> list2 = new ArrayList<>(1000000);        for(int i = 0 ; i < 1000000; i++){            student = new StudentVO(i,"chenssy_"+i,i);            list2.add(student);        }        long end2 = System.currentTimeMillis();        System.out.println("list2 time:" + (end2 - begin2));    }

In the above Code, both lists insert 1000000 data records, but list1 does not apply for initialization capacity, while list2 initializes a capacity of 1000000. The running result is as follows:

list1 time:1638list2 time:921

From the preceding running results, we can see that the speed of list2 is about twice that of list1. As mentioned above, the ArrayList Expansion Mechanism consumes resources. Let's first look at the add method of ArrayList:

Public boolean add (E e) {ensureCapacity (size + 1); elementData [size ++] = e; return true;} public void ensureCapacity (int minCapacity) {modCount ++; // modify the counter int oldCapacity = elementData. length; // if (minCapacity> oldCapacity) {Object oldData [] = elementData; // new capacity = old capacity * 1.5 + 1 int newCapacity = (oldCapacity * 3)/2 + 1; if (newCapacity <minCapacity) newCapacity = minCapacity; // copy an array, generate a new array elementData = Arrays. copyOf (elementData, newCapacity );}}

Each time an element is added to the ArrayList, the system checks whether the current capacity of the ArrayList has reached the critical point. If it reaches the critical point, the system expands by 1.5 times. However, the expansion of ArrayList and the generation of new arrays by copying arrays are resource-consuming. Therefore, if we know the use scenario of the set in advance and the approximate range of the set, we 'd better specify the initialization capacity to make better use of resources, especially on the premise of large data volumes, efficiency Improvement and resource utilization will become more advantageous.

>>>>>> Java Set Detail 1: Specify the initial capacity for the set.


----- Original from: http://cmsblogs.com /? P = 1233. Please respect the author's hard work and repost the source.

----- Personal site: http://cmsblogs.com



The java Collection class javautilArrayList is used. Which of the following statements is true ()

D

A. The elements in this set are disordered and ordered.
B. Element Errors in the set can be obtained through keys. They cannot be obtained through keys, and get (int index) can be obtained through specified positions.
C. The addFirst method can be used to insert element errors in the first part of the list. The addFirst method is not available.

Java: how to Add () in the Set; find () search; sort () sorting; method; Note: You can implement similar functions using java code.

If you score 0, I will give you some advice.
A class has an array. The default length is 10.
When adding an array, you will assign a value to the last element not empty in the array. If it is assigned to the last element, create an array and add it to the end of the array, add ();
Find (); just loop
Sort (); the new array can also be obtained cyclically.

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.