Java学习总结第三十天Java泛型(三)

本文介绍Java中泛型List与Map的具体应用案例,包括新闻标题管理、学生信息存储等场景,展示了如何利用泛型进行高效的数据操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

泛型List

如何定义泛型List对象:List<T>

List <Integer> numbers = new ArrayList<Integer>();

        for (int i = 1; i <= 49; i++){

            numbers.add(i);

        }

        Collections.shuffle(numbers);

        List<Integer> winningCombination =    numbers.subList(0, 6);

        Collections.sort(winningCombination);

        System.out.println(winningCombination);

Integer:存储Integer的List

 

泛型List接口和ArrayList类

新闻发布系统的存储对象,基于泛型实现

创建类型:新闻标题

包含属性: ID、名称、创建者、创建时间

public class FirstLevelTitle {

    private int id;

    private String titleName;

    private String creater;

    private Date createTime;

    public FirstLevelTitle(int id, String titleName, String creater,Date    createTime) {

        this.id = id;

        this.titleName = titleName;

        this.creater = creater;

        this.createTime = createTime;

    }

    public String getTitleName() { return titleName; }

    public void setTitleName(String titleName) {

        this.titleName = titleName;

    }

}

具体实现:按照顺序依次添加各类新闻标题、获取新闻标题的总数

public class FirstLevelTitleDB {

    public static void main(String[] args) {

        FirstLevelTitle car = new FirstLevelTitle(1, "汽车", "管理员", new Date());

        FirstLevelTitle test = new FirstLevelTitle(2, "高考", "管理员", new Date());

        List<FirstLevelTitle>  newsTitleList = new ArrayList<FirstLevelTitle>();

        newsTitleList.add(car);

        newsTitleList.add(test);

        System.out.println("新闻标题数目为:" + newsTitleList.size() + "条");…

    }

}

具体实现:根据位置获取相应新闻标题、逐条打印每条新闻标题的名称

public class FirstLevelTitleDB {

    public static void main(String[] args) {

        …

        System.out.println("新闻标题数目为:" + newsTitleList.size() + "条");

        print(newsTitleList);

    }

    public static void print(List<FirstLevelTitle>  newsList) {

        for (int i = 0; i < newsList.size(); i++) {

            FirstLevelTitle title = newsList.get(i);

            System.out.println(i + 1 + ":" + title.getTitleName());

        }

    }

}

 

基于foreach循环简化集合的输出

如何使用foreach循环简化代码?

public static void print(List<FirstLevelTitle>  newsList) {

    int i=0;

    for (FirstLevelTitle title : newsList) {

       System.out.println(i + 1 + ":" + title.getTitleName());

    }

}

 

List接口和ArrayList类

实现如下功能:

1、在指定的位置添加新闻标题

2、判断是否已经存储了某条新闻标题

3、删除指定位置处的某一新闻标题

FirstLevelTitle house = new FirstLevelTitle(3, "房产", "网站管理员", new Date());

List<FirstLevelTitle> newsTitleList = new ArrayList<FirstLevelTitle>();

newsTitleList.add(2,house);

if(newsTitleList.contains(test)) {

   System.out.println("有高考的新闻");

}

else{

   System.out.println("没有高考的新闻");

}

newsTitleList.remove(1);

 

List接口和LinkedList类

具体实现:添加头条、以及最末条新闻标题

FirstLevelTitle car = new FirstLevelTitle(1, "汽车", "管理员", new Date());

FirstLevelTitle medical = new FirstLevelTitle(2, "医学", "管理员",new Date());

LinkedList<FirstLevelTitle> newsTitleList = new LinkedList<FirstLevelTitle> ();

newsTitleList.addFirst(car);

newsTitleList.addLast(medical);

 

具体实现:获取头条、以及最末条新闻标题、删除头条、以及最末条新闻标题

LinkedList<FirstLevelTitle> newsTitleList = new LinkedList<FirstLevelTitle> ();

FirstLevelTitle first = newsTitleList.getFirst();

System.out.println("头条的新闻标题为:" + first.getTitleName());

FirstLevelTitle last = newsTitleList.getLast();

System.out.println("排在最后的新闻标题为:" + last.getTitleName());

newsTitleList.removeFirst();

newsTitleList.removeLast();

 

Map接口和HashMap类

确定存储对象

1、创建类型:AccpStudent

2、包含属性:中心名称、学员姓名

public class AccpStudent {

    private String name;// 学员姓名

    private String school;// 中心名称

    public AccpStudent(String name, String school) {

        this.name = name;

        this.school = school;

    }

    public String toString() {

       return school+"毕业的"+name;

    }

}

具体实现:

1、把英文名称与学员对象按照“键-值对”的方式存储在HashMap中

2、分别打印键集、值集、以及键-值对集合

AccpStudent student1 = new AccpStudent("李明", "西苑中心");

AccpStudent student2 = new AccpStudent("刘丽", "西苑中心");

Map<String,AccpStudent> students = new HashMap<String,AccpStudent>();

students.put("Jack", student1);

students.put("Rose", student2);

System.out.println("键集:"+students.keySet());

System.out.println("值集: "+students.values());

System.out.println("键-值对集合:"+students);

 

实现如下功能:

1、判断是否存在某个键,如果是,则根据键获取相应的值

2、根据键、删除某条记录

AccpStudent student1 = new AccpStudent("李明", "西苑中心");

AccpStudent student2 = new AccpStudent("刘丽", "西苑中心");

Map<String,AccpStudent> students = new HashMap<String,AccpStudent>();

students.put("Jack", student1);

students.put("Rose", student2);

String key = "Jack";

if(students.containsKey(key))

System.out.println(students.get(key));

students.remove(key);

System.out.println("键-值对集合:"+students);

 

Map<String, Employee> staff = new HashMap<String, Employee>();

      staff.put("144-25-5464", new Employee("Amy Lee"));

      staff.put("567-24-2546", new Employee("Harry Hacker"));

      staff.put("157-62-7935", new Employee("Gary Cooper"));

      staff.put("456-62-5527", new Employee("Francesca Cruz"));

      System.out.println(staff); // 输出全部数据

      staff.remove(“567-24-2546”); // 删除一个数据

      staff.put("456-62-5527", new Employee("Francesca Miller"));

      System.out.println(staff.get("157-62-7935"));

 

public class MapTest {

        public static void main(String[] args) {

               …

        // 循环

        for (Map.Entry<String, Employee>  entry : staff.entrySet()) {

              String key = entry.getKey();

              Employee value = entry.getValue();

              System.out.println("key=" + key + ", value=" + value);

        }

        …

         }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值