Java compares the values of two list to be equal the method _java

Source: Internet
Author: User

The example in this article describes how Java compares the values of two list to be equal. Share to everyone for your reference. Specifically as follows:

Suppose two queues {1,2,3,4} and {4,3,2,1}, this method is used to compare whether the values contained in these two queues are equal

/**
 * Queue comparison
 * @param <T>
 * @param a
 * @param b
 * @return/public
static <t extends C omparable<t>> Boolean compare (List<t> A, list<t> b) {
  if (a.size ()!= b.size ())
    return false;
  Collections.sort (a);
  Collections.sort (b);
  for (int i=0;i<a.size (); i++) {
    if (!a.get (i). Equals (B.get (i)), return
      false;
  return true;
}
The test method is as follows: public
static void Main (string[] args) {
  list<integer> a = arrays.aslist (1,2,3,4);
  List<integer> B = arrays.aslist (4,3,2,1);
  System.out.println (Compare (A, b));
Execution result True

I hope this article will help you with your Java programming.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.