Rumah > Java > javaTutorial > teks badan

Tuple di Jawa

王林
Lepaskan: 2024-08-30 16:15:58
asal
1069 orang telah melayarinya

Tuple dianggap sebagai koleksi pelbagai jenis objek yang dipesan. Walaupun objek itu mungkin berkaitan atau mungkin tidak satu sama lain, ia secara kolektif mempunyai makna tertentu. Di Jawa, tiada struktur data terbina yang disokong oleh tupel. Jadi, kelas akan dibuat apabila sebarang keperluan berlaku. Di samping itu, fungsi ini boleh digunakan dalam Senarai dan juga tatasusunan. Walau bagaimanapun, data jenis data yang berbeza tidak dapat mengekalkannya.

Sintaks

Di bawah ialah sintaks Tuples.

Mulakan Kursus Pembangunan Perisian Percuma Anda

Pembangunan web, bahasa pengaturcaraan, ujian perisian & lain-lain

Tuple daripada pembina:

Nthtuple<t1,t2,. .  tn> nthtuple= new Nthtuple<t1,t2,. . . . . . . tn>(v1, v2, . . . . , vn)
Salin selepas log masuk

Kaedah Tuple from with():

Nthtuple<t1,t2,. .  tn> nthtuple= new Nthtuple.with(v1, v2, . . . . , vn)
Salin selepas log masuk

Tuple daripada koleksi lain:

Nthtuple<t1,t2,.  tn> nthtuple=new Nthtuple.fromCollection(collectionWith nvalues);
Salin selepas log masuk

Di sini,

  • t1, t2, … tn ialah jenis 1, jenis 2,… ., jenis n
  • v1, v2, . . ., vn ialah nilai 1, nilai 2, . . . , nilai n
  • n ialah bilangan parameter dan bilangan nilai

Ciri-ciri Tuples di Jawa

Berikut ialah ciri utama Tuple di Jawa

  • Typesafe
  • Boleh diulang
  • Laksanakan toString()
  • Setanding (Tuple melaksanakan Sebanding)
  • Kekal
  • Boleh bersiri
  • Laksanakan kaedah equals() dan hashCode().

Bagaimanakah Tuples berfungsi di Jawa?

Mari kita ambil contoh tupel.

["Anna", "Computer Science", 23]
Salin selepas log masuk

Di sini, anda dapat melihat bahawa setiap objek tuple ini adalah daripada jenis data yang berbeza. Tetapi, apabila kita pertimbangkan secara kolektif, ia boleh dikenal pasti sebagai butiran seorang pelajar Anna di jabatan Sains Komputer yang berusia 23 tahun.

Nota: Tuples dalam Java menyokong saiz sehingga 10, dan terdapat cara pelaksanaan tertentu untuk setiap saiz tuple, seperti yang ditunjukkan di bawah.
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>
Saiz Tuple Nama Sampel Satu Elemen Unit Unit<1> Dua Elemen Pasangan Pasangan<1,2> Tiga Elemen Triplet Triplet<1,2,3> Empat Elemen Kuartet Kuartet<1,2,3,4> Lima Elemen Kuintet Kuintet<1,2,3,4,5> Enam Elemen Sextet Sextet<1,2,3,4,5,6> Tujuh Elemen Septet Septet<1,2,3,4,5,6,7> Lapan Elemen Oktet Oktet<1,2,3,4,5,6,7,8> Sembilan Elemen Ennead Ennead<1,2,3,4,5,6,7,8,9> Sepuluh Elemen Dekad Dekad<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);
}
}
Salin selepas log masuk

Output:

Tuple di Jawa

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);
}
}
Salin selepas log masuk

Output:

Tuple di Jawa

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);
}
}
Salin selepas log masuk

Output:

Tuple di Jawa

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);
}
}
Salin selepas log masuk

Output:

Tuple di Jawa

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);
}
}
Salin selepas log masuk

Output:

Tuple di Jawa

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);
}
}
Salin selepas log masuk

Output:

Tuple di Jawa

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.

Atas ialah kandungan terperinci Tuple di Jawa. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Label berkaitan:
sumber:php
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan