Java-the actual application of the collection framework and arrays-assemble the JSON string

Source: Internet
Author: User
Tags set set

I've been working on the concatenation of JSON strings recently. The specific is: according to the interface development document, the database data query out, and then make up the interface document JSON form to the front end. All of them have been dealing with the collection framework recently, so now do a simple summary of the application.

First: List collection

1. Basics: The list collection is an ordered set, and the elements in the collection can be duplicated, and access to the elements in the collection can be accessed based on the index of the element. Arraylist,linkedlist is the implementation class for the list interface

A.arraylist: Array structure, features: Fast access, insert, delete slow.

1) If the initialization length is not specified when initializing the ArrayList, the default length is 10.

2) ArrayList when adding new elements, if the original capacity is exceeded, the ArrayList expansion ensurecapacity scheme is "raw capacity *3/2+1"

3) ArrayList is thread insecure and should not be used in multi-threaded situations.

4) ArrayList several ways to implement traversal: foreach (); Iterator ()

B.linkedlist: List structure, features: Insert, delete fast, query slow

2. Application scenario: The list collection is often used to store objects queried from the database, and the individual believes that the reason is that it is "orderly and repeatable" characteristics. ArrayList used to store data in data:[,,,] format

Second: Map collection

1. Basics: The Map collection holds elements of the key-value pair form, which can only be accessed based on the key of each element. Hashmap,linkedhashmap,treemap is its implementation class

2.HASHMAP: Features: It meets the requirements of storing associated data, and secondly it has fast access speed

In general, the most we use is HashMap, inserting, deleting and locating elements in the map, HashMap is the best choice. But if you want to traverse the key in natural order or in a custom order, TreeMap is better. If the order of the output needs to be the same as the input, then it can be implemented by LINKEDHASHMAP, and it can be arranged in the order of reading.

Third: Set Set

1. Basic knowledge: The elements in set are unordered and not duplicated. Common implementation classes have Hashset,linedhashset and treeset of these three.

2.HashSet: Query speed is fast, can not guarantee access order, but Linkedhashset can guarantee access order

IV: Arrays

arrays, which can be thought of as orderly and repeatable, are used in some special environments, for example, I have 24 hours of data a day, the data can be used in accordance with coordinates one by one, no data on the use of NULL instead.

Using the set frame above, as well as the characteristics of the array, you can make any form of JSON string.

Java-the actual application of the collection framework and arrays-assemble the JSON string

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.