Maison > Java > javaDidacticiel > le corps du texte

Tuples en Java

王林
Libérer: 2024-08-30 16:15:58
original
984 Les gens l'ont consulté

Un tuple est considéré comme une collection de différents types d'objets qui sont commandés. Même si les objets peuvent ou non être liés les uns aux autres, ils ont collectivement une signification spécifique. En Java, il n'existe pas de structure de données intégrée prise en charge par les tuples. Ainsi, une classe sera créée chaque fois qu’une exigence se présentera. En plus de cela, cette fonctionnalité peut être utilisée dans les listes ainsi que dans les tableaux. Cependant, les données de différents types de données ne peuvent pas conserver cela.

Syntaxe

Vous trouverez ci-dessous la syntaxe des Tuples.

Commencez votre cours de développement de logiciels libres

Développement Web, langages de programmation, tests de logiciels et autres

Tuple du constructeur :

Nthtuple<t1,t2,. .  tn> nthtuple= new Nthtuple<t1,t2,. . . . . . . tn>(v1, v2, . . . . , vn)
Copier après la connexion

Tuple de la méthode with() :

Nthtuple<t1,t2,. .  tn> nthtuple= new Nthtuple.with(v1, v2, . . . . , vn)
Copier après la connexion

Tuple d'autres collections :

Nthtuple<t1,t2,.  tn> nthtuple=new Nthtuple.fromCollection(collectionWith nvalues);
Copier après la connexion

Ici,

  • t1, t2, … tn sont de type 1, type 2,… ., tapez n
  • v1, v2, . . ., vn sont la valeur 1, la valeur 2, . . . , valeur n
  • n est le nombre de paramètres et le nombre de valeurs

Caractéristiques des tuples en Java

Voici les principales caractéristiques des tuples en Java

  • Typesafe
  • Itérable
  • Implémenter toString()
  • Comparable (Tuple implémente Comparable)
  • Immuable
  • Sérialisable
  • Implémentez la méthode equals() et hashCode().

Comment fonctionnent les tuples en Java ?

Prenons un exemple du tuple.

["Anna", "Computer Science", 23]
Copier après la connexion

Ici, vous pouvez voir que chaque objet de ce tuple est de types de données différents. Mais, si nous y réfléchissons collectivement, il s'agit d'un détail d'Anna, étudiante au département d'informatique, âgée de 23 ans.

Remarque : Les tuples en Java prennent en charge des tailles allant jusqu'à 10, et il existe une manière particulière d'implémentation pour chaque taille de tuple, comme indiqué ci-dessous.
Size of Tuple Name Sample
One Element Unit Unit<1>
Two Elements Pair Pair<1,2>
Three Elements Triplet Triplet<1,2,3>
Four Elements Quartet Quartet<1,2,3,4>
Five Elements Quintet Quintet<1,2,3,4,5>
Six Elements Sextet Sextet<1,2,3,4,5,6>
Seven Elements Septet Septet<1,2,3,4,5,6,7>
Eight Elements Octet Octet<1,2,3,4,5,6,7,8>
Nine Elements Ennead Ennead<1,2,3,4,5,6,7,8,9>
Ten Elements Decade Decade<1,2,3,4,5,6,7,8,9,10>
Taille du tuple Nom Échantillon Un élément Unité Unité<1> Deux éléments Paire Paire<1,2> Trois éléments Triplet Triplet<1,2,3> Quatre éléments Quatuor Quatuor<1,2,3,4> Cinq éléments Quintette Quintette<1,2,3,4,5> Six éléments Sextuor Sextet<1,2,3,4,5,6> Sept éléments Septet Septet<1,2,3,4,5,6,7> Huit éléments Octet Octet<1,2,3,4,5,6,7,8> Neuf éléments Ennéad Ennéad<1,2,3,4,5,6,7,8,9> Dix éléments Décennie Décennie<1,2,3,4,5,6,7,8,9,10>

Examples to Implement Tuples in Java

Below are the examples mentioned:

Now, let us see some practical examples of tuples.

Example #1

Java program to create a pair tuple of string type

Code:

import org.javatuples.Pair;
public class TupExample {
public static void main(String[] args) {
//create a pair tuple from constructor
Pair<String,String>pobj = new Pair<String,String>("Happy", "Sad");
//print the tuples
System.out.println("Emotions are: " + pobj);
}
}
Copier après la connexion

Output:

