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<String> 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<String,String> 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;
}
7. mybatis_plus resultMap使用
<resultMap id="groupUserMap" type="com.v.im.user.entity.ImGroup">
<result column="id" property="id"/>
<result column="name" property="name"/>
<collection property="userList" ofType="com.v.im.user.entity.ImUser">
<result column="u_id" property="id"/>
<result column="u_name" property="name"/>
<result column="u_avatar" property="avatar"/>
<result column="u_sign" property="sign"/>
<result column="u_mobile" property="mobile"/>
<result column="u_email" property="email"/>
<result column="u_address" property="address"/>
<result column="u_sex" property="sex"/>
</collection>
</resultMap>
<select id="getUserFriends" resultMap="groupUserMap" parameterType="string">
SELECT
g.id AS id,
g.name AS name,
u.id AS u_id,
u. NAME AS u_name,
u.sign AS u_sign,
u.avatar AS u_avatar,
u.mobile AS u_mobile,
u.email AS u_email,
u.address as u_address,
u.sex as u_sex
FROM
(
SELECT
friend_id AS user_id,
user_group_id AS group_id
FROM
im_user_friend
WHERE
user_id = #{userId}
UNION ALL
SELECT
user_id AS user_id,
friend_group_id AS group_id
FROM
im_user_friend
WHERE
friend_id = #{userId}
) ug
JOIN im_user u ON u.id = ug.user_id
JOIN im_group g ON g.id = ug.group_id
<where>
<if test="name!=null and name.trim()!=''">
u.name like concat('%',concat(#{name},'%'))
</if>
</where>
</select>