集合练习专题

第一题 

public static void main(String[] args) {
        ArrayList arrayList = new ArrayList<>();
        arrayList.add(new News(" 新冠确诊病例超千万,数百万印度教信徒赴恒河\"圣浴\"引民众担忧"));
        arrayList.add(new News("男子突然想起2个月前钓的鱼还在网兜里,捞起一看赶紧放生"));
        //倒序遍历

        for (int i = arrayList.size() - 1; i >= 0; i--) {
            News news=(News) arrayList.get(i);
            System.out.println(processTitle(news.getTitle()));
        }
    }
    //处理显示新闻标题
     public static String processTitle(String title){
        if (title==null){
            return  "";
        }if (title.length()>15){
            return title.substring(0,15)+"..."; //取前面15个
         }else {
            return title;
         }
     }

class  News{
    private String content;
    private String title;

    public News(String title) {
        this.title = title;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    @Override
    public String toString() {
        return "title='" + title + '\'';
    }
}

运行结果:

第二题

public static void main(String[] args) {
        ArrayList arrayList = new ArrayList<>();
        //添加元素
        arrayList.add(new Car("宝马",400000));
        arrayList.add(new Car("宾利",5000000));
        System.out.println(arrayList);
        //删除指定的元素
        arrayList.remove(1);
        System.out.println(arrayList);
        //查看元素是否存在
        System.out.println(arrayList.contains(1));
        //获取元素个数
        System.out.println("元素个数为:"+arrayList.size());
        //判断集合是否为空
        System.out.println(arrayList.isEmpty());
        //清空
//        arrayList.clear();
        //添加多个元素
        arrayList.addAll(arrayList);
        System.out.println(arrayList);
        //查找多个元素是否存在
        arrayList.containsAll(arrayList);
         //删除多个元素
//        arrayList.removeAll(arrayList);
        //遍历
        System.out.println("==========增强for==========");
        for (Object A:arrayList){
            System.out.println(A);
        }
        System.out.println("==========迭代器==========");
        Iterator iterator = arrayList.iterator();
        while (iterator.hasNext()){
            Object next=iterator.next();
            System.out.println(next);
        }
    }
class  Car{
    private String name;
    private  double price;

    public Car(String name, double price) {
        this.name = name;
        this.price = price;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "name='" + name + '\'' +
                ", price=" + price;
    }
}

运行结果:

第三题 

 public static void main(String[] args) {
        HashMap Map=new HashMap<>();
        Map.put("jack",650);
        Map.put("tom",1200);
        Map.put("smith",2900);
        System.out.println(Map);
        Map.replace("jack",2600);
        System.out.println(Map);
        //所有人加100
        Set KeySet=Map.keySet();
        for (Object key:KeySet){
            Map.put(key,(Integer)Map.get(key)+100);
        }
        System.out.println(Map);
        //遍历集合中所有的员工
        Set set = Map.entrySet();
        Iterator iterator = set.iterator();
        while (iterator.hasNext()){
            Map.Entry next=(Map.Entry)iterator.next();
            System.out.println(next.getKey()+"-----"+next.getValue());
        }
        //遍历集合中所有的工资
        Collection values = Map.values();
        System.out.println(values);
    }
class Employee{
    private String name;
    private double salary;

    public Employee(String name, double salary) {
        this.name = name;
        this.salary = salary;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }

    @Override
    public String toString() {
        return  name + '\'' +
                "-----" + salary+"元";
    }
}

运行结果:

第四题

第五题


解决方法:Person 去实现 Comparable 接口

第六题 

第七题

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

╭⌒心岛初晴

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值