5 things you don't know about the Java Collections API, part 1th

Source: Internet
Author: User
Tags arrays

customizing and extending Java Collections

For many Java developers, the Java collections API is a very needed alternative to standard Java arrays and all their drawbacks. It is not wrong to associate collections mainly with ArrayList, but for those who have the spirit of exploration, this is just the tip of the iceberg of collections.

Although the MAP (and its common implementation HashMap) is well-suited for name-value pairs or key-value pairs, there is no reason to confine yourself to these familiar tools. You can use the appropriate APIs, or even the appropriate Collection, to correct many error-prone code.

This is the second article in the 5 series, and the first article in the 7 articles devoted to collections, which has taken so much space to discuss collections because these collections are so important in Java programming. First I'll discuss the quickest (but perhaps not most common) way to do everything, such as moving the contents of an Array to list. Then we delve into something less people know, such as writing custom collections classes and extending the Java collections API.

1. Collections is better than array

Developers who have just contacted Java technology may not know that the Java language initially included arrays in response to the performance criticism of C + + developers in the early 90. Since then, we've come a long way, and now, compared to the Java collections Library, arrays no longer have a performance advantage.

For example, to dump the contents of an array to a string, you need to iterate over the entire array and then concatenate the content into a string, and the collections implementation has an available toString () implementation.

Except for a few cases, it is good practice to convert any array you encounter as soon as possible into a collection. So the question is, what is the easiest way to accomplish this transformation? It turns out that the Java collections API makes this transition easy, as shown in Listing 1:

Listing 1. Arraytolist

import java.util.*;

public class ArrayToList
{
   public static void main(String[] args)
   {
     // This gives us nothing good
     System.out.println(args);

     // Convert args to a List of String
     List<String> argList = Arrays.asList(args);

     // Print them out
     System.out.println(argList);
   }
}

Note that the returned List is not modifiable, so if you try to add a new element to it, it throws a unsupportedoperationexception.

Also, because Arrays.aslist () uses the varargs parameter to represent the elements added to the list, you can use it to easily create a list for new objects newly created.

2. Low efficiency of iterations

It is not uncommon to move the contents of a collection (especially a collection converted from an array) to another collection, or to remove a collection of smaller objects from a collection of large objects.

You may want to iterate over the collection and then add elements or remove the found elements, but do not do so.

In this case, the iterations have great drawbacks:

It is very inefficient to readjust the collection each time you add or remove elements.

Each time a lock is acquired, an operation is performed, and a lock is released, there is a potential concurrency dilemma.

When elements are added or removed, other threads that access the collection can cause competition conditions.

You can avoid all of these problems by using AddAll or RemoveAll to pass in the collection that contains the elements that you want to add or remove.

Related Article

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.