Java tuple tuple usages (@ bupt-kobe) __java

Source: Internet
Author: User

Tuples and list lists, can be used for data storage, with multiple data, but unlike a list: a list can store only the same data type, unlike a tuple, it can store different data types, such as storing int, string, list, and so on, and can be extended indefinitely based on demand. A tuple (Messenger) is a set of objects that are packaged directly and stored in a single object. This container object allows you to read the elements, but it is not allowed to store new objects in them.

For example, in Web applications, often encountered a problem is the data paging problem, the query page needs to include several information: the current page size, page sizes; the query results return the data as: the current pages, but if you need information about the current page, page size, and total pages in the foreground, There must be another message: The total number of data records, and then based on the information above to calculate the total page size and other information. This is the time to query a page of information needs to return two data types, one is the list (the current data record), one is int (the total number of records). Of course, the two values can be obtained in two methods and two database connections. In fact, query list, has been through the SQL query to get the total number of records, if you open a method, and then do a database connection to query the total number of records, unavoidably a little superfluous, wasting time, wasting code, waste of life. Strongly worded ~ In this case, we can use the two-tuple, in a database connection, the total number of records, the current page record, and stored in it, simple and clear.

The code example looks like this:

Import java.util.*;

Import java.lang.*;
	Class Kobe {} class Jasonzhou {} class Twotuple<a, b> {public final String A;
	Public final Integer second;
		Public Twotuple (String A, Integer b) {A;
	second = b;
	Public String toString () {return second;
	} class Threetuple<a, B, kobe> extends Twotuple<a, b> {public final Kobe third;
		Public Threetuple (String A, Integer B, Jasonzhou c) {super (A,B);
	This.third = (Kobe) C;
	Public String toString () {return second + third; } public class Tupletest {public static twotuple<string, integer> F () {return new twotuple<string, Integ
	Er> ("Jaosnking", 24); public static threetuple<string, Integer, kobe> h () {return new threetuple<string, Integer, kobe> ("Kobeb
	Ryant ", New Jasonzhou ());
		public static void Main (string[] args) {//Twotuple TT = f ();
		System.out.println (f ());
		System.out.println ();
	System.out.println (H ());
 }
}


As shown in the code above, created two empty classes, a two-tuple, defined two-tuple and did not specify type,<a,b> used to specify the type of object you want to access in your actual operation, such as the definition in the scope of the two-tuple class and the generic type in the constructor. Also defines a ternary group inherited from the previously defined two-tuple class. Write a main to test the practicality of the above code. The output results are:

JaosnKing24 kobebryant24jasonzhou@15db9742

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.