繁花如伊
1234567891011121314151617181920212223242526272829303132333435363738import java.util.HashSet;import java.util.Iterator;import java.util.Set; public class SetTest { //结果输出为ELEM static void AddToSet(){ String str = "ELEM"; String str1 = new String("ELEM"); Set set = new HashSet(); Set setCompare = new HashSet(); set.add(str); set.add(str1); setCompare.add(str1); //Iterator是用来遍历容器中的元素的 Iterator itera = set.iterator(); Iterator iteraCompare = setCompare.iterator(); while(itera.hasNext()) { System.out.println((String)itera.next()); } while(iteraCompare.hasNext()) { System.out.println((String)iteraCompare.next()); } if(set.equals(setCompare)){ System.out.println("两个对象内容相同"); } if(set == setCompare){ System.out.println("两个对象相等"); } } public static void main(String[] args){ SetTest.AddToSet(); } }