java中判断两个对象是否相等

Posted sunny_2016

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java中判断两个对象是否相等相关的知识,希望对你有一定的参考价值。

package ceshi.com.job;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class EqualTest {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String A = "ABba";
        String B = "ABbaa";
        
        if (A.equals(B)) {
            System.out.println("相等");
            
        }else
            System.out.println("不相等");
        
        System.out.println("-------------");
//        String C = new String("hello");
        String C = "hello";
        String D = "hello";
        
        if (C==D) {
            System.out.println("相等");
            
        }else
            System.out.println("不相等");
        System.out.println("——————————————————————");
        String[] E = {"1","2","3","4"};
        String[] F = {"1","2","3","4"};
        
        if (Arrays.equals(E,F)) {
            System.out.println("相等");
        }else
            System.out.println("不相等");
        System.out.println("---------ooo");
        String[] G = {"1","2","3","4"};
        String[] H = {"1","2","3","4"};
        if (G.length==H.length) {
            for (int i = 0; i < G.length; i++) {
                if (G[i].equals(H[i])) {
//                    System.out.println("相等");
                }else
                    System.out.println("不相等");
            }
            System.out.println("相等");
        }else{
            System.out.println("长不不相等,不需要判断");
        }
        System.out.println("-------------");
        
        List<String> one = Arrays.asList("Tom","Anthony","Beijing");
        List<String> two = Arrays.asList("Tom","Anthony","Beijing");
        
        if (one.containsAll(two)) {
            System.out.println("相等");
        }else{
            System.out.println("不相等");
            
        }
        System.out.println("-----------");
        List<String> str1 = Arrays.asList("Tom","Anthony");
        List<String> str2 = Arrays.asList("Tom");
        
        if (str1.size() == str2.size()) {
            for (int i = 0; i < str1.size(); i++) {
                if (str1.get(i).equals(str2.get(i))) {
                    //do thing
                }else{
                    System.out.println("不相同");
                }
            }
            System.out.println("");
            
        }else{
            System.out.println("长度不相等,没有必要比较");
        }
    }
        
    
}

 

以上是关于java中判断两个对象是否相等的主要内容,如果未能解决你的问题,请参考以下文章

c# 比较两个对象的值是不是相等

Java 中判断两个对象是否相等

Java中怎么比较两个字符串是不是相等?

java中判断两个对象是否相等

Java 判断两个对象是否相等

如何判断两个对象的值是不是相等java