HashSet常用方法:
向Set集合中添加元素
但不能添加重复元素,Set集合中不允许元素重复
因为HashMap的key不允许重复,所以HashSet添加的元素也不允许重复
Set集合中HashSet不是按照存储顺序保存,输出顺序具有不确定性
import java.util.HashSet;public class Demo {public static void main(String[] args) {HashSet set = new HashSet();set.add("a");set.add("c");set.add("b");for (String name : set) {System.out.println(name);}}
}
若添加元素重复:
import java.util.HashSet;public class Demo {public static void main(String[] args) {HashSet set = new HashSet();set.add("a");set.add("c");set.add("b");set.add("b");System.out.println(set.size());for(String name:set) {System.out.println(name);}}
}
返回Set集合中的元素个数
import java.util.HashSet;public class Demo {public static void main(String[] args) {HashSet set = new HashSet();set.add("a");set.add("c");set.add("b");System.out.println(set.size());}
}
删除Set集合中的元素,删除成功返回true,否则返回false
import java.util.HashSet;public class Demo {public static void main(String[] args) {HashSet set = new HashSet();set.add("a");set.add("c");set.add("b");System.out.println(set.size());boolean flag1 = set.remove("a");System.out.println(flag1);System.out.println(set.size());boolean flag2 = set.remove("a");System.out.println(flag2);System.out.println(set.size());}
}
判断Set集合是否为空,若为则返回true,否则返回false
import java.util.HashSet;public class Demo {public static void main(String[] args) {HashSet set = new HashSet();boolean flag1 = set.isEmpty();System.out.println(flag1);set.add("a");set.add("c");set.add("b");flag1 = set.isEmpty();System.out.println(flag1);}
}
清空Set集合
import java.util.HashSet;public class Demo {public static void main(String[] args) {HashSet set = new HashSet();boolean flag1 = set.isEmpty();System.out.println(flag1);set.add("a");set.add("c");set.add("b");flag1 = set.isEmpty();System.out.println(flag1);set.clear();flag1 = set.isEmpty();System.out.println(flag1);}
}
判断集合中是否含有指定元素o,如果含有则返回true,否则返回false
import java.util.HashSet;public class Demo {public static void main(String[] args) {HashSet set = new HashSet();boolean flag1;set.add("a");set.add("c");set.add("b");flag1 = set.contains("a");System.out.println(flag1);flag1 = set.contains("d");System.out.println(flag1);}
}
判断其他的对象是否和该对象相等,参数o引用的对象与当前对象为同一个对象时,就返回true,否则返回false.
import java.util.HashSet;public class Demo {public static void main(String[] args) {HashSet set1 = new HashSet();HashSet set2 = new HashSet();System.out.println(set1.equals(set2));set1.add("a");set1.add("c");set1.add("b");System.out.println(set1.equals(set2));set2.add("a");set2.add("c");set2.add("b");System.out.println(set1.equals(set2));}
}
import java.util.HashSet;public class Demo {public static void main(String[] args) {HashSet set1 = new HashSet();set1.add("a");set1.add("c");set1.add("b");System.out.println(set1);for (String string : set1) {System.out.println(string);}}
}
迭代器,用于遍历,将Set集合中的元素转存
import java.util.HashSet;
import java.util.Iterator;public class Demo {public static void main(String[] args) {HashSet set1 = new HashSet();set1.add("a");set1.add("c");set1.add("b");System.out.println(set1);Iterator list = set1.iterator();while (list.hasNext()) {System.out.println(list.next());}}
}