【JAVA常用工具类】Java工具类&常用工具类&不造轮子用轮子

Java常用工具类以及方法
Collection
将指定collection元素添加到collection中
boolean b =c1.addAll©
返回collection的哈希码值
int i = c.hashCode();
移除此collection中那些也包含在指定collection中的所有元素
boolean b7 = c1.removeAll©; //差集
保留此 collection 中那些也包含在指定 collection 的元素
boolean b8 = c1.retainAll©; //交集
返回包含此 collection 中所有元素的数组
Object[] arr = c.toArray();
返回在此collection的元素上进行迭代的迭代器
Iterator it = c.iterator();
while (it.hasNext()) { //判断是否有下一个元素
String s = it.next(); //取得下一个元素
System.out.println(s);//[小小]
}
排序:
Collection.sort(list) 升序 Collection.reverse(list)降序
获取最大值或最小值:
Collection.max(list) Collection.min(list)
返回集合大小
list.size()
返回一个集合的子视图
list.subList(start,end)//修改父集合会影响子视图
集合判空
list.isEmpty()
//判空不能只用isEmpty(),而是还要考虑空指针异常
(!list.isEmpty()&&list != null)
转换为线程安全集合
List integers = Collections.synchronizedList(list);//将ArrayList转换成线程安全集合
它的底层会创建SynchronizedRandomAccessList或者SynchronizedList类,这两个类的很多方法都会用synchronized加锁。
返回空集合
Collections.emptyList()
二分查找
int i = Collections.binarySearch(list, 3);//二分查找,返回的是一个索引
返回空列表
Collections.emptyList()
转换成不可修改集合
List integers = Collections.unmodifiableList(list);
CollectionUtils
判断集合是否为空
CollectionUtils.isEmpty(c1) //判断集合为空
CollectionUtils.isNOtEmpty(c1) //判断集合不为空
//注意CollectionUtils.isEmpty(null)—>false 所以在集合判空的时候需要判定是否空指针异常
对两个集合进行操作
如果想在数组上使用CollectionUtils,可以使用asList将数组转换为集合
String[] arrayA = new String[] { “A”, “B”, “C”, “D”, “E”, “F” };
String[] arrayB = new String[] { “B”, “D”, “F”, “G”, “H”, “K” };
List listA = Arrays.asList(arrayA);
List listB = Arrays.asList(arrayB);
获取并集
Collection unionList = CollectionUtils.union(list, list2);
获取交集
Collection intersectionList = CollectionUtils.intersection(list, list2);
获取交集的补集
Collection disjunctionList = CollectionUtils.disjunction(list, list2);
获取差集
Collection subtractList = CollectionUtils.subtract(list, list2);
判断集合是否相等
CollectionUtils.isEqualCollection(c,c1)
转换成不可修改的集合
Collection s = CollectionUtils.unmodifiableCollection©;
//获得一个集合的镜像,它的返回结果是不可直接被改变,否则会报错
StringUtils
static boolean contains(CharSequence str,CharSequence searchStr)
StringUtils是null安全的(如果输入参数String为null则不会抛出NullPointerException,而是做了相应处理,输入为null返回也是null)
判断字符串是否为空,为空的标准是str.length() ==0 或者 str == null
//判断字符串为空
public static boolean isEmpty(String str)
//判断字符串不为空
public static boolean isNotEmpty(String str)
判断某字符串是否为空或长度为0或由空白符(whitespace)构成
public static boolean isBlank(String str)
public static boolean isNotBlank(String str)

去掉字符串两端的控制符
//只是去掉控制符
public static String trim(String str) //包括空格,\b \t \n \f \r
//去掉控制符如果为空或者null就返回null
public static String trimToNull(String str)
//去掉控制符如果为空或者null就返回空串
public static String trimToEmpty(String str)

