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 8357297
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T10:26:01+00:00 2026-06-09T10:26:01+00:00

A.java public class A implements Comparable { private String id; private String name; public

  • 0

A.java

public class A implements Comparable {
    private String id;
    private String name;

    public A(String a, String b) {
        id = a;
        name = b;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int compareTo(Object o) {
        A a = (A) o;
        return id.compareTo(a.getId());
    }
}

B.java

public class B implements Comparable {
    private String b_id;
    private String other;

    public B(String a, String b) {
        b_id = a;
        other = b;
    }

    public String getBId() {
        return b_id;
    }

    public void setBId(String id) {
        this.b_id = id;
    }

    public String getOther() {
        return other;
    }

    public void setOther(String other) {
        this.other = other;
    }

    public int compareTo(Object o) {
        B b = (B) o;
        return b_id.compareTo(b.getId());
    }
}

Learn.java

public class Learn {

    public static void main(String[] args) {

        List<A> listA = new ArrayList<A>();
        List<B> listB = new ArrayList<B>();
        List<Object> listAll = new ArrayList<Object>();
        listA.add(new A("aa", "bb"));
        listA.add(new A("ae", "bbn"));
        listA.add(new A("dfr", "GSDS"));
        listB.add(new B("nm", "re"));
        listB.add(new B("asd", "asfa"));

        listAll.addAll(listA);
        listAll.addAll(listB);
        Collections.sort(listAll);
        for (Object o : listAll) {
            if (o instanceof A)
                System.out.println(o.getId);
            else if (o instanceof B)
                Syatem.out.println(o.getBId);
        }

    }

}

The error i get is at the line Collections.sort(listAll);
It says.

Bound mismatch: The generic method sort(List<T>) of type Collections is not applicable
for the arguments (List<Object>). The inferred type Object is not a valid substitute
for the bounded parameter <T extends Comparable<? super T>>

What to do? Also is the rest of the logic all right?

What i am trying to do is have a list of A and list of B with one attribute same as id; though the variable name is not the same. i.e id in A and bid in B.
Now i put both the lists in ListAll and do sort on them on the same variable id/bid.
I have A and B implementing Comparable.

and my listAll is of type Object?

how do I do it?
thanks.

  • 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-09T10:26:04+00:00Added an answer on June 9, 2026 at 10:26 am

    You could add a common base class and implement comparison there, as in:

    abstract class AandBComparable implements Comparable {
    
      public int compareTo(Object o) {
        AandBComparable ab = (AandBComparable) o;
        return getId().compareTo(ab.getId());
      }
    
      public abstract String getId();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My Address Class: public class Address implements java.io.Serializable { private String addressId; private String
I have this class public class wordObject implements java.io.Serializable { String wordName; int occCount;
package geometricobject; public abstract class GeometricObject implements Comparable { private String color = white;
$ cat Const.java public class Const { String Const(String hello) { return hello; }
import java.util.*; public class Test { public static void main(String[] args) { List db
import java.math.BigInteger; public class Rational extends Number implements Comparable { // Data fields for
My SSCE: public class ComparableItem implements Comparable<ComparableItem> { private final int itemNo; public ComparableItem(final
I have a class Node Node.java public class WebPage implements BusinessObject { @Id private
Here's Pair.java import java.lang.*; import java.util.*; public class Pair<TYPEA, TYPEB> implements Comparable< Pair<TYPEA, TYPEB>
I have two entities A and B: A.java: ... public class A implements Serializable

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.