Java标准类库util里的Math类与集合Collection的常用方法

package com.qimeng.javabase.schoolbook;

import java.util.*;

public class FastReivw {
    public static void main(String[] args) {

//        long times = 1_000_000_000;//优点:有多少位,一目了然
//        times(times);

//        copyArr();

//        javaMath();

        javaCollection();
    }

    static void times(long counts) {
        long start, end;
        //【*】System.currentTimeMillis():1970/1/1午起时到系统当前时间(毫秒)
        start = System.currentTimeMillis();
        for (int i = 0; i < counts; i++) {
            end = 0;
        }
        end = System.currentTimeMillis();
        System.out.println("所耗费时长为" + (end - start) + "毫秒");
    }

    static void copyArr() {
        byte a[] = {65, 66, 67, 68, 69, 70, 71};
        byte b[] = {88, 88, 88, 88, 88, 88, 88, 88, 88, 88};
        System.out.println("a = " + new String(a));
        System.out.println("b = " + new String(b));
        //【*】System.arraycopy():复制数组,参数自行参考提示
        System.arraycopy(a, 0, b, 0, a.length);//b从0位开始向a的0位开始替换进去b中a.length这么多个的字符
        System.out.println("a = " + new String(a));
        System.arraycopy(b, 5, a, 0, 4);
        System.out.println("a = " + new String(a));

    }

    static void javaMath() {
        //【*】Math类和Random类
        double radians = 2 * Math.PI;//PI => Π ; Math.E = 2071828182 :ln是以e为底的对数,
        // 即底数为e,e是自然常数,约等于2.71828,在一般的计算中不要求算出具体数值。
        double d1 = 3.14159265;
        System.out.println("ceil :" + Math.ceil(d1));//向上取整
        //Returns the closest long to the argument, with ties rounding to positive infinity.(转换为long类型)
        System.out.println("Round :" + Math.round(d1));//四舍五入
        System.out.println("floor :" + Math.floor(d1));//向下取整
        System.out.println("exp :" + Math.exp(d1));//指数函数(y1=3^4 ,y2=3^5)???
        System.out.println("pow :" + Math.pow(Math.E, d1));//指数函数,返回以E为底,d1为指数的幂值
        System.out.println("sin :" + Math.sin(Math.PI / 6));//三角函数
        System.out.println("asin :" + Math.asin(0.5));
        System.out.println("log :" + Math.log(Math.E));//对数函数
        System.out.println("角度转弧度toDegress :" + Math.toDegrees(radians));//???
        System.out.println("random :" + Math.random());//返回[0-1]的一个double类型数据

        //Random的使用
//        Random r = new Random();//实例化
//        r.nxet...(根据提示便知道都有哪些了)

    }

