常用代码块

1. 枚举单例模式

public class User {

    private User(){}

    static enum SingletonEnum{

        INSTANCE;
        private User user;

        private SingletonEnum(){
            user = new User();
        }

        public User getInstance(){
            return user;
        }
    }

    public static User getInstance(){
        return SingletonEnum.INSTANCE.getInstance();
    }

    public static void main(String[] args) {
        System.out.println(User.getInstance());
        System.out.println(User.getInstance());
        System.out.println(User.getInstance() == User.getInstance());
    }

}

2. 判断集合是否为空

List stringList = new ArrayList<>();

//为空返回true
CollectionUtils.isEmpty(stringList)

3. filter判断对象是否为空(方法的引用)

listName.stream().filter(Objects::nonNull).collect(Collectors.toList())

4. 判断map集合是否为空

MapUtils.isEmpty(mapName)

5. 生成指定范围内的无重复的随机数组

package suishen.wlwy.utils;

import java.util.Random;

public class NumberUtil {

    /**
     * 根据输入的范围随机产生指定的数量的不重复整数
     * @param min 下限值
     * @param max 上限值
     * @param n 个数
     * @return int[]
     */
    public static int[] randomArray(int min,int max,int n){
        int len = max-min+1;
        if(max < min || n > len){
            return null;
        }
        //初始化给定范围的待选数组
        int[] source = new int[len];
        for (int i = min; i < min+len; i++){
            source[i-min] = i;
        }
        int[] result = new int[n];
        Random rd = new Random();
        int index = 0;
        for (int i = 0; i < result.length; i++) {
            //待选数组0到(len-2)随机一个下标
            index = Math.abs(rd.nextInt() % len--);
            //将随机到的数放入结果集
            result[i] = source[index];
            //将待选数组中被随机到的数,用待选数组(len-1)下标对应的数替换
            source[index] = source[len];

        }
        return result;
    }

    public static void main(String[] args) {
        int[] ints = randomArray(0, 9, 10);
        if (ints != null) {
            for (int i : ints) {
                System.out.println(i);
            }
        }
    }

}

6. java HttpURLConnection 发送post请求, 传递json参数

    public static Map SMS(String phone)  {

//        {
//            "phone": "11111111111", 
//            "channel": ""
//        } 


        String PostData = "{" + "\"phone\"" + ":"+ "\"" + phone + "\"" + ","
                + "\"channel\"" +":"+ "\"" + CHANNEL + "\"" + "}";
        System.out.println(PostData);
        String result = "";
        BufferedReader reader = null;
        try {
            URL url = new URL(URL);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("POST");
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setUseCaches(false);
            conn.setRequestProperty("Connection", "Keep-Alive");
//            conn.setRequestProperty("Charset", "UTF-8");
            // 设置文件类型:
            conn.setRequestProperty("Content-Type","application/json; charset=UTF-8");
            // 设置接收类型否则返回415错误
            //conn.setRequestProperty("accept","*/*")此处为暴力方法设置接受所有类型,以此来防范返回415;
            //conn.setRequestProperty("accept","application/json");
            // 往服务器里面发送数据

            byte[] writebytes = PostData.getBytes();
            // 设置文件长度
            conn.setRequestProperty("Content-Length", String.valueOf(writebytes.length));
            OutputStream outwritestream = conn.getOutputStream();
            outwritestream.write(PostData.getBytes());
            outwritestream.flush();
            outwritestream.close();


            if (conn.getResponseCode() == 200) {
                reader = new BufferedReader(
                        new InputStreamReader(conn.getInputStream()));
                result = reader.readLine();
            }
            System.out.println(result);
            Map map = JSON.parseObject(result, Map.class);
            
            return map;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;

    }

 

你可能感兴趣的:(java)