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("长度不相等,没有必要比较");
        }
    }
        
    
}

 

posted on 2017-10-27 13:02  sunny_2016  阅读(349)  评论(0编辑  收藏  举报

导航