java 配对问题_Java中的配对类是什么?

java 配对问题

Java Pair class stores two values in the form of a tuple. This can be useful to get a function to return two values.

Java Pair类以元组的形式存储两个值。 这对于使函数返回两个值很有用。

Java has an inbuilt Pair class from Java 8 onwards. That is why in this lesson we will not only look at the inbuilt pair class but also learn how to make our own pair class.

Java具有从Java 8开始内置的Pair类。 这就是为什么在本课中,我们不仅要看内建的配对类,还要学习如何制作自己的配对类。

Pair class is very useful when using a Tree data structure. While using recursion on a tree, pair class makes it easy to pass values from one node to another. These values can be minimum and maximum node value up to that node. This would prevent revisiting certain nodes again and again.

配对类在使用Tree数据结构时非常有用。 在树上使用递归时,pair类使将值从一个节点传递到另一个节点变得容易。 这些值可以是直到该节点的最小和最大节点值。 这将防止一次又一次地访问某些节点。

Java内置的Pair类 (In-built Pair class in Java)

Inbuilt Pair class is a part of the JavaFX package. However, this package is not part of the standard OpenJDK installation. We can add it to our project using the following maven dependencies.

内置对类是JavaFX包的一部分。 但是,此软件包不是标准OpenJDK安装的一部分。 我们可以使用以下Maven依赖项将其添加到我们的项目中。


<dependency>
	<groupId>org.openjfx</groupId>
	<artifactId>javafx-base</artifactId>
	<version>14.0.1</version>
</dependency>

Let’s get right to the usage of the inbuilt pair classes in Java.

让我们开始使用Java中的内置对类。

导入配对类 (Importing Pair class)

To import use:

导入使用:


import javafx.util.Pair;

Inbuilt pair class uses the notation of Key and Value to store a pair.

内置对类使用键和值的表示法来存储对。


<Key, Value>

However, don’t confuse it with HashMap.

但是,不要将其与HashMap混淆。

声明一个配对对象 (Declaring a Pair object)


 Pair<Integer, String> p = new Pair<>(7,"Hello");

This creates a pair object of the type <Integer, String> .

这将创建<Integer,String>类型的对对象。

The constructor takes the values 7 and “hello” and stores them in the Pair class.

构造函数将值7和“ hello”存储在Pair类中。

访问值 (Accessing values )

We can access values from Pair object using getKey() and getValue() methods.

我们可以使用getKey()和getValue()方法从Pair对象访问值。

  • getKey gets the first value.

    getKey获取第一个值。
  • getValue gets the second value.

    getValue获取第二个值。

System.out.println("the first value is :" + p.getKey());
System.out.println("the first second is :" + p.getValue());
Pairclass Output
Output
输出量

The complete code for this section is :

本节的完整代码为:


import javafx.util.Pair;

public class Main {

public static void main(String[] args)
{
Pair<Integer, String> p = new Pair<>(7,"Hello");
System.out.println("the first value is :" + p.getKey());
System.out.println("the first second is :" + p.getValue());
}
}

创建我们自己的Pair类 (Creating our own Pair class )

Creating our own pair class is a better alternative as we can customize it according to our own preference. Also, if you are not using JavaFX in your project, it makes sense to avoid it completely.

创建我们自己的pair类是更好的选择,因为我们可以根据自己的喜好自定义它。 另外,如果您不在项目中使用JavaFX,则完全避免使用JavaFX是有意义的。

1.定义自定义对类 (1. Defining the Custom Pair class)

For the purpose of simplicity, we are using a pair class that stores two values. An Integer and a String. Using generics would be a good idea too.

为了简单起见,我们使用一个存储两个值的对类。 整数和字符串。 使用泛型也是一个好主意。


public class Pair {
        int first;
        String second;
        public Pair(int first, String second) {
            this.first = first;
            this.second = second;
        }
}

Two variables first and second have been defined to store two values of the type int and String respectively.

已经定义了两个变量first和second来分别存储int和String类型的两个值。

The constructor takes two values and assigns them to the variables.

构造函数采用两个值并将它们分配给变量。

2.配对活动 (2. Pair class in action)

To use the pair class just created, define an object of the pair class and call the constructor.

要使用刚刚创建的对类,请定义对类的对象并调用构造函数。


Pair a = new Pair(1, "one");

This creates a pair object with 1 stored in first and “one” stored in second.

这将创建一个配对对象,其中第一个存储在一个对象中,第二个存储在一个对象中。

To access the values :

要访问这些值:


System.out.println("the first value is :" + a.first);
System.out.println("the first second is :" + a.second);
Self Created Pair Output
Output
输出量

Since pair class is defined by us, we can customize and add a print function to it.

由于pair class是由我们定义的,因此我们可以自定义并为其添加打印功能。

Code for pair class :

配对类的代码:


 public class Pair {
        int first;
        String second;
        public Pair(int first, String second) {
            this.first = first;
            this.second = second;
        }
        public void print(){
            System.out.println("< "+first+", "+second+ " >");
        }
        
        public static void main(String[] args){
            Pair a = new Pair(1,"Hello");
            a.print();
        }
}
print -pairclass
output
输出

Including a print function, this way is a good coding habit as now the variables could be made private. Keeping the variables public is not safe.

包括打印功能在内,这种方式是一种良好的编码习惯,因为现在可以将变量设为私有。 公开变量是不安全的。

将Pair类与函数一起使用 (Using Pair class with a function )

Pair class can be used to return two values from a function. To do this, the return type of the function must be pair.

Pair类可用于从函数返回两个值。 为此,该函数的返回类型必须为pair。


 public static Pair func (Scanner s){
    System.out.println("what is your name");
    String name = s.next();
    System.out.println("what is your favourite number?");
    int num = s.nextInt();
    Pair ans = new Pair(num, name);
    return ans;
}

The object that is returned from this function can be captured in the main function. Let’s print the object in the main.

从此函数返回的对象可以在主函数中捕获。 让我们在主体中打印对象。


public static void main(String[] args) {
Scanner s = New Scanner(System.in)
Pair ans = func(s);
ans.print();
}
Pair With Function
Output
输出量

The complete code for this section is as follows.

本节的完整代码如下。


import java.util.Scanner;

public class Main {       
public static void main(String[] args) {
Scanner s = New Scanner(System.in)
Pair ans = func(s);
ans.print();
}

public static Pair func (Scanner s){
System.out.println("what is your name");
String name = s.next();
System.out.println("what is your favourite number?");
int num = s.nextInt();
Pair ans = new Pair(num, name);
return ans;
}
}

结论 (Conclusion )

Pair classes are important and can improve the efficiency of the code if used properly. One interesting area to use pair class is to store the maximum and minimum values of a subtree in BST problems. This saves the multiple traversals down the tree.

配对类很重要,如果使用得当,可以提高代码的效率。 使用配对类的一个有趣的领域是存储BST问题中子树的最大值和最小值。 这样可以将多次遍历保存在树中。

翻译自: https://www.journaldev.com/41647/pair-class-in-java

java 配对问题

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值