加入收藏 | 设为首页 | 会员中心 | 我要投稿 北几岛 (https://www.beijidao.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Collections工具类

发布时间:2021-05-20 09:45:24 所属栏目:大数据 来源: https://blog.csdn.net/summoxj
导读:import java.util.Collections; import java.util.List; import java.util.ArrayList; public class CollectionsDemo { ?? ?public static void main(String[] args) { ?? ??? ?function_1(); ?? ?} ?? ? ?? ?/* ?? ? * Collections.sort静态方法(只针对List
import java.util.Collections;
import java.util.List;
import java.util.ArrayList;

public class CollectionsDemo {
?? ?public static void main(String[] args) {
?? ??? ?function_1();
?? ?}
?? ?
?? ?/*
?? ? * Collections.sort静态方法(只针对List有效)
?? ? * 对于List集合,进行升序排列
?? ? */
?? ?public static void function(){
?? ??? ?//创建List集合
?? ??? ?List<String> list=new ArrayList<String>();
?? ??? ?list.add("qeqwe");
?? ??? ?list.add("dfsf");
?? ??? ?list.add("vxb");
?? ??? ?list.add("Iiuyiy");
?? ??? ?System.out.println(list);
?? ??? ?//调用集合工具类的方法sort
?? ??? ?Collections.sort(list);? //按照字母顺序排序(大写在最前面,ascii码更小)
?? ??? ?System.out.println(list);
?? ?}
?? ?
?? ?/*
?? ? * Collections.binarySearch静态方法
?? ? * 对List集合进行二分搜索,方法参数,传递List集合,传递被查找的元素
?? ? */
?? ?public static void function_1(){
?? ??? ?List<Integer> list=new ArrayList<Integer>();
?? ??? ?list.add(1);
?? ??? ?list.add(5);
?? ??? ?list.add(8);
?? ??? ?list.add(10);
?? ??? ?list.add(15);
?? ??? ?list.add(20);
?? ??? ?System.out.println(list);
?? ??? ?//调用工具类静态方法binarySearch
?? ??? ?int index=Collections.binarySearch(list,15);
?? ??? ?System.out.println(index);
?? ?}
?? ?
?? ?/*
?? ? * Collections.shuffle方法
?? ? * 对List集合中的元素,进行随机排列
?? ? */
?? ?public static void function_2(){
?? ??? ?List<Integer> list=new ArrayList<Integer>();
?? ??? ?list.add(1);
?? ??? ?list.add(5);
?? ??? ?list.add(9);
?? ??? ?list.add(11);
?? ??? ?list.add(8);
?? ??? ?list.add(10);
?? ??? ?list.add(15);
?? ??? ?list.add(20);
?? ??? ?System.out.println(list);
?? ??? ?//调用工具类方法shuffle对集合随机排列
?? ??? ?Collections.shuffle(list);
?? ??? ?System.out.println(list);
?? ?}
}

(编辑:北几岛)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读