1.HashSet:将元素存储在哈希表中,性能较好,但不保证迭代顺序。
package learn.collection;
import org.testng.annotations.Test;
import java.util.*;
public class hashset {
Set<String> hashset = new HashSet<String>();
Set<String> treeset = new TreeSet<String>();
Set<String> linkedhashset = new LinkedHashSet<String>();
@Test(groups = {"set"},priority = 0)
public void addmethod(){
hashset.add("3");
hashset.add("e");
hashset.add("a");
hashset.add("1");
hashset.add("2");
System.out.println("hashset add 方法:"+hashset);
}
@Test(groups = {"set"},priority = 1)
public void addAllmethod(){
Set<String> newhashset = new HashSet<>();
System.out.println("addAll方法调用前hashset:"+hashset);
newhashset.add("a");
newhashset.add("c");
newhashset.add("金");
hashset.addAll(newhashset);
System.out.println("addAll方法调用后hashset:"+hashset);
}
@Test(groups = {"set"},priority = 2)
public void containsmethod(){
String str = "5";
String str2 = "1";
boolean judge = hashset.contains(str);
boolean judge2 = hashset.contains(str2);
if(!judge) {
System.out.println(hashset + "不包含" + str);
}
if(judge2) {
System.out.println(hashset + "包含" + str2);
}
}
@Test(groups = {"set"},priority = 3)
public void containsallmethod(){
Set<String> newhashset = new HashSet<>();
newhashset.add("1");
newhashset.add("a");
boolean judge = hashset.containsAll(newhashset);
if(judge){
System.out.println(hashset+"包含"+newhashset);
}
newhashset.add("b");
boolean judge2 = hashset.containsAll(newhashset);
if(judge2){
System.out.println(hashset+"包含"+newhashset);
}else{
System.out.println(hashset+"不包含"+newhashset);
}
}
@Test(groups = {"set"},priority = 4)
public void removemethod(){
System.out.println("remove方法调用前hashset:"+hashset);
hashset.remove("b");
System.out.println("remove方法调用后hashset:"+hashset);
hashset.remove("a");
System.out.println("remove方法调用后hashset:"+hashset);
}
@Test(groups = {"set"},priority = 5)
public void retainmethod(){
Set<String> newhashset = new HashSet<>();
newhashset.add("金");
newhashset.add("6");
hashset.retainAll(newhashset);
System.out.println(hashset+"调用retain方法:"+hashset);
}
}
2.TreeSet:将元素存储在红黑树中,按元素值顺序排列,比HashSet处理较慢。
package learn.collection;
import org.testng.annotations.Test;
import java.util.Set;
import java.util.TreeSet;
public class treeset {
private Set<String> treeset = new TreeSet<>();
@Test(groups = {"treeset"},priority = 0)
public void treesetmethod(){
System.out.println("treeset add调用前"+treeset);
treeset.add("3");
treeset.add("b");
treeset.add("1");
treeset.add("a");
treeset.add("2");
System.out.println("treeset add调用后"+treeset);
}
}
3.LinkedHashSet:作为哈希表实现,用链表连接元素,按元素插入顺序排列。
package learn.collection;
import org.testng.annotations.Test;
import java.util.LinkedHashSet;
import java.util.Set;
public class linkedhashset {
Set<String> linkedhashset = new LinkedHashSet<>();
@Test(groups = {"linkedhashset"},priority = 0)
public void treesetmethod(){
System.out.println("linkedhashset add调用前"+linkedhashset);
linkedhashset.add("3");
linkedhashset.add("b");
linkedhashset.add("1");
linkedhashset.add("a");
linkedhashset.add("2");
System.out.println("linkedhashset add调用后"+linkedhashset);
}
}
扩展知识:
接口、接口继承
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。