Java tuple usage instance — reprint
Original address: http://50vip.com/35.html
@H_ 403_ 3 @ I. why use tuples tuple@H_403_3 @?
Tuples, like lists, can be used for data storage and contain multiple data; However, unlike the list, the list can only store the same data type, but the tuple is different. It can store different data types, such as int, string, list, etc., and can be expanded infinitely according to needs.
For example, in web applications, we often encounter the problem of data paging. Query paging needs to contain several information: current page number and page size; The data returned from the query result is the data record of the current page. However, if you need to display the current page, page size, total pages and other information in the foreground, there must be another information: the total number of data records, and then calculate the total pages and other information according to the above information. At this time, two data types need to be returned when querying the information of a page, One is list (the current data record), and the other is int (total records). Of course, these two values can be obtained in two methods and two database connections. In fact, when querying the list, the total records have been obtained through SQL query. If you open another method and make another database connection to query the total records, it will be a bit superfluous, waste time, waste code and waste life. I'm serious ~ here In this case, we can use two tuples to get the total records and current page records in a database connection and store them in it. It's simple and clear!
@H_ 403_ 3 @ II. Source code examples
Binary: