Java 集合练习题

SourceURL:file:///home/windstorm/Documents/JAVA/JavaCoursePractise/Java 集合练习题.docx

答案:


import java.lang.reflect.Array;
import java.security.cert.CollectionCertStoreParameters;
import java.util.*;

public class Main {
    public static void main(String[] args) {
        News news = new News("新冠病毒大了faasdfsadlfjkhalwkjehf");
        News news1 = new News("吃了鱼以后,好吃“””asdfasfwefawsef");

         ArrayList arrayList =  new ArrayList();

         arrayList.add(news);
         arrayList.add(news1);

         Collections.reverse(arrayList);

         for(Object obj : arrayList)
         {
             String str = ((News)obj).getTitle();
             if(str.length()>15)
             {
                 str = str.substring(0,15);
                 str=str+"...";
                 ((News)obj).setTitle(str);
             }
             System.out.println(obj);

         }


    }
}

class News{
     private String title;
     private String content;

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

    }

    public String getTitle() {
        return title;
    }

    public String getContent() {
        return content;
    }

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

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

    @Override
    public String toString() {
        return  this.title;
    }
}

 

 SourceURL:file:///home/windstorm/Documents/JAVA/JavaCoursePractise/Java 集合练习题.docx

import java.lang.reflect.Array;
import java.security.cert.CollectionCertStoreParameters;
import java.util.*;

public class Main {
    public static void main(String[] args) {
        ArrayList<Object> arrayList = new ArrayList<>();
//        添加元素
        Car c1 = new Car("宝马",440000);
        arrayList.add(c1);
        arrayList.add(new Car("兵力",23423));

//        删除指定元素
        arrayList.remove(1);

//        查找元素是否存在
        System.out.println(arrayList.contains(c1));
//       获取arraylist元素的个数
        System.out.println(arrayList.size());

        System.out.println(arrayList.isEmpty());

        ArrayList arrayList1 = new ArrayList<>();
        arrayList1.add("good");
        arrayList1.add("morning");

//        添加多个元素到arraylist
        arrayList.addAll(arrayList1);
        System.out.println(arrayList);


//        查找多个元素是否存在
        System.out.println(arrayList.containsAll(arrayList1));

//        删除多个元素
        arrayList.removeAll(arrayList1);
        System.out.println(arrayList);

//        遍历arraylist
        for(Object obj : arrayList)
        {
            System.out.println(obj);
        }

//        清空所有元素
        arrayList.clear();
        arrayList1.clear();
        System.out.println(arrayList);
        System.out.println(arrayList1);
    }
}

class Car{
    private  String name;
    private  double price;

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

    @Override
    public String toString() {
        return  this.name +"\t"+ this.price;
    }
}

SourceURL:file:///home/windstorm/Documents/JAVA/JavaCoursePractise/Java 集合练习题.docx

import java.lang.reflect.Array;
import java.security.cert.CollectionCertStoreParameters;
import java.util.*;

public class Main {
    public static void main(String[] args) {
        HashMap<Object, Object> map = new HashMap<>();

        map.put("jack",650);
        map.put("tom",1200);
        map.put("smith",2900);

//        更改
        map.replace("jack",2600);
        System.out.println(map);
//    加薪
        Set set = map.keySet();

        for(Object obj : set)
        {
            map.replace(obj,(int)map.get(obj)+100);

        }
        System.out.println(map);

//        遍历员工
        for (Object obj:set)
        {
            System.out.println(obj);
        }
//        遍历工资
        Collection collection = map.values();

        for(Object obj: collection)
        {
            System.out.println(obj);
        }
    }
}

 

 

 SourceURL:file:///home/windstorm/Documents/JAVA/JavaCoursePractise/Java 集合练习题.docx

答:没有comparator


import java.lang.reflect.Array;
import java.security.cert.CollectionCertStoreParameters;
import java.util.*;

public class Main {
    public static void main(String[] args) {
        TreeSet<Object> treeSet = new TreeSet<>(
                new Comparator<Object>() {
                    @Override
                    public int compare(Object o, Object t1) {
                        if(((Person)o).getAge()>(((Person)t1).getAge()))
                        {
                            return  1;
                            
                        }
                        else
                        {
                            return 0;
                        }
                    }
                }
        );
        
        treeSet.add(new Person("googchan",22));
        
        
    }
}

class Person{
    private  String name;
    private  int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public int getAge() {
        return age;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值