Tuples en Java

Explanation: In this program, a pair tuple is created of string type. For that, the package org.javatuples.Pair has to be imported first. Once it is created, the objects of the tuple can be printed.

Example #2

Java program to create a pair tuple of different data types

Code:

import org.javatuples.Pair;
public class TupExample {
public static void main(String[] args) {
//create a pair tuple from constructor
Pair<String,Integer>pobj = new Pair<String,Integer>("Anna", 23);
//print the tuples
System.out.println("Student is: " + pobj);
}
}
Copier après la connexion

Output:

Tuples en Java

Explanation: In this program, a pair tuple is created of two different data types, string and integer. Here also, the package org.javatuples.Pair has to be imported first. Once it is created, the objects of the tuple can be printed.

Example #3

Java program to print a pair tuple using with () method.

Code:

import org.javatuples.Pair;
public class TupExample {
public static void main(String[] args) {
//create a pair tuple from constructor
Pair<String,Integer>pobj = Pair.with("Anna", 23);
//print the tuples
System.out.println("Student is: " + pobj);
}
}
Copier après la connexion

Output:

Tuples en Java

Explanation: Unlike the above programs, a pair tuple is created using with() method in this program. Here also, the package org.javatuples.Pair has to be imported first. Once it is created, the objects of the tuple can be printed.

Example #4

Java program to create an octet tuple of integer type

Code:

import org.javatuples.Octet;
public class TupExample {
public static void main(String[] args) {
//create an octet tuple from constructor
Octet<Integer,Integer,Integer,Integer,Integer,Integer,Integer,Integer>pobj = Octet.with(12, 23, 34, 45, 56, 67, 78, 89);
//print the tuples
System.out.println("Numbers are: " + pobj);
}
}
Copier après la connexion

Output:

Tuples en Java

Explanation: In this program, an octet tuple is created using with () method. Here, the package org.javatuples.Octet has to be imported first. Once it is created, the objects of the tuple can be printed.

Example #5

Java program to create decade tuple of integer type using fromCollection()

Code:

import java.util.ArrayList;
import java.util.List;
import org.javatuples.Decade;
public class TupExample {
public static void main(String[] args) {
// Create a li of 10 elements
List<Integer>li = new ArrayList<Integer>();
li.add(12);
li.add(23);
li.add(34);
li.add(45);
li.add(56);
li.add(67);
li.add(78);
li.add(89);
li.add(90);
li.add(101);
Decade<Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer>dobj
= Decade.fromCollection(li);
Integer[] a = { 12, 23, 34, 45, 56, 67, 78, 89, 90, 101 };
Decade<Integer, Integer, Integer, Integer, Integer, Integer, Integer,
Integer, Integer, Integer>dnew = Decade.fromArray(a);
System.out.println("Numbers are: " +dobj);
System.out.println("Numbers are: " +dnew);
}
}
Copier après la connexion

Output:

Tuples en Java

Explanation: In this program, a decade tuple is created using fromCollection () method. For that, first, a list has to be created with 10 elements. These 10 elements will later gets printed using the fromCollection() method. Here, the package org.javatuples. A decade has to be imported first. Only after importing this, the objects of the tuple can be printed.

Example #6

Java program to create an octet tuple using with() and fromCollection() method.

Code:

import java.util.ArrayList;
import java.util.List;
import org.javatuples.Octet;
public class TupExample {
public static void main(String[] args) {
//create an octet tuple from constructor
Octet<Integer,Integer,Integer,Integer,Integer,Integer,Integer,Integer>pobj = Octet.with(12, 23, 34, 45, 56, 67, 78, 89);
//print the tuples
System.out.println("Numbers using with() method are: " + pobj);
// Create a list of 8 elements
List<Integer>li = new ArrayList<Integer>();
li.add(12);
li.add(23);
li.add(34);
li.add(45);
li.add(56);
li.add(67);
li.add(78);
li.add(89);
Octet<Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer>dobj
= Octet.fromCollection(li);
System.out.println("Numbers using the fromCollection() method are: " +dobj);
}
}
Copier après la connexion

Output:

Tuples en Java

Explanation: In this program, an octet tuple is created using fromCollection () and with() method.

Conclusion

A tuple is considered as a collection of different type of ordered objects. In this article, a detailed explanation that contains the syntax, characteristics, working, and examples of Java Tuples is addressed.

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Étiquettes associées:
source:php
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!