常用API大全

目录

目录

异常

@SneakyThrows

集合

HashMultiset

HashMap

computeIfPresent

computeIfAbsent

compute

putIfAbsent

工具类

Joiner拼接

StringUtils

ObjectUtils

NumberUtil

流转Set集合

hutool工具包 

StrUtil

StopWatch 


异常

@SneakyThrows

@SneakyThrows将异常转换为非受检异常(Unchecked Exception)并抛出

集合

HashMultiset

多重集合,和set唯一的不同是 set 集合中一个值只能出现一次,而multiset多重集合中一个值可以出现多次。一个典型的应用就是统计单词出现次数。eg:

public class MultiSetTest {
    public static void main(String[] args) {
        Multiset<String> multiset = HashMultiset.create();
        multiset.add("hello");
        multiset.add("hello");
        multiset.add("hello");
        multiset.add("world");
        multiset.add("world");
        multiset.add("world");
        System.out.println("hello count: "+multiset.count("hello"));
        System.out.println("world count: "+multiset.count("world"));
    }
}

hello count:3

world count::3

HashMap

computeIfPresent

computeIfAbsent

compute

putIfAbsent
key不存在,直接添加,putIfAbsent方法返回null;

key存在,不进行操作,putIfAbsent方法返回旧值。

public static void main(String[] args) {
  Map<Integer, String> map = new HashMap<>();
  // 初始化
  map.putIfAbsent(1, "a");
  map.putIfAbsent(2, "b");
  // 测试
  map.putIfAbsent(2, "c");// 不会被覆盖
  map.putIfAbsent(3, "d");
  System.out.println(map);
}
输出:{1=a, 2=b, 3=d}

工具类

Joiner拼接

Joiner.on("~").join("1", "2") 输出:1~2

StringUtils

StringUtils.defaultIfBlank(resp.getMsg(), "获取数据失败")
 为空返回默认参数值
String str = StringUtils.join(Arrays.asList(1, 2, 3), "-");
System.out.println(str);
 输出:1-2-3

ObjectUtils

ObjectUtils.defaultIfNull(orderPay.getAccount(), BigDecimal.ZERO);

NumberUtil

NumberUtil.div(skuDTO.getAmount(),skuDTO.getQuantity())

流转Set集合

private static final Set<String> EXZAP =Stream.of("aa","bb","cc").collect(Collectors.toSet());

hutool工具包 

StrUtil

含有各种符号,和一些额外功能

public static final char C_SPACE = ' ';
public static final char C_TAB = '\t';
public static final char C_DOT = '.';
public static final char C_SLASH = '/';
public static final char C_BACKSLASH = '\\';
public static final char C_CR = '\r';
public static final char C_LF = '\n';
public static final char C_UNDERLINE = '_';
public static final char C_COMMA = ',';
public static final char C_DELIM_START = '{';
public static final char C_DELIM_END = '}';
public static final char C_BRACKET_START = '[';
public static final char C_BRACKET_END = ']';
public static final char C_COLON = ':';
public static final char C_AT = '@';
public static final String TAB = "\t";
public static final String DOT = ".";
public static final String DOUBLE_DOT = "..";
public static final String SLASH = "/";
public static final String BACKSLASH = "\\";
public static final String CR = "\r";
public static final String LF = "\n";
public static final String CRLF = "\r\n";
public static final String UNDERLINE = "_";
public static final String DASHED = "-";
public static final String COMMA = ",";
public static final String DELIM_START = "{";
public static final String DELIM_END = "}";
public static final String BRACKET_START = "[";
public static final String BRACKET_END = "]";
public static final String COLON = ":";
public static final String AT = "@";
public static final String HTML_NBSP = "&nbsp;";
public static final String HTML_AMP = "&amp;";
public static final String HTML_QUOTE = "&quot;";
public static final String HTML_APOS = "&apos;";
public static final String HTML_LT = "&lt;";
public static final String HTML_GT = "&gt;";
public static final String EMPTY_JSON = "{}";

StopWatch 

StopWatch stopWatch = new StopWatch();
stopWatch.start("fetchProduct" + i.getId());
stopWatch.stop();
log.info("fetchOrder finish,店铺:{},耗时:{}", i.getId(), stopWatch.getTotalTimeSeconds());

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值