5 things you don't know about the Java Collections API, part 2nd

Source: Internet
Author: User

Note Variable objects

The collections class in Java.util is designed to improve Java performance by substituting arrays. As you can see in part 1th, they are also changeable, customizable and scalable in a variety of ways to help achieve quality, concise code.

Collections are very powerful, but many change: use them carefully, and misuse them poses risks.

1. List is different from array

Java developers often mistakenly assume that ArrayList is a substitute for Java arrays. Collections is supported by arrays and performs better when searching for content randomly within a collection. As with arrays, a collection uses an integer ordinal to get a specific item. But the collection is not a simple substitution of arrays.

To understand the difference between an array and a set, you need to figure out the difference between order and position. For example, the list is an interface that holds the order in which items are put into the collection, as shown in Listing 1:

Listing 1. Variable key value

import java.util.*;

public class Orderandposition
{
public static <T> void Dumparray (t[] array)
{
System . OUT.PRINTLN ("=============");   
for (int i=0; i<array.length; i++)
System.out.println ("Position" + i + ":" + array[i]);

public static <T> void Dumplist (list<t> List)
{
System.out.println ("=============");    br> for (int i=0; i<list.size (); i++)
System.out.println ("Ordinal" + i + ":" + list.get (i));


public static void Main (string[] args)
{
List<string> argList = new Arraylist<strin G> (Arrays.aslist (args));     

Dumparray (args);
ARGS[1] = null;
Dumparray (args);     

Dumplist (argList);
Arglist.remove (1);
Dumplist (argList);
}
}

When the third element is removed from the list above, its "back" items are raised to fill the vacancy. Obviously, this collection behavior differs from the behavior of the array (in fact, removing an item from an array and removing it from the list is not entirely the same thing-removing an item from an array means overwriting its index slot with a new reference or null).

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.