> Java > java지도 시간 > 본문

자바의 튜플

王林
풀어 주다: 2024-08-30 16:15:58
원래의
984명이 탐색했습니다.

튜플은 순서가 지정된 다양한 유형의 개체 모음으로 간주됩니다. 객체들은 서로 관련될 수도 있고 그렇지 않을 수도 있지만 집합적으로 특정한 의미를 갖습니다. Java에는 튜플이 지원하는 내장 데이터 구조가 없습니다. 따라서 요구사항이 발생할 때마다 클래스가 생성됩니다. 그 외에도 이 기능은 목록뿐만 아니라 배열에서도 사용할 수 있습니다. 그러나 다른 데이터 유형의 데이터는 이를 유지할 수 없습니다.

구문

아래는 Tuples의 구문입니다.

무료 소프트웨어 개발 과정 시작

웹 개발, 프로그래밍 언어, 소프트웨어 테스팅 등

생성자의 튜플:

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에서 Tuples의 주요 특징입니다

  • 타입 안전
  • 반복 가능
  • toString() 구현
  • Comparable(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>
튜플 크기 이름 샘플 요소 1개 단위 단위 두 요소 페어 페어 세 가지 요소 삼중선 삼중항 4가지 요소 4중주 콰르텟 5대 요소 중주 5중주 6가지 요소 섹스텟 섹스텟 7가지 요소 7월 Septet 8개 요소 옥텟 옥텟 9가지 요소 애니어 엔네드 10대 요소 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:

자바의 튜플

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:

자바의 튜플

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:

자바의 튜플

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:

자바의 튜플

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:

자바의 튜플

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:

자바의 튜플

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.

위 내용은 자바의 튜플의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

관련 라벨:
원천:php
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!