目錄
一.Set特點(diǎn)
特點(diǎn):無序,不重復(fù)
思考:如果對(duì)List容器中的元素去重?
二.遍歷:foreach,迭代器
三.擴(kuò)容: 初始容量16,負(fù)載因子0.75,擴(kuò)容增量1倍
性能參數(shù):初始容量,負(fù)載因子
默認(rèn)值: 初始容量16,負(fù)載因子0.75
示例:new HashSet<>(20, 0.5f);
四.HashSet
五.思考
1. 如何給ArrayList集合去重
???? ?@Before?? ?public void setup() {?? ??? ?list.add(1);?? ??? ?list.add(2);?? ??? ?list.add(3);?? ??? ?list.add(3);?? ??? ?list.add(4);?? ??? ?list.add(5);?? ??? ?list.add(3);?? ??? ?list.add(4);?? ??? ?//list.add(3);?? ?}
?? ?//去重復(fù)?? ?@Test?? ?public void test05() {?? ??? ??? ??? ?HashSet ll = new ArrayList (new??HashSet(list));?? ??? ??? ?ll.forEach(t->System.out.println(t));?? ?}
2. set有ArrayList中存在的通過下標(biāo)刪除,或foreach循環(huán)刪除時(shí)的問題嗎? 為什么
答:set無下標(biāo)? 所有沒有
3. set是否存在List刪除,傳入整數(shù)需要區(qū)分是基本型還是對(duì)象型的問題,【例如:list.remove(2)】,為什么??
答:存在?基本型的整數(shù)
六.TreeSet
示例:
?自定義比較器
①通過構(gòu)造函數(shù)傳入比較器文章來源:http://www.zghlxwxcb.cn/news/detail-545100.html
②實(shí)現(xiàn)排序接口文章來源地址http://www.zghlxwxcb.cn/news/detail-545100.html
一.Set特點(diǎn)
特點(diǎn):無序,不重復(fù)
思考:如果對(duì)List容器中的元素去重?
??
?? ?@Before
?? ?public void setup() {
?? ??? ?list.add(1);
?? ??? ?list.add(2);
?? ??? ?list.add(3);
?? ??? ?list.add(3);
?? ??? ?list.add(4);
?? ??? ?list.add(5);
?? ??? ?list.add(3);
?? ??? ?list.add(4);
?? ??? ?//list.add(3);
?? ?}?? ?//去重復(fù)
?? ?@Test
?? ?public void test05() {
?? ??? ?
?? ??? ?HashSet<Integer> ll = new HashSet<Integer>(list);
?? ?
?? ??? ?ll.forEach(t->System.out.println(t));
?? ?}
?? ?
?
二.遍歷:foreach,迭代器
//遍歷方式
?? ?@Test
?? ?public void test06() {
?? ??? ?Set<Integer> set = new HashSet<>();//里氏替換原則
?? ??? ?set.add(3);
?? ??? ?set.add(2);
?? ??? ?set.add(1);
?? ??? ?//foreach
?? ??? ?for (Integer i : set) {
?? ??? ??? ?System.out.println(i);
?? ??? ?}
?? ????//迭代器
?? ??? ?Iterator<Integer> it = set.iterator();
?? ??? ?while(it.hasNext()) {
?? ??? ??? ?Integer n =it.next();
?? ??? ??? ?System.out.println(n);
?? ??? ?}
?? ?
?? ?}
三.擴(kuò)容: 初始容量16,負(fù)載因子0.75,擴(kuò)容增量1倍
性能參數(shù):初始容量,負(fù)載因子
默認(rèn)值: 初始容量16,負(fù)載因子0.75
示例:new HashSet<>(20, 0.5f);
四.HashSet
- 它存儲(chǔ)唯一元素并允許空值??依據(jù)對(duì)象的hashcode來確定該元素是否存在
- 由HashMap支持
- 不保持插入順序
- 非線程安全
五.思考
1. 如何給ArrayList集合去重
??
?? ?@Before
?? ?public void setup() {
?? ??? ?list.add(1);
?? ??? ?list.add(2);
?? ??? ?list.add(3);
?? ??? ?list.add(3);
?? ??? ?list.add(4);
?? ??? ?list.add(5);
?? ??? ?list.add(3);
?? ??? ?list.add(4);
?? ??? ?//list.add(3);
?? ?}?? ?//去重復(fù)
?? ?@Test
?? ?public void test05() {
?? ??? ?
?? ??? ?HashSet<Integer> ll = new ArrayList<Integer> (new??HashSet<Integer>(list));
?? ?
?? ??? ?ll.forEach(t->System.out.println(t));
?? ?}
2. set有ArrayList中存在的通過下標(biāo)刪除,或foreach循環(huán)刪除時(shí)的問題嗎? 為什么
答:set無下標(biāo)? 所有沒有
3. set是否存在List刪除,傳入整數(shù)需要區(qū)分是基本型還是對(duì)象型的問題,【例如:list.remove(2)】,為什么??
答:存在?基本型的整數(shù)
六.TreeSet
- 是一個(gè)包含有序的且沒有重復(fù)元素的集合
- 作用是提供有序的Set集合,自然排序或者根據(jù)提供的Comparator進(jìn)行排序
- TreeSet是基于TreeMap實(shí)現(xiàn)的
示例:
//TreeMap
?? ?@Test
?? ?public void test07() {
?? ??? ?//回調(diào)函數(shù)
?? ??? ?TreeSet<Integer> ts = new TreeSet<>(
?? ??? ??? ?//匿名類
?? ??? ??? ?new Comparator<Integer>() {
?? ??? ??? ??? ?@Override
?? ??? ??? ??? ?public int compare(Integer o1, Integer o2) {
?? ??? ??? ??? ??? ?//降序
?? ??? ??? ??? ??? ?return o2-o1;
?? ??? ??? ??? ?}
?? ??? ??? ?}
?? ??? ??? ??? ?);? ? ? ? //測(cè)試數(shù)據(jù)
?? ??? ?ts.add(1);
?? ??? ?ts.add(3);
?? ??? ?ts.add(4);
?? ??? ?ts.add(5);
?? ??? ?ts.add(6);
?? ??? ?ts.add(8);
?? ??? ?ts.add(2);?? ??? ?//1. 默認(rèn)自然排序
?? ??? ?for(Integer e: ts) {
?? ??? ??? ?System.out.println(e);
?? ??? ?}
?? ??? ?
?? ?}
?
?自定義比較器
①通過構(gòu)造函數(shù)傳入比較器
TreeSet<Integer> tset = new TreeSet<Integer>(new Comparator<Integer>() {
?? ??? ??? ?@Override
?? ??? ??? ?public int compare(Integer o1, Integer o2) {
?? ??? ??? ??? ?// TODO Auto-generated method stub
?? ??? ??? ??? ?return o2 - o1;
?? ??? ??? ?}
?? ??? ?});
②實(shí)現(xiàn)排序接口
public class Student implements Comparable<Student>{
?? ?
?? ?private Integer sid;
?? ?
?? ?private String name;
?? ?
?? ?private int age;
?? ?
?? ?//構(gòu)造函數(shù),getter,setter,hashCode,equals等方法省略
?? ?
?? ?@Override
?? ?public int compareTo(Student o) {
?? ??? ?// TODO Auto-generated method stub
?? ??? ?return o.getAge() - this.getAge();
?? ?}}
到了這里,關(guān)于J2EE&集合框架&Set的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!