    static void javaCollection() {
        /*【*】Java集合类:Set(里面的东西无序,不可重复),List(有序,可重复),Map
        相关的接口有Collection、List、Set、SortSet(继承Set,对Set进行排序)*/

        //实现list接口的类
        //ArrayList类支持随需要而增长的动态数组
        ArrayList al = new ArrayList();//创建一个list
        System.out.println("size:" + al.size());
        al.add("A");//仅支持加一个(在末尾加)
        al.add("B");
        al.add("C");
        al.add(0, "1");//指定位置加
        System.out.println("size:" + al.size());
        System.out.println("内容:" + al);
        al.remove("C");//删除指定元素
        al.remove(1);//删除指定位置元素
        System.out.println("size:" + al.size());
        System.out.println("内容:" + al);
        //toArray():转化为数组
        Object[] arr = al.toArray();
        for (Object ob : arr) {
            System.out.print(ob + " ");
        }

        //Vector与ArrayList非常像,特点是:是同步的(多个线程同时访问,较为安全),性能稍差
        //Stack继承Vector:帮助实现栈的功能,函数有(empty(判空),peek(查看栈顶对象)
        // ,pop(出栈),push(入栈),search(返回某对象在栈中位置,以1为基数))

        System.out.println("\n===============================================");

        //Set接口
        //HashSet:里面的元素散列存储,无序
        //TreeSet:树结构存储,对象按升序存储(因为是有序的,所以检索效率高)
        TreeSet ts = new TreeSet();
        HashSet hs = new HashSet();
        Random r = new Random();
        for (int i = 0; i < 10; i++) {
            char c = (char) ('A' + r.nextInt(25));/*随机生成某个字母( 因为‘A’用的是单引号,所以不是字符串,
            +号不会进行字符串拼接运算 )*/
            ts.add(c);
            hs.add(c);
        }
        System.out.println("HashSet: " + hs);
        System.out.println("TreeSet: " + ts);
        //操作
        System.out.println(ts.lower('T'));//求小于给定元素的最大元素
        System.out.println(ts.pollFirst());//移除第一个(最低)元素
        System.out.println("TreeSet: " + ts);

        hs.retainAll(ts);//保留与ts相同的元素
        System.out.println("HashSet: " + hs);


        System.out.println("\n===============================================");
        //迭代器iterator(依次访问集合中的元素最简便的方法)
        ArrayList alist = new ArrayList();
        for (int i = 0; i < 10; i++) {
            char c = (char) ('A' + r.nextInt(25));/*随机生成某个字母( 因为‘A’用的是单引号,所以不是字符串,
            +号不会进行字符串拼接运算 )*/
            alist.add(c);
        }
        System.out.println("原列表内容:");
        /*每个Collection类都提供一个iterator()方法,返回一个类集合的迭代器*/
        Iterator itr = alist.iterator();
        while (itr.hasNext())//遍历集合
        {
            Object element = itr.next();//获取集合里的一个元素
            System.out.print(element + " ");
        }
        System.out.println();
        /*对继承List接口的集合可用listIterator()来获得迭代接口,优点是可以向前或向后访问,且可以修改元素*/
        ListIterator litr = alist.listIterator();
        while (litr.hasNext()) {
            Object element = litr.next();
            //toString():Returns a string representation of the object.返回对象的字符串表示形式。
            litr.set(element.toString().toLowerCase());
        }
        System.out.println("修改后前向遍历列表:");
        itr = alist.iterator();
        while (itr.hasNext())//遍历集合
        {
            Object element = itr.next();//获取集合里的一个元素
            System.out.print(element + " ");
        }
        System.out.println("\n后向遍历:");
        while (litr.hasPrevious())//遍历集合
        {
            Object element = litr.previous();//获取集合里的一个元素
            System.out.print(element + " ");
        }

        System.out.println("\n===============================================");

        //映射(map)是一个存储关键字/值(和在一起称项) 对的集合,关键字与值之间是一对多的关系
        HashMap hm = new HashMap();
        //把元素加入映射
        hm.put("Alisi", r.nextInt(1000));
        hm.put("Daming", r.nextInt(1000));
        hm.put("Zhangsan", r.nextInt(1000));
        hm.put("Lisi", r.nextInt(1000));
        //得到映射项的集合
        Set set = hm.entrySet();
        Iterator i = set.iterator();
        //显示元素
        while (i.hasNext())
        {
            Map.Entry me = (Map.Entry) i.next();//Map.Entry:描述映射中的项,是Map的一个内部接口
            System.out.println(me.getKey() + ":" + me.getValue());
        }
        //存400元到Lisi账户
        int balance = ((Integer) hm.get("Lisi")).intValue();//Returns the value of this Integer as an int.(拆箱)
        hm.put("Lisi", (int) (balance + 400));
        System.out.println("Lisi's balance:" + hm.get("Lisi"));

        //TreeMap类使用树实现Map接口,元素按照关键字升序排序
        //得到关键字列表:Set set = tm.keySet();   tm 是TreeMap的实例

    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值