Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9115693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T04:29:31+00:00 2026-06-17T04:29:31+00:00

I am given the Pair.java class and have to implement the PairTools.java class. Pair.java

  • 0

I am given the Pair.java class and have to implement the PairTools.java class.

Pair.java

import java.util.Objects;

public class Pair<A, B> {

    public final A a;
    public final B b;

    public Pair(A a, B b) {
        this.a = a;
        this.b = b;
    }

    @Override
    public String toString() {
        // appending things to the empty string prevents us from having to worry about null
        // and calling toString explicitly, Objects.toString(a) + " " + Objects.toString(b)
        // would also work
        return "" + a + " " + b;
    }

    @Override
    public boolean equals(Object obj) {
        // `obj instanceof Pair` will automatically return false if obj is null
        if (!(obj instanceof Pair)) {
            return false;
        }

        // some warnings with generics are unavoidable
        @SuppressWarnings("unchecked")
        Pair<A, B> p = (Pair<A, B>) obj;

        // we use Objects.equals() to handle nulls easily
        return Objects.equals(a, p.a) && Objects.equals(b, p.b);
    }

    @Override
    public int hashCode() {
        // we use Objects.hashCode() to handle nulls easily, 
        // the operation ^ is XOR, not exponentiation
        return Objects.hashCode(a) ^ Objects.hashCode(b);
    }
}

In PairTools.java I have to implement the following method:

public class PairTools {

    /**
     * this is how you can use wildcards in generics
     * 
     * @param pair (assume never null)
     * @return a pair containing two references to a of the given pair
     */
    public static <A> Pair<A, A> copyA(Pair<A, ?> pair) {
        return null;
    }

}

I don’t understand the implementation. I need an explanation.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T04:29:33+00:00Added an answer on June 17, 2026 at 4:29 am

    A possible implementation can look like this.

    public class PairTools {
    
        /**
         * this is how you can use wildcards in generics
         * 
         * @param pair (assume never null)
         * @return a pair containing two references to a of the given pair
         */
        public static <A> Pair<A, A> copyA(Pair<A, ?> pair) {
            return new Pair<A, A>(pair.a, pair.a);
        }
    
    }
    

    This ignores the b value of the given pair and returns a new pair with two references to a.

    You cannot simply do this

    return new Pair<A, A>(pair.a, pair.b);
    

    because you have to return a Pair<A, A>. You get a Pair<A, ?> as parameter so you can only be sure that the first value of the given pair is of type A. You don’t know the type of pair.b.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In Java, given a pair of public methods, enableFoo and disableFoo, that set a
I have a java.util.HashMap object m (a return value from a call to Java
I'm writing a Java application that will instantiate objects of a class to represent
I'm trying to understand some String class functions in Java. So, here's is a
this is the code I have written in Java, itr is the iterator for
I've come across a tuples problem where given a list of pair tuples it
Given that the web application doesn't have su privileges, I'd like to execute a
Given this method to work on a HTML page in a webbrowser: bool semaphoreForDocCompletedEvent;
given a Python class hierarchy, say class Base: def method1 def method2 def method3
I want to do a very simple job: given a string containing pronouns, I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.