java多返回值javafx.util.Pair

本文详细介绍了JavaFX中的Pair类,包括其作用、构造方法、主要属性和方法,如getKey()、getValue()、toString()、hashCode()以及equals()。此外,还提供了一个示例展示如何创建并使用Pair类来存储键值对。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

源码:

/*
 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
 * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

package javafx.util;

import java.io.Serializable;
import javafx.beans.NamedArg;

 /**
  * <p>A convenience class to represent name-value pairs.</p>
  * @since JavaFX 2.0
  */
public class Pair<K,V> implements Serializable{

    /**
     * Key of this <code>Pair</code>.
     */
    private K key;

    /**
     * Gets the key for this pair.
     * @return key for this pair
     */
    public K getKey() { return key; }

    /**
     * Value of this this <code>Pair</code>.
     */
    private V value;

    /**
     * Gets the value for this pair.
     * @return value for this pair
     */
    public V getValue() { return value; }

    /**
     * Creates a new pair
     * @param key The key for this pair
     * @param value The value to use for this pair
     */
    public Pair(@NamedArg("key") K key, @NamedArg("value") V value) {
        this.key = key;
        this.value = value;
    }

    /**
     * <p><code>String</code> representation of this
     * <code>Pair</code>.</p>
     *
     * <p>The default name/value delimiter '=' is always used.</p>
     *
     *  @return <code>String</code> representation of this <code>Pair</code>
     */
    @Override
    public String toString() {
        return key + "=" + value;
    }

    /**
     * <p>Generate a hash code for this <code>Pair</code>.</p>
     *
     * <p>The hash code is calculated using both the name and
     * the value of the <code>Pair</code>.</p>
     *
     * @return hash code for this <code>Pair</code>
     */
    @Override
    public int hashCode() {
        // name's hashCode is multiplied by an arbitrary prime number (13)
        // in order to make sure there is a difference in the hashCode between
        // these two parameters:
        //  name: a  value: aa
        //  name: aa value: a
        return key.hashCode() * 13 + (value == null ? 0 : value.hashCode());
    }

     /**
      * <p>Test this <code>Pair</code> for equality with another
      * <code>Object</code>.</p>
      *
      * <p>If the <code>Object</code> to be tested is not a
      * <code>Pair</code> or is <code>null</code>, then this method
      * returns <code>false</code>.</p>
      *
      * <p>Two <code>Pair</code>s are considered equal if and only if
      * both the names and values are equal.</p>
      *
      * @param o the <code>Object</code> to test for
      * equality with this <code>Pair</code>
      * @return <code>true</code> if the given <code>Object</code> is
      * equal to this <code>Pair</code> else <code>false</code>
      */
     @Override
     public boolean equals(Object o) {
         if (this == o) return true;
         if (o instanceof Pair) {
             Pair pair = (Pair) o;
             if (key != null ? !key.equals(pair.key) : pair.key != null) return false;
             if (value != null ? !value.equals(pair.value) : pair.value != null) return false;
             return true;
         }
         return false;
     }
 }


    public static void main(String[] args) {
        Pair<String, Integer> test = test();
        System.out.println(test.getKey());
        System.out.println(test.getValue());
    }


    private static Pair<String, Integer> test() {
        return new Pair<>("hello", 1);
    }
### JavaFX项目中`javafx.util.converter`包不存在的问题 对于JavaFX项目遇到`javafx.util.converter`包不存在的情况,可以从以下几个方面来排查并解决问题。 在某些环境中,如使用特定版本的OpenJDK或者Zulu JDK时,可能会缺少JavaFX库的支持[^2]。这是因为这些发行版默认并不包含完整的JavaFX模块集。当尝试访问像`javafx.util.*`这样的包时就会抛出错误提示程序包不存在。 针对这个问题的一种解决方案是在构建工具(Maven或Gradle)配置文件里加入依赖项以引入所需的JavaFX模块。例如,在POM.xml中添加如下片段可以确保获取到必要的组件: ```xml <dependency> <groupId>org.openjfx</groupId> <artifactId>javafx-controls</artifactId> <version>17.0.1</version> </dependency> <dependency> <groupId>org.openjfx</groupId> <artifactId>javafx-fxml</artifactId> <version>17.0.1</version> </dependency> <!-- 如果确实需要用到converter, 可能还需要额外指定 --> <dependency> <groupId>org.openjfx</groupId> <artifactId>javafx-base</artifactId> <version>17.0.1</version> </dependency> ``` 另外一种方法则是调整IDE设置以及项目的classpath路径。确认所使用的开发环境已经正确设置了指向含有JavaFX SDK的JDK版本,并且将相应的jar文件放置于合适的位置以便被加载进工程之中[^3][^4]。如果正在使用IntelliJ IDEA,则需检查Project Structure里的Modules选项卡下是否有正确的SDK关联。 最后值得注意的是,不同操作系统平台上的处理方式可能存在差异。比如在MacOS M1设备上运行时,由于架构兼容性的原因,可能需要特别挑选适合ARM64处理器的JDK版本才能正常工作[^5]。 #### 关键点总结: - 添加适当的Maven/Gradle依赖; - 设置好IDE内的JDK配置; - 对于特殊硬件平台考虑其特有的适配需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值