Java TreeSet, HashSet and HashMap can use equals () comparison

problem

TreeSet, HashSet and Map collections can be used equals () do?

answer

Can euquals (Object o) to reflect the interface type and the elements are identical, and independent of the input sequence;

import java.util.HashMap;
import java.util.HashSet;
import java.util.TreeSet;

public class test {
    public static void main(String[] args) {
        TreeSet ts = new TreeSet();
        ts.add('3');
        ts.add('4');
        TreeSet ts1 = new TreeSet();
        ts1.add('3');
        ts1.add('4');
        TreeSet ts2 = new TreeSet();
        ts2.add('4');
        ts2.add('3');
        System.out.println(ts.equals(ts1));//true
        System.out.println(ts1.equals(ts2));//true
        System.out.println("-------------");
        HashSet hs = new HashSet();
        hs.add('3');
        hs.add('4');
        HashSet hs1 = new HashSet();
        hs1.add('3');
        hs1.add('4');
        HashSet hs2 = new HashSet();
        hs2.add('4');
        hs2.add('3');
        System.out.println(hs.equals(hs1));//true
        System.out.println(hs1.equals(hs2));//true
        System.out.println(ts1.equals(hs1));//true
        System.out.println("-------------");
        HashMap hm = new HashMap();
        hm.put('3', '3');
        hm.put('4', '4');
        HashMap hm1 = new HashMap();
        hm1.put('3', '3');
        hm1.put('4', '4');
        HashMap hm2 = new HashMap();
        hm2.put('4', '4');
        hm2.put('3', '3');
        System.out.println(hm.equals(hm1));//true
        System.out.println(hm1.equals(hm2));//true
        System.out.println(hs1.equals(hm2));//false
    }
}
Published 59 original articles · won praise 60 · views 1576

Guess you like

Origin blog.csdn.net/Regino/article/details/104553286