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

  • SEARCH
  • Home
  • 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 6797777
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:39:02+00:00 2026-05-26T18:39:02+00:00

I have a class for a string-number pair. This class has the method compareTo

  • 0

I have a class for a string-number pair. This class has the method compareTo implemented.

A method of another class returns a collection of elements of the pair type.

I wanted to perform a unit test on this method, and therefore wrote the following:

@Test
public void testWeight() {

    Collection<StringNumber<BigDecimal>> expected = new Vector<StringNumber<BigDecimal>>();
    expected.add(new StringNumber<BigDecimal>("a", BigDecimal.ONE));
    expected.add(new StringNumber<BigDecimal>("b", BigDecimal.ONE));

    Collection<StringNumber<BigDecimal>> actual = new Vector<StringNumber<BigDecimal>>();
    expected.add(new StringNumber<BigDecimal>("a", BigDecimal.ONE));
    expected.add(new StringNumber<BigDecimal>("b", BigDecimal.ONE));

    //Collection<StringNumber<BigDecimal>> actual = A.f();

    assertEquals(expected, actual);
}

But as you can see, the assertion fails, even though the elements in the collections are identical. What can be the reason?

The error I get is

java.lang.AssertionError: expected: java.util.Vector<[a:1, b:1]>
but was: java.util.Vector<[a:1, b:1]>

Which does not make scene to me.

  • 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-05-26T18:39:03+00:00Added an answer on May 26, 2026 at 6:39 pm

    Your StringNumber class requires equals() method. Then it will work. Assuming this class contains string and number fields (auto-generated by my IDE):

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (!(o instanceof StringNumber)) {
            return false;
        }
        StringNumber that = (StringNumber) o;
        if (number != null ? !number.equals(that.number) : that.number != null) {
            return false;
        }
        return !(string != null ? !string.equals(that.string) : that.string != null);
    
    }
    
    @Override
    public int hashCode() {
        int result = string != null ? string.hashCode() : 0;
        result = 31 * result + (number != null ? number.hashCode() : 0);
        return result;
    }
    

    Few remarks:

    • Two Vector‘s (why are you using such archaic data structure) are equal if:

    both […] have the same size, and all corresponding pairs of elements in the two lists are equal. (Two elements e1 and e2 are equal if (e1==null ? e2==null : e1.equals(e2)).)

    That’s why overriding equals() is required.

    • when implementing equals() you must implement hashCode(). Not required here, but better be safe than sorry: What issues should be considered when overriding equals and hashCode in Java?.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have class like this: public class object { [Key] int number; String mystring;
I have this class called Table: class Table { public string Name { get
I have this Class: public class User { public string id{ get; set; }
Suppose I have a class with a string instance attribute. Should I initialize this
Lets say I have this class in foobar-shared.lib: class FooBar { std::string m_helloWorld; }
Assume I have a class that looks like this: class Sample { public string
I have class: static class AnotherClass { int number; String isPrime; public int getNumber()
I have a class with a status : class A{ String status } This
I have a class like this Class A{ private String string1; private String string2;
I have a class type and I have a value in a string: Class<?>

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.