This article is a mirror article of machine translation, please click here to jump to the original article.

View: 28269|Reply: 1

[Source] The use of tuples in Java

[Copy link]
Posted on 8/31/2021 11:22:41 AM | | | |
Tuples have a special meaning in the field of computers, this name may sound a little unfamiliar, and there are basically no application scenarios in writing code, however, surprisingly, tuples are closely related to programming, some students may not know that another academic name for "records" in relational databases is "tuples", a record is a tuple, a table is a relationship, records are composed of tables, tuples generate relationships, this is the core concept of relational databases.

Tuples are an inseparable part of a relational database, but they are not so indispensable in programming. Some programming languages have their own tuple syntax, such as python, F#, haskell, scala, etc., while others are more popular but do not have tuple syntax, such as Java, JavaScript, C++, C#, etc.

Tuples are not indispensable programming elements like arrays and objects, but their use can be very convenient for writing code, especially when a function needs to return multiple values. In this case, the common practice is to define an object, set the value that the function needs to return as the object's attribute, and then set the return value type of the function to the type of the object. Or you can have this function return a map data structure in which the specific data exists. However, both approaches have their flaws, and the first method is reliable, but the code is unusually bloated. The requirements themselves are very simple, as long as the function returns multiple values, but this method requires defining a type in advance, then instantiating it, then setting the instance properties, and finally returning, which is too inefficient. Although the second method is fast, but not secure enough, inside the function may know what kind of value is stored in the map, but outside the function, only know that the return value of this function is a map, as for what values are stored in the map, what type is not known, in multi-person development projects This disadvantage is especially obvious, sadly this practice is the preferred solution in some dynamically typed languages, which is also one of the fundamental reasons why dynamically typed languages are complained about for poor security and readability. Therefore, the best solution to this type of problem is to use tuples.

In languages where the syntax itself supports tuples, tuples are represented by parentheses, such as (int, bool, string) is a triple type, and its value can be (1, true, "abc"). It should be noted that each tuple type is unique, (int, bool), (bool, int), (string, double) are all binaries, but they are different tuples, if you take a certain tuple here as the return value of the function, although it is not as good as the first solution using custom types mentioned earlier in terms of readability and security, but it is much better than the second solution using map, at least using tuples can know how many values the function will return, What type of value are these values, and it has the advantage of being simple and fast to code a second scheme using map.

Fortunately, these programming languages now support generics, and the implementation of non-built-in tuples has become extremely simple, but after all, it is a non-language built-in syntax element, and it is definitely not as convenient to use as native tuples.

The following introduces a third-party Java tuples library, called Javatuples, with its own official homepage and hundreds of github stars, which almost plays a monopoly position in the field of Java tuples.

Javatuples define a tuple with a maximum length of 10, but I think the number of elements in a 10-tuple is already too much, and there is basically no readability. Tuple classes are defined as follows:

Unit<A> (1 element)

Pair<A,B> (2 elements)

Triplet<A,B,C> (3 elements)

Quartet<A,B,C,D> (4 elements)

Quintet<A,B,C,D,E> (5 elements)

Sextet<A,B,C,D,E,F> (6 elements)

Septet<A,B,C,D,E,F,G> (7 elements)

Octet<A,B,C,D,E,F,G,H> (8 elements)

Ennead<A,B,C,D,E,F,G,H,I> (9 elements)

Decade<A,B,C,D,E,F,G,H,I,J> (10 elements)

These archetypal classes are generic classes, so the letters in angle brackets can be replaced with arbitrary types. Below is an example of a binary code





By drawing inferences, the rest of the tuple types are used in the same way.

Because Java's tuples are not supported by the language itself, the code looks Javanese and less elegant. However, when writing code, in addition to the convenience of writing can improve efficiency, it is more important to pay more attention to the meaning expressed by the code itself, which is to use the meaning (function) of the tuple itself to enhance the code, and how to write it is actually only secondary.

Finally, it should be noted that the type in the generic angle brackets cannot be the basic type of Java, if you want to use the basic type, it must also be the basic type that has been boxed, such as the int type must be converted to Integer, and the bool type corresponds to the Boolean type.




Previous:POI CellStyle sets the cell background color
Next:.NET/C# MailKit Tutorial for Sending Mail [with Source Code]
 Landlord| Posted on 9/15/2021 10:55:08 AM |
You need to use maven to introduce dependencies like this:


javatuples allow us to create up to ten return values


Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com