首頁 >Java >java教程 >Java 中的元組

Java 中的元組

王林
王林原創
2024-08-30 16:15:581104瀏覽

元組被視為有序的不同類型物件的集合。儘管這些物件可能彼此相關,也可能不相關,但它們共同具有特定的意義。在 Java 中,沒有元組支援的內建資料結構。因此,每當出現任何需求時都會建立一個類別。除此之外,此功能還可以在清單和陣列中使用。然而,不同資料類型的資料無法保留這一點。

文法

下面是元組的語法。

開始您的免費軟體開發課程

網頁開發、程式語言、軟體測試及其他

來自建構子的元組:

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、. 。 。 ,值 n
  • n 是參數的數量和值的數量

Java 中元組的特徵

以下是Java中元組的主要特徵

  • 模式安全
  • 可迭代
  • 實作 toString()
  • 可比較(Tuple 實作 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>
元組的大小 姓名 範例 一個元素 單位 單位 兩個元素 配對 配對 三個要素 三元組 三元組 四個元素 四重奏 四重奏 五個元素 五重奏 五重奏 六個元素 六重奏 六重奏 七個元素 七重奏 七重奏 八個元素 八位元組 八位元組 九個元素 九角 九角 十個元素 十年 十年 表>

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中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
上一篇:本地爪哇下一篇:本地爪哇