Javaのタプル

王林
リリース: 2024-08-30 16:15:58
オリジナル
983 人が閲覧しました

タプルは、順序付けられたさまざまなタイプのオブジェクトのコレクションと見なされます。オブジェクトは互いに関連している場合とない場合でも、集合的に特定の意味を持ちます。 Java には、タプルによってサポートされる組み込みのデータ構造はありません。したがって、何らかの要件が発生するたびにクラスが作成されます。それに加えて、この機能は配列だけでなくリストでも使用できます。ただし、異なるデータ型のデータはこれを保持できません。

構文

以下はタプルの構文です。

無料ソフトウェア開発コースを始めましょう

Web 開発、プログラミング言語、ソフトウェア テスト、その他

コンストラクターからのタプル:

Nthtuple<t1,t2,. .  tn> nthtuple= new Nthtuple<t1,t2,. . . . . . . tn>(v1, v2, . . . . , vn)
ログイン後にコピー

with() メソッドからのタプル:

Nthtuple<t1,t2,. .  tn> nthtuple= new Nthtuple.with(v1, v2, . . . . , vn)
ログイン後にコピー

他のコレクションからのタプル:

Nthtuple<t1,t2,.  tn> nthtuple=new Nthtuple.fromCollection(collectionWith nvalues);
ログイン後にコピー

こちら

  • t1、t2、…tn はタイプ 1、タイプ 2、…、タイプ n です
  • v1、v2、. 。 .、vn は値 1、値 2、...、vn です。 。 。 、値 n
  • n はパラメータの数と値の数です

Java のタプルの特徴

Java のタプルの主な特徴は次のとおりです

  • タイプセーフ
  • 反復可能
  • toString() を実装
  • Comparable (タプルは Comparable を実装します)
  • 不変
  • シリアル化可能
  • equals() メソッドと hashCode() を実装します。

Java ではタプルはどのように機能しますか?

タプルの例を見てみましょう。

["Anna", "Computer Science", 23]
ログイン後にコピー

ここでは、このタプルの各オブジェクトが異なるデータ型であることがわかります。しかし、総合的に考えると、それは 23 歳のコンピュータ サイエンス学科の学生アンナの詳細であると特定できます。

注: Java のタプルは最大 10 のサイズをサポートしており、以下に示すように、タプル サイズごとに特定の実装方法があります。
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>
タプルのサイズ 名前 サンプル 1 つの要素 単位 ユニット<1> 2 つの要素 ペア ペア 3 つの要素 三連符 三連符 4 つの要素 カルテット カルテット 5 つの要素 クインテット クインテット 6 つの要素 六重奏団 六重奏曲 7 つの要素 セプテット セプテット 8 つの要素 オクテット オクテット 9 つの要素 エニード エンニード 10 の要素 ディケイド 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);
}
}
ログイン後にコピー

Output:

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);
}
}
ログイン後にコピー

Output:

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);
}
}
ログイン後にコピー

Output:

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);
}
}
ログイン後にコピー

Output:

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);
}
}
ログイン後にコピー

Output:

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);
}
}
ログイン後にコピー

Output:

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.

以上がJavaのタプルの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

関連ラベル:
ソース:php
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!