2021-05-16

1.建文件夹tools
建Java Demo01

public class Demo01 {
    public static void main(String[] args) {
        String str[] = { "中文", "计算机", "ABC", "123", "[email protected]" };
        List list = Arrays.asList(str); // 1 Arrays.asList() 将数组转换成列表
        System.out.println(list);

        // 2 将数组转换成数组列表(链式列表)  new ArrayList<>(Arrays.asList(str))
        ArrayList alist = new ArrayList<>(Arrays.asList(str));
        LinkedList llist = new LinkedList<>(Arrays.asList(str));
        alist.remove("ABC");
        llist.add("1321123");
        System.out.println(alist);
        System.out.println(llist);

        // 3 将列表转换成数组  列表.toArray();
        Object[] str2 = alist.toArray();
        for ( Object o : str2 ) {
            System.out.print(o +" ");
        }
        System.out.println();
        Object[] str3 = llist.toArray();
        for ( Object o : str3 ) {
            System.out.print(o + " ");
        }


    }
}

建Java Demo02

public class Demo02 {
    public static void main(String[] args) {
        String str[] = { "中文", "计算机", "ABC", "123", "[email protected]" ,"ABC" };
        // 1 将数组转换成集合 new HashSet<>(Arrays.asList(str));
        HashSet set = new HashSet<>(Arrays.asList(str));
        System.out.println(set);
        // 2 将集合再转换成数组  toArray();
        Object[] arr = set.toArray();
        for( Object a : arr ) {
            System.out.println(a);
        }


    }
}

建Java Demo03

public class Demo03 {
    public static void main(String[] args) {
        HashMap map = new HashMap<>();
        map.put("1", "A");
        map.put("2", "B");
        map.put("3", "C");
        // 1 keySet()
        Set keys = map.keySet();
        System.out.println(keys);
        // 2 把set转换成其他集合
        ArrayList alistkeys = new ArrayList<>(keys);
        System.out.println(alistkeys);
        // 3 取出map的值 value()
        Collection v = map.values();
        System.out.println(v);
        // 4 把值转换成其他的集合 new LinkedList<>(v)
        LinkedList llistv = new LinkedList<>(v);
        System.out.println(llistv);


    }
}

建Java Demo04

public class Demo04 {
    public static void main(String[] args) {
        HashMap map = new HashMap<>();
        for( int i = 0 ; i < 200000 ; i ++ ) {
            map.put(i , "haha"+ i ) ;
        }
        // 迭代器 减少资源占用
        Iterator> iterator = map.entrySet().iterator();
        while ( iterator.hasNext() ) {
            Map.Entry next = iterator.next();
            System.out.println(next);
        }





    }
}

建Java Demo05

public class Demo05 {
    public static void main(String[] args) {
        ArrayList nums = new ArrayList<>();
        nums.add(2);
        nums.add(0);
        nums.add(-5);
        nums.add(0);
        nums.add(3);

        // 1 排序 sort
        Collections.sort(nums);
        System.out.println(nums);
        // 2 反转 reverse
        Collections.reverse(nums);
        System.out.println(nums);
        // 3 混洗
        Collections.shuffle(nums);
        System.out.println(nums);
        // 4 求最大小值
        System.out.println(Collections.max(nums));
        System.out.println(Collections.min(nums));
        // 5 替换
        Collections.replaceAll(nums , 0 ,100);
        System.out.println(nums);
        // 6 求频率
        System.out.println(Collections.frequency(nums,100));
        // 7 二分搜索 ,使用的条件是集合是有序的
        Collections.sort(nums);
        System.out.println(nums);
        int i = Collections.binarySearch(nums, 3);
        System.out.println("位置" + i) ;




    }
}

你可能感兴趣的:(2021-05-16)