java set泛型_Java 集合二 泛型、Set相关
泛型
1、在定義一個類的方法時,因為不確定返回值類型,所以用一個符號代替,這個符號就是泛型
eg:ArrayList list = new ArrayList();
2、泛型的好處:
1、提高了數(shù)據(jù)的安全性,將運行時的問題提前暴露在編譯階段
2、避免了強轉(zhuǎn)的麻煩
3、泛型類的定義 包含了泛型的類就是泛型類
格式:class 類名{
}
說明:
1.類名后面跟著的泛型類型,是泛型的聲明,一旦泛型聲明出,就相當(dāng)于這個類型成了已知類型,這個類型可以在
整個類中使用。
2.泛型的聲明需要一個合法的標(biāo)識符即可。常用的大寫字母:T W Q K V E
3.泛型的確定:將來在使用這個類創(chuàng)建對象的時候
4、練習(xí) 模擬棧和隊列
importjava.util.LinkedList;public classDemo03 {public static voidmain(String[] args) {
MyStack ms = new MyStack();
ms.push("abc");
ms.push("aaaa");
ms.push("bbb");while(!ms.isEmpty()) {
System.out.println(ms.pop());
}
}
}class MyStack{private LinkedList ll = new LinkedList();//添加元素
public voidpush(T t) {
ll.addFirst(t);
}//刪除元素
publicT pop() {//return ll.removeLast();
returnll.removeFirst();
}//判斷為空
public booleanisEmpty() {returnll.isEmpty();
}
}
模擬棧
5、泛型方法的定義
1、在方法聲明中,帶著泛型聲明的方法,就是泛型方法
2、格式:修飾符返回值類型 方法名(參數(shù)列表){}
3、說明:
1.在方法上聲明的類型,可以在整個方法中當(dāng)做已知類型來使用。
2.如果非靜態(tài)的方法沒有任何泛型的聲明,那么可以使用類中定義的泛型。
3.如果靜態(tài)方法上沒有任何的泛型的聲明,那就不能使用類中定義的泛型。
6、泛型接口的定義與使用
1、帶著泛型定義的接口
2、定義格式:interface 接口名稱{}
3、說明:
1.在接口聲明上,定義好的泛型,作用范圍整個接口。
2.泛型接口被其它類實現(xiàn)的時候,有兩種實現(xiàn)方式:
1)聲明的類不再是一個泛型類,而是一個確定了泛型的類。
2)聲明的類還是一個泛型類,可以在創(chuàng)建對象的時候確定類型。
7、通配符
? extends E: E的類型以及子類類型
? super E:E的類型及其父類
Animal
|--Dog
|--YelloDog
method(? extends Animal)
method(? super Dog)
Set
1、Set是Collection的子接口
2、特點:無序、沒有索引、不能重復(fù)
3、是接口,所以是抽象的,所以實現(xiàn)的子類,HashSet、TreeSet
4、HashSet特點:使用哈希表的存儲方式存儲元素,相同的元素?zé)o法存進集合,集合本身沒有順序,存儲和取出的順序不一致
5、Set集合的遍歷:
第一種,轉(zhuǎn)成數(shù)組,toArray(),不帶泛型的數(shù)組,得到的是Object類型的數(shù)組
第二種,轉(zhuǎn)成數(shù)組,toArray(T[] arr) 帶泛型的數(shù)組,得到T類型的數(shù)組
當(dāng)自己定義的數(shù)組大小小于集合時,會自動創(chuàng)建一個新的數(shù)組,將新數(shù)組返回
當(dāng)自定義的數(shù)組大小大于等于集合時,不會自動創(chuàng)建,剩余位置用null填充,返回數(shù)組
第三種,使用迭代器 iterator()
第四種,增強for循環(huán)
格式:for (元素的數(shù)據(jù)類型 元素名稱:要遍歷的集合) {
使用元素名稱代表當(dāng)前訪問的元素
}
本質(zhì):底層還是迭代器,在使用增強for循環(huán)時使用集合的功能會觸發(fā)并發(fā)修改異常
使用增強for循環(huán)時無法修改元素
6、
/** 隨機生成10個20-40之間的隨機數(shù),存儲在合適的集合中,并且進行遍歷。
要求:隨機數(shù)不能重復(fù)。*/
importjava.util.HashSet;importjava.util.Random;importjava.util.Set;public classDemo01 {public static voidmain(String[] args) {
Set s = new HashSet<>();
Random r= newRandom();
Integer a ;while(s.size()<10) {
a= r.nextInt(21)+20;
s.add(a);
}for(Integer i:s) {
System.out.println(i);
}
}
}
HashSet練習(xí)
/** 鍵盤錄入一個字符串,輸出其中的字符,相同的字符只輸出一次*/
importjava.util.HashSet;importjava.util.Scanner;importjava.util.Set;public classDemo04 {public static voidmain(String[] args) {
Scanner sc= newScanner(System.in);
Set s = new HashSet<>();
String s1=sc.nextLine();for(int i=0;i
s.add(s1.charAt(i));
}for(Character c:s) {
System.out.print(c);
}
sc.close();
}
}
HashSet練習(xí)二
7、HashSet如何使元素唯一
先比較元素的HashCode(),如果相同則再比較元素的equals()方法,如果還相同則認為一致,不添加此元素
想要修改可以在元素的類中重寫這兩個方法
importjava.util.HashSet;public classDemo06 {public static voidmain(String[] args) {/** 姓名和年齡全部一致為相同元素
**/HashSet hs = new HashSet();
hs.add(new Person("jack",20));
hs.add(new Person("rose",30));
hs.add(new Person("wangwu",40));
hs.add(new Person("jack",20));
hs.add(new Person("zhaoli",21));
System.out.println(hs);
}
}classPerson {
String name;intage;public Person(String name, intage) {super();this.name =name;this.age =age;
}
@OverridepublicString toString() {return name+"..."+age;
}
@Overridepublic inthashCode() {return name.hashCode()+age;
}
@Overridepublic booleanequals(Object obj) {
Person p=(Person)obj;return name.equals(p.name)&&age==p.age;
}
}
HashSet元素重寫唯一性
8、LinkedHashSet 是HashSet的子類,在元素存儲的時候記錄了元素的前后地址
特點:元素的存儲取出順序一致
9、TreeSet 元素的順序按照字典順序排列的集合,保證元素唯一性有兩種
一種是讓元素本身具有比較性? 在元素類中繼承Comparable接口,重寫comparaTo方法
第二種是讓集合具有比較性 比較器,繼承Comparator接口,重寫compara方法
/** 對下列四位同學(xué)的成績做降序排序,如果成績一樣,
* 那在成績排序的基礎(chǔ)上按照年齡由小到大排序。*/
public classHomework4 {public static voidmain(String[] args) {
TreeSet s = new TreeSet<>();
s.add(new Student("qq",20,80));
s.add(new Student("ww",22,60));
s.add(new Student("ee",22,70));
s.add(new Student("rr",21,60));
s.add(new Student("qq",20,40));
System.out.println(s);
}
}class Student implements Comparable{privateString name;private intage;private floatscore;publicStudent() {}public Student(String name, int age, floatscore) {super();this.name =name;this.age =age;this.score =score;
}publicString getName() {returnname;
}public voidsetName(String name) {this.name =name;
}public intgetAge() {returnage;
}public void setAge(intage) {this.age =age;
}public floatgetScore() {returnscore;
}public void setScore(floatscore) {this.score =score;
}
@Overridepublic intcompareTo(Student o) {if(score==o.score) {if(age==o.age) {return 0;
}else if(age
}else{return 1;
}
}else if(score
}else{return -1;
}
}
@OverridepublicString toString() {return "Student [name=" + name + ", age=" + age + ", score=" + score + "]";
}
}
TreeSet 保證元素唯一
importjava.util.Comparator;importjava.util.Set;importjava.util.TreeSet;public classHomework7 {public static voidmain(String[] args) {//TODO Auto-generated method stub
Set set = new TreeSet<>(new Comparator() {
@Overridepublic intcompare(Student o1, Student o2) {//TODO Auto-generated method stub
return o1.scoreAll-o2.scoreAll>=0?-1:1;
}
});
set.add(new Student("qq",30,89,56));
set.add(new Student("ww",57,90,78));
set.add(new Student("ee",35,99,56));
set.add(new Student("rr",78,77,57));for(Student s:set) {
System.out.println(s);
}
}
}classStudent{
String name;intscore1;intscore2;intscore3;intscoreAll;public voidsetScoreAll() {
scoreAll= score1+score2+score3;
}public Student(String name, int score1, int score2, intscore3) {super();this.name =name;this.score1 =score1;this.score2 =score2;this.score3 =score3;
setScoreAll();
}
@OverridepublicString toString() {return name + ", 語文" + score1 + ", 數(shù)學(xué):" + score2 + ", 英語:" +score3+ ", 總分:" +scoreAll;
}
}
TreeSet比較器
總結(jié)
以上是生活随笔為你收集整理的java set泛型_Java 集合二 泛型、Set相关的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 特斯拉预测今年车辆交付量 将会推出最便
- 下一篇: 电脑有多个硬盘启动不了怎么办 电脑多硬盘