第十一章 泛型 ② 笔记
1.List和Map数据转存 0712
package com.test1;
/**
* @author: hy
* @create: 2022-07-12 09:27:23
*/
public class Student {
private String clsName;
private String stuName;
private int age;
public Student() {
}
public Student(String clsName, String stuName, int age) {
this.clsName = clsName;
this.stuName = stuName;
this.age = age;
}
public String getClsName() {
return clsName;
}
public void setClsName(String clsName) {
this.clsName = clsName;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"clsName='" + clsName + '\'' +
", stuName='" + stuName + '\'' +
", age=" + age +
'}';
}
}
package com.test1;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author: hy
* @create: 2022-07-12 09:28:18
*/
public class Test {
public static void main(String[] args) {
Student s1 = new Student("A01","张三",18);
Student s2 = new Student("A02","李四",13);
Student s3 = new Student("A03","王五",16);
Student s4 = new Student("A01","赵六",18);
Student s5 = new Student("A02","孙琦",19);
List<Student> studentList = new ArrayList<>();
studentList.add(s1);
studentList.add(s2);
studentList.add(s3);
studentList.add(s4);
studentList.add(s5);
for (Student s: studentList){
System.out.println(s);
}
System.out.println("========================");
//定义转存的容器
Map<String,String> map = new HashMap<>();
//遍历学生集合
for (int i=0;i<studentList.size();i++){
//获取遍历到的学生
Student s = studentList.get(i);
//将学生存储到map中
//先判断要添加的学生的班级,在map中是否已经存在
if (map.containsKey(s.getClsName())){
//取出已有的班级对应的学生信息
String oldName = map.get(s.getClsName());
//把之前的学生姓名和要添加的学生姓名,拼接到一起
String newName = oldName+" "+s.getStuName();
//重新放回map
map.put(s.getClsName(),newName);
}else{
//map没有存储过对应的班级
map.put(s.getClsName(),s.getStuName());
}
}
for (String key : map.keySet()){
System.out.println(key+" ---> "+map.get(key));
}
}
}
注意:
public static void main(String[] args) {
Student stu1 = new Student("A01", "zhangsan", 22);
Student stu2 = new Student("A01", "lisi", 23);
Student stu3 = new Student("A01", "wangwu", 24);
Student stu4 = new Student("A02", "zhaoliu", 25);
Student stu5 = new Student("A03", "laoyang", 21);
List <Student> list = new ArrayList<>();
list.add(stu1);
list.add(stu2);
list.add(stu3);
list.add(stu4);
list.add(stu5);
for(Student stux:list){
System.out.println(stux);
}
System.out.println("====================");
Map<String,String> map=new HashMap<>(); //map中存放的是键值对
/*//键所对应的值会出现覆盖的情况
for(int i=0;i<list.size();i++){
Student stui=list.get(i);
map.put(stui.getBanji(),stui.getName());
}
System.out.println(map);*/
for(int i=0;i<list.size();i++){
Student stui=list.get(i);
//判断班级信息是否已存在
if(map.containsKey(stui.getBanji())){
String old=map.get(stui.getBanji()); //获得班级已存在的学生姓名(通过班级 --键值对-->姓名)
String newo=old+" "+stui.getName(); //现在的名字(被拼接)
map.put(stui.getBanji(),newo);
}else{
map.put(stui.getBanji(),stui.getName()); //键值对 键(班级)对应值(姓名)
}
}
for(String key:map.keySet()){
System.out.println(key+"---->"+map.get(key));
}
}
2.泛型
package com.test4;
/**
* @author: hy
* @create: 2022-07-12 11:04:55
*/
public class Position<T> {
private T x;
private T y;
public Position() {
}
public Position(T x, T y) {
this.x = x;
this.y = y;
}
public T getX() {
return x;
}
public void setX(T x) {
this.x = x;
}
public T getY() {
return y;
}
public void setY(T y) {
this.y = y;
}
@Override
public String toString() {
return "Position{" +
"x=" + x +
", y=" + y +
'}';
}
}
package com.test4;
/**
* @author: hy
* @create: 2022-07-12 11:05:27
*/
public class Test {
public static void main(String[] args) {
//使用泛型:可以灵活设置为各种类型
//整数坐标
Position<Integer> position = new Position<Integer>(10,100);
System.out.println(position);
//小数坐标
Position<Double> position1 = new Position<Double>(1.234,4.234);
System.out.println(position1);
//地理位置坐标
Position<String> position2 = new Position<String>("东经87","北纬37.5");
System.out.println(position2);
//当没有为泛型类定义类型的时候,默认用Object来定义
Position p = new Position(12,"abc");
}
}
3.泛型擦除
package com.test5;
import java.util.ArrayList;
import java.util.List;
/**
* @author: hy
* @create: 2022-07-12 11:15:26
*/
public class Test {
public static void main(String[] args) {
//java中的泛型是伪泛型
//泛型擦除
List<Integer> list = new ArrayList<Integer>();
list.add(100);
list.add(200);
//list.add("abc");
//泛型擦除
List list2 = list;
list2.add("abc");
list2.add("dbc");
System.out.println(list2);
}
}
4.泛型上/下限测试
package com.test6;
/**
* 泛型类
* @author: hy
* @create: 2022-07-12 11:33:26
*/
public class Info<T> {
//属性
private T var;
public Info() {
}
public Info(T var) {
this.var = var;
}
public T getVar() {
return var;
}
public void setVar(T var) {
this.var = var;
}
@Override
public String toString() {
return "Info{" +
"var=" + var +
'}';
}
}
package com.test6;
/**
* @author: hy
* @create: 2022-07-12 11:34:38
*/
public class Test {
public static void main(String[] args) {
//测试泛型对象的传值问题
// Info<String> info = new Info<>("abc");
// test3(info);
//
// Info<Integer> info2= new Info<>(123);
// test3(info2);
//泛型上限测试
// Info<Double> info = new Info<>(123.23);
// test4(info);
//
// Info<Integer> info2= new Info<>(123);
// test4(info2);
//泛型下限测试
Info<String> str1 = new Info<>("abc");
test5(str1);
Info<Object> str2 = new Info<>("abc");
test5(str2);
}
//受限泛型: <? super String> 设置泛型下限,表示具体类型必须是String或者String的父类
public static void test5(Info<? super String> info){
System.out.println(info);
}
//受限泛型: <? extends Number> 设置泛型上限。表示具体类型必须是Number或Number的子类
public static void test4(Info<? extends Number> info){
System.out.println(info);
}
//Info<?>: ?泛型通配符,可以接受任意类型
public static void test3(Info<?> info){
System.out.println(info);
//使用泛型通配符只能访问,不能设值
//info.setVar("abc");
}
//Info info:这种方式会擦除类型,不符合要求
//Info<Object> 和 Info<String>是完全不同的类型,没有父子关系
// public static void test2(Info<Object> info){
// System.out.println(info);
// }
public static void test1(Info<String> info){
System.out.println(info);
}
}
5.泛型方法
package com.test7;
/**
* @author: hy
* @create: 2022-07-12 11:53:53
*/
public class MyCls{
//泛型方法:设置参数可以接受任何类型
public <T> void test(T t){
System.out.println(t);
}
//有返回值的泛型方法
public <T> T test2(T t){
System.out.println(t);
return t;
}
}
package com.test7;
/**
* @author: hy
* @create: 2022-07-12 11:54:48
*/
public class Test {
public static void main(String[] args) {
MyCls myCls = new MyCls();
myCls.test(100);
myCls.test("abc");
myCls.test(1.123);
int i = myCls.test2(100);
String n = myCls.test2("abc");
}
}
6.泛型接口
package com.test7;
/**
* 泛型接口
* @author: hy
* @create: 2022-07-12 11:58:19
*/
public interface IA<T>{
void test(T t);
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/118119.html