标签:联合主键 可靠 isa started oob too 源码 equal 友好
Commons类库
commons: 一套开发源码、免费使用、商业友好的优秀API作为Java自带API的补充,大多数都是一些工具类 包括
可有效避免空指针异常
clone
出一个新数组,相比list.addAll()
不影响原数组// 有时我们需要将两个数组合并为一个数组,用ArrayUtils就非常方便,示例如下:
private static void testArr() {
String[] s1 = new String[] { "1", "2", "3" };
String[] s2 = new String[] { "a", "b", "c" };
String[] s = (String[]) ArrayUtils.addAll(s1, s2);
Stream.of(s).forEach(System.out::println);
String str = ArrayUtils.toString(s);
System.out.println(str + ">>" + str.length());
}
add,addAll,addFirst,clone,contains,copyArrayGrow1,getLength,hashCode,indexesOf,insert,isArrayIndexValid,isEmpty,isNotEmpty,isSameLength,isSorted,lastIndexOf,nullToEmpty,remove(针对index移除),removeAll(针多个index移除),removeAllOccurrences(针对值移除),removeElement(针对值移除第一次出现的值),removeElements(针对多个值移除第一次出现的值),reverse(逆转),shift(循环偏移),shuffle(随机排列),subarray,swap,toArray,toMap(遍历元素转换为map),toObject(将基本数据类型转为包装类),toPrimitive(将包装类转为基本类型),toString(可修改默认的‘{}’),toStringArray(每个元素toString)
StringUtils.substringAfter("SELECT * FROM PERSON ", "FROM").trim();
// 只能正整数.
StringUtils.isNumeric("454534"); //返回true
StringUtils.equals("454534", "454534")
StringUtils.equalsIgnoreCase("ABC534", "abc534")
//判断是否是空格和null
System.out.println(StringUtils.isBlank(" "));
List<String> list = Lists.newArrayList("a", "b", "c");
//将数组中的内容以,分隔
System.out.println(StringUtils.join(list, ","));
//在右边加下字符,使之总长度为6
System.out.println(StringUtils.rightPad("123", 6, ‘0‘));
//首字母大写
System.out.println(StringUtils.capitalize("abc"));
//删除所有空格
System.out.println( StringUtils.deleteWhitespace(" ab c "));
//判断是否包含这个字符
System.out.println( StringUtils.contains("abc", "ba"));
//表示左边两个字符
System.out.println(StringUtils.left("abc", 2));
3.ClassUtils
ClassUtils.getShortClassName(Test.class);
ClassUtils.getPackageName(Test.class);
4.NumberUtils
// 可以识别正负小数等 就别用stringutils里的isnumberic.
System.out.println(NumberUtils.toInt("a", 0));
System.out.println(NumberUtils.isNumber("-2.5"));
System.out.println(NumberUtils.isNumber("2.5"));
5.RandomStringUtils
// 字母加数字
System.out.println(RandomStringUtils.randomAlphanumeric(5));
// 字母
System.out.println(RandomStringUtils.randomAlphabetic(5));
// 数字
System.out.println(RandomStringUtils.randomNumeric(5));
6.StringEscapeUtils
// 转义反转义
System.out.println(StringEscapeUtils.escapeHtml("<html>"));
System.out.println(StringEscapeUtils.unescapeHtml("<html>"));
/** **OrderedMap**
得到集合里按顺序存放的key之后的某一Key
*/
OrderedMap map = new LinkedMap();
map.put("FIVE", "5");
map.put("SIX", "6");
map.put("SEVEN", "7");
System.out.println(map.firstKey());
System.out.println(map.nextKey("FIVE"));
System.out.println(map.nextKey("SIX"));
/** **bidimap**
\* 通过key得到value
\* 通过value得到key
\* 将map里的key和value对调
*/
BidiMap bidi = new TreeBidiMap();
bidi.put("SIX", "6");
System.out.println(bidi.get("SIX")); // returns "6"
System.out.println(bidi.getKey("6")); // returns "SIX"
BidiMap inverse = bidi.inverseBidiMap(); // returns a map with keys and values swapped
System.out.println(inverse);
/**
* 得到两个集合中相同的元素
*/
List<String> list1 = Lists.newArrayList("1", "2", "3");
List<String> list2 = Lists.newArrayList("5", "2", "3");
// 交集
System.out.println(CollectionUtils.intersection(list1, list2));
// 交集
System.out.println(CollectionUtils.retainAll(list1, list2));
// 并集
System.out.println(CollectionUtils.union(list1, list2));
// 减
System.out.println(CollectionUtils.subtract(list1, list2));
// 加
CollectionUtils.addAll(list1, list2.iterator());
System.out.println(list1);
list2.add(null);
System.out.println(list2);
// 不为空才加
list2.forEach(key -> CollectionUtils.addIgnoreNull(list1, key));
System.out.println(list1);
xxx xxx = new xxx().setxxx(Lists.newArrayList("1", "2"))
.xxx(2).xxx(1).xxx(Lists.newArrayList("ab", "bc"));
try {
//克隆,没克隆属性.
xxx xxx = (xxx) BeanUtils.cloneBean(xxx);
System.out.println(xxx);
// 不建议用apache赋值,会抛出异常,可用spring提供的不用try.注意参数顺序
org.springframework.beans.BeanUtils.copyProperties(xxx, xxx);
System.out.println(xxx);
} catch (IllegalAccessException|InstantiationException|InvocationTargetException|NoSuchMethodException e) {
e.printStackTrace();
}
2、 将一个Map对象转化为一个Bean,通过Java的反射机制来做的。
// 与lombok @Accessors(chain = true) 冲突.
//这个Map对象的key必须与Bean的属性相对应.
Map<String, Integer> map = Maps.newHashMap();
map.put("xxx", 1);
map.put("xxx", 2);
//将map转化为对象属性.
xxx xxx = new xxx();
try {
BeanUtils.populate(xxx, map);
System.out.println(xxx);
// 将对象转换为map.
Map newMap = BeanUtils.describe(xxx);
System.out.println(newMap);
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
e.printStackTrace();
}
// 一般用spring提供的beantuils, 而且不会抛异常.
System.out.println(Base64.encodeBase64String("test".getBytes()));
System.out.println(new String(Base64.decodeBase64("dGVzdA=="), StandardCharsets.UTF_8));
System.out.println(DigestUtils.md5Hex("asdfasdfasdfasfdasdfasdfasfasd"));
// 可以直接使用spring 提供的
System.out.println(Base64Utils.encodeToString("absd".getBytes()));
System.out.println(new String(Base64Utils.decodeFromString("YWJzZA=="), StandardCharsets.UTF_8));
//1.读取Stream
//标准代码:
try (InputStream in = new URL( "http://jakarta.apache.org" ).openStream()) {
InputStreamReader inR = new InputStreamReader(in);
BufferedReader buf = new BufferedReader(inR);
String line;
while ((line = buf.readLine()) != null) {
System.out.println(line);
}
}
//使用IOUtils
try (InputStream in = new URL( "http://www.baidu.com").openStream()) {
System.out.println( IOUtils.toString(in, StandardCharsets.UTF_8));
}
//比较两个流是否相等
InputStream in = new URL("http://www.baidu.com").openStream();
InputStream in2 = new URL("http://www.baidu.com").openStream();
System.out.println(IOUtils.contentEquals(in, in2));
//将字节从InputStream复制到OutputStream或者是ByteArrayOutputStream
File src = new File("D:/test.txt");
InputStream inputStream = new FileInputStream(src);
File dest = new File("D:/blank.txt");
OutputStream outputStream = new FileOutputStream(dest);
IOUtils.copy(inputStream, outputStream);
//从输入流中读取字节(通常返回输入流的字节数组的长度)
InputStream in4 = new URL("http://www.baidu.com").openStream();
byte[] buffer = new byte[100000];
System.out.println(IOUtils.read(in4, buffer));
//获得输入流的内容放回一个List<String>类型的容器,每一行为这个容器的一个入口,使用特定的字符集(如果为空就使用默认的字符集)
InputStream in5 = new URL("http://www.baidu.com").openStream();
List<String> list = IOUtils.readLines(in5, StandardCharsets.UTF_8);
Iterator<String> iter = list.iterator();
while(iter.hasNext()){
System.out.println(iter.next());
}
//使用FileUtils
File src = new File("D:/test.txt");
List lines = FileUtils.readLines(src, "UTF-8");
System.out.println(lines);
File dest = new File("D:/blank.txt");
//拷贝文件 --这里会覆盖--而非追加
FileUtils.copyFile(src, dest);
//拷贝文件到某一路径
FileUtils.copyFileToDirectory(src, new File("E:/"));
//写字符串到一个文件--此种为覆盖的方法
String string = "Blah blah blah";
FileUtils.writeStringToFile(dest, string, "ISO-8859-1");
// 向文件追加内容.
FileUtils.write(dest, "hhhhhhhhh", StandardCharsets.UTF_8, true);
//删除文件实例
File file = new File( ("E:/test.txt") );
FileUtils.forceDelete(file);
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</dependency>
// 普通Collection的创建
List<String> list = Lists.newArrayList();
Set<String> set = Sets.newHashSet();
Map<String, String> map = Maps.newHashMap();
// 不变Collection的创建
***不可变集合***
* 在多线程操作下,是线程安全的。
* 所有不可变集合会比可变集合更有效的利用资源。
* 中途不可改变。
ImmutableList<String> iList = ImmutableList.of("a", "b", "c");
ImmutableSet<String> iSet = ImmutableSet.of("e1", "e2");
ImmutableMap<String, String> iMap = ImmutableMap.of("k1", "v1", "k2", "v2");
System.out.println(iList);
System.out.println(iSet);
System.out.println(iMap);
iList.add("d");
/**
Exception in thread "main" java.lang.UnsupportedOperationException
at com.google.common.collect.ImmutableCollection.add(ImmutableCollection.java:202)
at com.thunisoft.zx.zxznglxt.controller.SyncDataController.main(SyncDataController.java:62)
**/
// **多值map,Multimap**
//以前
Map<String,List<Integer>> map = new HashMap<String,List<Integer>>();
List<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(2);
map.put("aa", list);
system.out.println(map.get("aa"));//[1, 2]
Multimap<String,Integer> map = ArrayListMultimap.create();
map.put("aa", 1);
map.put("aa", 2);
map.put("aa", 1);
System.out.println(map.get("aa"));
MultiKeyMap multiKeyMap = new MultiKeyMap();
multiKeyMap.put("a","c", "b");
System.out.println(multiKeyMap.get("a"));
System.out.println(multiKeyMap.get("a", "c"));
System.out.println(multiKeyMap.get("a", "d"));
MultiValueMap multiValueMap = new MultiValueMap();
multiValueMap.put("aa", 1);
multiValueMap.put("aa", 2);
multiValueMap.put("ab", 2);
System.out.println(multiValueMap);
Multiset<Integer> set = HashMultiset.create();
for (int i=0;i<10;i++) {
set.add(i%3);
}
System.out.println(set);
System.out.println(set.count(1));
System.out.println(set.count(3));
BiMap<String, String> biMap = HashBiMap.create();
biMap.put("a","1");
System.out.println(biMap);
System.out.println(biMap.inverse());
biMap.put("b", "1");
/**
Exception in thread "main" java.lang.IllegalArgumentException: value already present: 1
at com.google.common.collect.HashBiMap.put(HashBiMap.java:285)
at com.google.common.collect.HashBiMap.put(HashBiMap.java:260)
at com.thunisoft.zx.zxznglxt.controller.SyncDataController.main(SyncDataController.java:69)
**/
Table<String, String, Integer> tables = HashBasedTable.create();
tables.put("a", "b", 3);
tables.put("a", "c", 4);
System.out.println(tables.get("a", "b"));
System.out.println(tables);
//use guava
List<String> list = Lists.newArrayList();
list.add("aa");
list.add("bb");
list.add("cc");
System.out.println(Joiner.on("-").join(list));
//use guava
String str = "1-2-3-4-5-6";
System.out.println(Splitter.on("-").splitToList(str)); //去除空格 .omitEmptyStrings().trimResults()
//list为 [1, 2, 3, 4, 5, 6]
String str = "xiaoming=11,xiaohong=23";
System.out.println(Splitter.on(",").withKeyValueSeparator("=").split(str));
String input = "aa.dd,,ff,,.";
System.out.println(Splitter.onPattern("[.|,]").omitEmptyStrings().splitToList(input));
HashSet<Integer> setA = Sets.newHashSet(1, 2, 3, 4, 5);
HashSet<Integer> setB = Sets.newHashSet(4, 5, 6, 7, 8);
Sets.SetView<Integer> union = Sets.union(setA, setB); //union:12345867
Sets.SetView<Integer> difference = Sets.difference(setA, setB); //difference:123678
Sets.SetView<Integer> symmetricDifference = Sets.symmetricDifference(setA, setB); //difference:123678
Sets.SetView<Integer> intersection = Sets.intersection(setA, setB); //intersection:45
System.out.println(union);
System.out.println(difference);
System.out.println(symmetricDifference);
System.out.println(intersection);
Stopwatch stopwatch = Stopwatch.createStarted();
TimeUnit.SECONDS.sleep(3);
long nanos = stopwatch.elapsed(TimeUnit.MILLISECONDS);
System.out.println(nanos);
标签:联合主键 可靠 isa started oob too 源码 equal 友好
原文地址:https://www.cnblogs.com/yuyy114/p/13246723.html