Five things you don’t know about the Java collections API — turn

Part 1

http://www.ibm.com/developerworks/cn/java/j-5things2.html

For many Java developers, the Java collections API is a much-needed alternative to standard Java arrays and all their shortcomings. There is nothing wrong with associating collections mainly with ArrayList, but for those who have the spirit of exploration, this is only the tip of the iceberg of collections.

Although map (and its common implementation HashMap) is very suitable for name value pairs or key value pairs, there is no reason to limit yourself to these familiar tools. You can use the appropriate API or even the appropriate collection to correct a lot of error prone code.

This is the second article in the five things series and the first of seven articles devoted to collections. The reason why I spend so much time discussing collections is because these collections are so important in Java programming. First, I'll discuss the fastest (but perhaps not the most common) way to do everything, such as moving the content in array to list. Then we'll delve into something less known, such as writing a custom collections class and extending the Java collections API.

Developers who have just come into contact with Java technology may not know that the Java language initially included arrays to deal with the performance criticism of C + + developers in the early 1990s. We have come a long way since then, and arrays no longer have performance advantages over the Java collections library. For example, to dump the contents of an array to a string, you need to iterate over the entire array, and then connect the contents into a string; All collections implementations have an available toString () implementation. With a few exceptions, it is good practice to convert any array encountered into a collection as soon as possible. So the question arises, what is the easiest way to complete this transformation? It turns out that the Java collections API makes this conversion easy, as shown in Listing 1:

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
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>