不区分大小写比较两个字符串是否相等,如果为空也认为相等
public static boolean equalsIgnoreCase(String str1, String str2)
返回字符在字符串中第一次出现的位置
//返回字符 searchChar 在字符串 str 中第一次出现的位置。如果 searchChar 没有在 str 中出现则返回
//-1,如果 str 为 null 或 “” ,则也返回-1
public static int indexOf(String str, char searchChar)
//字符在另一个字符串的startPos开始第一次出现的位置
public static int indexOf(String str, char searchChar, int startPos)
//字符串在另一个字符串第一次出现的位置
public static int indexOf(String str, String searchStr)
//字符串在另一个字符的startPos开始第一次出现的位置
public static int indexOf(String str, String searchStr, int startPos)
//返回字符串searchStr在字符串str中第ordinal次出现的位置
public static int ordinalIndexOf(String str, String searchStr, int ordinal)
返回字符在字符串中最后一次出现的位置(用法同上)
public static int lastIndexOf(String str, char searchChar)
public static int lastIndexOf(String str, char searchChar, int startPos)
public static int lastIndexOf(String str, String searchStr)
public static int lastIndexOf(String str, String searchStr, int startPos)

  • static String center(String str,int Size,char padChar):该方法将扩大字符串的长度,如果str的长度小于给定的Size,那么将把str放在新的字符串中间,并在左右用padChar填充,同理还有但对对左或右进行填充的leftPad(String str,int size,char padChar)或rightPad(String str,int size,char padChar)。
  • static String deleteWhitespace(String str):删除str中的whitespace,whitespace指的是Character.isWhitespace(char)定义的字符。
  • static String difference(String str1,String str2):以str1作为源字符,str2作为比较字符,返回str2不在str1中的字符。
  • static boolean endsWith(CharSequence str,CharSequence suffix):检查str是否以suffix作为结尾,并返回结果,如果两个str和suffix都为null的话讲返回true。同理还有static boolean startsWith(CharSequence str,CharSequence preffix)检查前缀字符。
  • static boolean isAlpha(final CharSequence cs):检查该字符序列是否都是字母
  • static boolean isAlphanumeric(final CharSequence cs):检查该字符序列是否只包含数字或字母。
  • static String replace(String text,String searchStr,String replacement,int n):该方法用于将text中的searchStr替换为replacement,从前往后一共替换n次。
  • static String reverse(String str):用于字符串的反转,用的是StringBuilder的reverse()方法
  • static String[] split(String str,String separator,int max):将给定的字符串str按照separator进行分割,最多分割max次,并返回分割后的数组。
  • static String strip(String str,String stripChars):去掉字符串str前后的stripChars
  • static String swapCase(String str):将字符串str中的大写换成小写,小写换成大写。

Joda-Time
创建Joda-Time对象

  • 无参构造函数(获取系统时间)
    val date = new DateTime()

  • Joda可以精确地控制创建DateTime对象的方式,该对象表示时间上的某个特定的瞬间
    val dateTime = new DateTime(
    2000, //year
    1, // month
    1, // day
    0, // hour
    0, // minute
    0, // second
    0 // milliseconds
    )

  • 通过时间戳来创建 val date = new DateTime(1553677539000l)

  • joda支持使用许多其他对象作为构造函数的参数,用于创建DateTime
    // 使用已有的Calendar类
    var dateTime = new DateTime(calendar)

    // 使用另外一个 Joda DateTime
    dateTime = new DateTime(anotherDateTime)

    // 使用字符串 (必须按照以下格式)
    var timeString = “2019-03-26T13:30:00-06:00”
    dateTime = new DateTime(timeString)

timeString = “2019-03-26”
dateTime = new DateTime(timeString)

LocalDate

  • LocalDate:该类封装了一个年/月/日的组合。当时区变得不重要时,使用它存储日期将非常方便。
    (例如,某个特定对象的出生日期 可能为 1999 年 4 月 16 日,在保存所有业务值的同时不会了解有关此日期的任何其他信息(比如这是一周中的星期几,或者这个人出生地所在的时区)。在这种情况下,应当使用 LocalDate。)
  • 只需要年月日的时候
    val localDate = new LocalDate(2018, 9, 6)
  • 只需要时分秒的时候
    val localTime = new LocalTime(13, 30, 26, 0)//13:30:26
    Joda可以很轻易的转换成Java Date对象和Calendar对象(通过 dateTime.getMillis()得到时间戳作为介质)

日期计算
val dt = new DateTime()
//昨天
val yesterday = dt.minusDays(1)
//明天
val tomorrow = dt.plusDays(1)
//1个月前
val before1month = dt.minusMonths(1)
//3个月后
val after3month = dt.plusMonths(3)
//2年前
val before2year = dt.minusYears(2)
//5年后
日期格式化
val dateTime = DateTime.now
dateTime.toString(“MM/dd/yyyy hh:mm:ss.SSSa”)
dateTime.toString(“dd-MM-yyyy HH:mm:ss”)
dateTime.toString(“EEEE dd MMMM, yyyy HH:mm:ssa”)
dateTime.toString(“MM/dd/yyyy HH:mm ZZZZ”)
dateTime.toString(“MM/dd/yyyy HH:mm Z”)
//输出
09/06/2018 02:30:00.000PM
06-Sep-2018 14:30:00
Sunday 06 September, 2018 14:30:00PM
09/06/2018 14:30 America/Chicago
09/06/2018 14:30 -0500

Guava
Guava工程包含了若干被Google的 Java项目广泛依赖 的核心库,例如:集合 collections 、缓存 caching 、原 生类型支持 primitives support 、并发库 concurrency libraries 、通用注解 common annotations 、字符 串处理 string processing 、I/O 等等。
使用guava的优点
1、 不可变集合:用不变的集合进行防御性编程和性能提升。
2 、新集合类型: multisets, multimaps, tables,等
3、 强大的集合工具类:提供java.util.Collections中没有的集合工具
4 、扩展工具类:让实现和扩展集合类变得更容易,比如创建Collection的装饰器,或实现迭代器
java 第三方库Guava的基本使用_心静自然无的博客-CSDN博客_guava 使用

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

子陌.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值