------<a href="http://www.itheima.com" target="blank">Java培训、Android培训、iOS培训、.Net培训</a>、期待与您交流! -------
集合框架(二)
一 泛型(Generic)
1. 泛型概述
A:泛型概述: 是一种把类型明确的工作推迟到创建对象或者调用方法的时候才去明确的特殊的类型。参数化类型,把类型当作参数一样的传递。
B:泛型的格式 : <数据类型> 这里的数据类型只能是引用数据类型
C:泛型好处
(1): 把运行时期的问题提前到了编译期间
(2): 避免了强制类型转换
(3): 优化了程序设计,解决了黄色警告线
2. ArrayList存储字符串并遍历泛型版
代码实现:
package cn.itcast_day080504;
import java.util.ArrayList;
import java.util.Iterator;
public class Test1 {
public static void main(String[] args) {
ArrayList<String> al = new ArrayList<String>();
al.add("Hello");
al.add("World");
al.add("Java");
Iterator<String> it = al.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
}
}
3. 泛型的由来:
通过Object转型问题引入.早期的Object类型可以接收任意的对象类型,但是在实际的使用中,会有类型转换的问题。 也就存在这隐患,所以Java提供了泛型来解决这个安全问题。
4. 泛型类
代码实现
package cn.itcast_test04;
public class GenericDemo {
public static void main(String[] args) {
ObjectTools<String> objTool = new ObjectTools<String>();
objTool.setT("张三") ;
String t = objTool.getT() ;
System.out.println(t);
System.out.println("---------------------------------");
ObjectTools<Integer> objTool2 = new ObjectTools<Integer>();
objTool2.setT(20);
Integer t2 = objTool2.getT() ;
System.out.println(t2);
}
}
package cn.itcast_test04;
public class ObjectTools<T> {
private T t ;
public T getT() {
return t;
}
public void setT(T t) {
this.t = t;
}
}
5. 泛型方法
代码实现:
package cn.itcast_test05;
public class GenericDemo {
public static void main(String[] args) {
// 创建对象
Phone p = new Phone();
p.show("张三");
p.show(20);
p.show(true);
}
}
package cn.itcast_test05;
public class Phone {
public <T> void show(T t){
System.out.println(t);
}
}
6. 泛型接口
代码实现:
package cn.itcast_day080504;
public interface Inter<T> {
public abstract void show(T t);
}
package cn.itcast_day080504;
public class InterfaceImp<T> implements Inter<T> {
public void show(T t) {
System.out.println(t);
}
}
package cn.itcast_day080504;
public class InterImpDemp {
public static void main(String[] args) {
InterfaceImp<String> i1 = new InterfaceImp<String>();
i1.show("刘德华");
InterfaceImp<Integer> i2 = new InterfaceImp<Integer>();
i2.show(20);
}
}
二. 增强for
1.概述
A:增强for概述: 简化数组和Collection集合的遍历
B:格式:
for(元素数据类型 变量 : 数组或者Collection集合) {
使用变量即可,该变量就是元素}
C:好处和注意事项
简化遍历
注意事项:增强for的目标要判断是否为null
2.ArrayList存储字符串增强for版
代码实现
package cn.itcast_day080505;
import java.util.ArrayList;
public class Test1 {
public static void main(String[] args) {
ArrayList<String> al = new ArrayList<String>();
al.add("西施");
al.add("貂蝉");
al.add("昭君");
for(String s : al){
System.out.println(s);
}
}
}
3.ArrayList 存储自定义对象增强for版
代码实现:
package cn.itcast_day080505;
import java.util.ArrayList;
import cn.itcast_day0803.Student;
public class Test2 {
public static void main(String[] args) {
ArrayList<Student> al = new ArrayList<Student>();
Student s1 = new Student("Peter",18);
Student s2 = new Student("Allen",18);
Student s3 = new Student("Bob",18);
al.add(s1);
al.add(s2);
al.add(s3);
for(Student s : al){
System.out.println(s.getName()+"------"+s.getAge());
}
}
}
Set集合
1.概述
无序唯一
2. HashSet存储字符串并遍历
代码实现
package cn.itcast_day080901;
import java.util.HashSet;
import java.util.Set;
public class Test1 {
public static void main(String[] args) {
Set<String> set = new HashSet<String>();
set.add("张三");
set.add("李四");
set.add("王五");
set.add("赵六");
for(String s : set){
System.out.println(s);
}
}
}
}
打印结果:
赵六
张三
李四
王五
3. HashSet存储自定义对象保证元素唯一性
代码实现:
package cn.itcast_day080901;
import java.util.HashSet;
import cn.itcast_day0803.Student;
public class Test2 {
public static void main(String[] args) {
HashSet<Student> hm = new HashSet<Student>();
Student s1 = new Student("ALLEN",18);
Student s2 = new Student("Bob",18);
Student s3 = new Student("Bob",18);
Student s4 = new Student("Peter",18);
Student s5= new Student("Peter",17);
hm.add(s1);
hm.add(s2);
hm.add(s3);
hm.add(s4);
hm.add(s5);
for(Student s : hm){
System.out.println(s.getName()+"--------"+s.getAge());
}
}
}
打印结果
Peter--------17
Peter--------18
ALLEN--------18
Bob--------18
4. LinkedHashSet的概述和使用
LinkedHashSet的概述: 元素有序 , 并且唯一
代码实现
package cn.itcast_day080901;
import java.util.LinkedHashSet;
public class Test3 {
public static void main(String[] args) {
LinkedHashSet<String> lhs = new LinkedHashSet<String>();
lhs.add("hello");
lhs.add("world");
lhs.add("world");
lhs.add("java");
lhs.add("hello");
lhs.add("itcast");
for(String s : lhs){
System.out.println(s);
}
}
}
TreeSet集合
1. TreeSet集合的特点: 元素唯一,并且可以对元素进行排序
排序:
a: 自然排序
b: 使用比较器排序
到底使用的是哪一种的排序取决于,构造方法.
2. TreeSet存储Integer类型的元素并遍历
20 , 18 , 23 , 22 , 17 , 24, 19 , 18 , 24
代码实现:
package cn.itcast_day080901;
import java.util.TreeSet;
public class Test4 {
public static void main(String[] args) {
TreeSet<Integer> ts = new TreeSet<Integer>();
ts.add(10);
ts.add(18);
ts.add(23);
ts.add(22);
ts.add(17);
ts.add(24);
ts.add(19);
ts.add(18);
ts.add(24);
for(Integer in : ts){
System.out.print(in+” ”);
}
}
}
打印结果: 10 17 18 19 22 23 24
3. TreeSet集合这种排序分为两个方式:
第一种: 自然排序
第二种: 比较器排序
区分这两种排序方式,主要依赖于构造方法.
A 如果说构造方法是空参,那么使用的就是自然排序
要求元素必须实现Comparable接口,复写compareTo方法
B 如果使用的是有参的构造方法: public TreeSet(Comparator comparator) 使用的是比较器在进行比较.两种方式:
①第一种编写一个类,然后让这个类实现Comparator接口, 复写compare方法
②第二种使用匿名内部类的方式.
自然排序
1. public class TreeSetDemo {
public static void main(String[] args) {
// 创建一个TreeSet集合对象
TreeSet<Integer> ts = new TreeSet<Integer>() ;
// 添加元素
// 存储下列元素: 20 , 18 , 23 , 22 , 17 , 24, 19 , 18 , 24
ts.add(20) ;
ts.add(18) ;
ts.add(23) ;
ts.add(22) ;
ts.add(17) ;
ts.add(24) ;
ts.add(19) ;
ts.add(18) ;
ts.add(24) ;
// 遍历
for(Integer i : ts){
System.out.println(i);
}
}
}
2.
implements Comparable<Student>
public int compareTo(Student o) {
// 按照自定义对象的年龄进行排序
int num = this.age - o.age ;
// 比较姓名
int num2 = (num == 0) ? this.name.compareTo(o.name) : num ;
return num2;
比较器排序()
1.编写一个类,实现Comparator接口, 复写compare方法
public class MyComparator implements Comparator<Student>{
@Override
public int compare(Student s1, Student s2) {
// 比较姓名的长度
int num = s1.getName().length() - s2.getName().length() ;
// 比较姓名
int num2 = (num == 0) ? s1.getName().compareTo(s2.getName()) : num ;
// 比较年龄
int num3 = (num2 == 0) ? s1.getAge() - s2.getAge() : num2 ;
return num3;
}
2.匿名内部类
(见下面)
3.TreeSet存储自定义对象并遍历a按照年龄进行排序 b年龄就是主要条件c 次要条件就是姓名
public class TreeSetDemo {
public static void main(String[] args) {
// 创建TreeSet集合的对象
//TreeSet<Student> ts = new TreeSet<Student>(new MyComparator()) ;
// 使用匿名内部类改进
TreeSet<Student> ts = new TreeSet<Student>(new Comparator<Student>(){
@Override
public int compare(Student s1, Student s2) {
// 比较姓名的长度
int num = s1.getName().length() - s2.getName().length() ;
// 比较姓名
int num2 = (num == 0) ? s1.getName().compareTo(s2.getName()) : num ;
// 比较年龄
int num3 = (num2 == 0) ? s1.getAge() - s2.getAge() : num2 ;
return num3 ;
}
}) ;
// 创建自定对象
Student s1 = new Student("diaochan" , 23) ;
Student s2 = new Student("lvbu" , 25) ;
Student s3 = new Student("dongzhuo" , 41) ;
Student s4 = new Student("caocao" , 23) ;
Student s5 = new Student("liubei" , 25) ;
Student s6 = new Student("liubei" , 25) ;
Student s7 = new Student("liubei" , 27) ;
// 把元素添加到集合中
ts.add(s1) ;
ts.add(s2) ;
ts.add(s3) ;
ts.add(s4) ;
ts.add(s5) ;
ts.add(s6) ;
ts.add(s7) ;
// 遍历
for(Student s : ts){
System.out.println(s.getName() + "---" + s.getAge());
}
}
}
4.根据总分,语文,数学,英语成绩,姓名排序
public class TreeSetTest {
public static void main(String[] args) {
// 创建一个集合对象
TreeSet<Student> ts = new TreeSet<Student>(new Comparator<Student>() {
@Override
public int compare(Student s1, Student s2) {
// 比较总分
int num = s2.getTotalScore() - s1.getTotalScore() ;
// 语文成绩
int num2 = (num == 0) ? s2.getChineseScore() - s1.getChineseScore() : num ;
// 数学成绩
int num3 = (num2 == 0) ? s2.getMathScore() - s1.getMathScore() : num2 ;
// 英语成绩
int num4 = (num3 == 0 ) ? s2.getEnglishScore() - s1.getEnglishScore() : num3 ;
// 比较姓名
int num5 = (num4 == 0) ? s2.getName().compareTo(s1.getName()) : num4 ;
return num5;
}
}) ;
// 键盘录入3个学生信息
for(int x = 0 ; x < 3 ; x++){
// 创建键盘录入对象
Scanner sc = new Scanner(System.in) ;
System.out.println("请输入姓名: ");
String userName = sc.nextLine() ;
System.out.println("请输入语文成绩: ");
String chineseScoreStr = sc.nextLine() ;
System.out.println("请输入数学成绩: ");
String mathScoreStr = sc.nextLine() ;
System.out.println("请输入英语成绩: ");
String englishScoreStr = sc.nextLine() ;
// 创建Student对象
Student s = new Student() ;
s.setName(userName) ;
s.setChineseScore(Integer.parseInt(chineseScoreStr));
s.setMathScore(Integer.parseInt(mathScoreStr)) ;
s.setEnglishScore(Integer.parseInt(englishScoreStr)) ;
// 把对象添加到集合中
ts.add(s) ;
}
// 遍历
System.out.println("姓名\t\t总分\t\t语文\t\t数学\t\t英语") ;
for(Student s : ts){
System.out.println(s.getName() + "\t\t" + s.getTotalScore() + "\t\t" + s.getChineseScore() + "\t\t" + s.getMathScore() + "\t\t"+ s.getEnglishScore());
}
}
}