diff --git a/javase-tutorials/pom.xml b/javase-tutorials/pom.xml index 4fb3bab..5b195ec 100644 --- a/javase-tutorials/pom.xml +++ b/javase-tutorials/pom.xml @@ -78,5 +78,13 @@ junit 4.11 + + + + com.google.guava + guava + 17.0 + + diff --git a/javase-tutorials/src/main/java/org/javase/tutorials/guava/GuavaTutorial.java b/javase-tutorials/src/main/java/org/javase/tutorials/guava/GuavaTutorial.java new file mode 100644 index 0000000..bea9a31 --- /dev/null +++ b/javase-tutorials/src/main/java/org/javase/tutorials/guava/GuavaTutorial.java @@ -0,0 +1,533 @@ +package org.javase.tutorials.guava; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.StringWriter; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import org.junit.Test; + +import com.google.common.base.CharMatcher; +import com.google.common.base.Charsets; +import com.google.common.base.Joiner; +import com.google.common.base.Splitter; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Lists; +import com.google.common.collect.MapDifference; +import com.google.common.collect.MapDifference.ValueDifference; +import com.google.common.collect.Maps; +import com.google.common.collect.Sets; +import com.google.common.collect.Sets.SetView; +import com.google.common.io.ByteStreams; +import com.google.common.io.CharStreams; +import com.google.common.io.Files; +import com.google.common.io.LineReader; +import com.google.common.primitives.Ints; + +/** + * Google Guava工具类 + * + * @author 冯靖 + * + */ +public class GuavaTutorial { + + @Test + public void example1() { + + // Map>> map1 = new HashMap>>(); + + ImmutableList list = ImmutableList.of("小米2", "小米2S", "小米2A", + "小米3"); + + // 声明Map>> + // Map>> map2 = Maps.newHashMap(); + + // 赋值 + ImmutableMap> mp = ImmutableMap.of(1999l, + (List) list); + + System.out + .println("-----------Map>>---------"); + + Set keySet = mp.keySet(); + for (Long log : keySet) { + System.out.println("key:" + log); + List lt = mp.get(log); + for (String string : lt) { + System.out.println(string); + } + } + + // 声明List>> + // List>> list = Lists.newArrayList(); + + // 赋值 + ImmutableList>> lst = ImmutableList + .of((Map>) mp); + + System.out.println("-----------List>>---------"); + + for (Map> map : lst) { + Set key = map.keySet(); + for (Long log : key) { + System.out.println("key:" + log); + List lt = map.get(log); + for (String string : lt) { + System.out.println(string); + } + } + } + + // 声明Set>> + // Set>> sets = Sets.newHashSet(); + + // 赋值 + ImmutableSet>> st = ImmutableSet + .of((Map>) mp); + + System.out.println("-----------Set>>---------"); + + for (Map> map : st) { + Set key = map.keySet(); + for (Long log : key) { + System.out.println("key:" + log); + List lt = map.get(log); + for (String string : lt) { + System.out.println(string); + } + } + } + + } + + @Test + public void example2() { + + List list = new ArrayList(); + + list.add("a"); + list.add("b"); + list.add("c"); + list.add("d"); + + // 直接为List赋值 + ImmutableList of = ImmutableList.of("a", "b", "c", "d"); + + for (String string : of) { + System.out.println(string); + } + + // 直接为Map赋值 + ImmutableMap map = ImmutableMap.of("key1", "value1", + "key2", "value2"); + + System.out.println("-----------------------------------"); + + ImmutableSet keySet = map.keySet(); + + for (String string : keySet) { + System.out.println(string + ":" + map.get(string)); + } + + // 直接为Set赋值 + ImmutableSet set = ImmutableSet.of("a", "b", "c", "d"); + + System.out.println("-----------------------------------"); + + for (String string : set) { + System.out.println(string); + } + } + + @Test + public void example3() { + File file = new File("src/main/resources/sample.txt"); + List lines = null; + try { + lines = Files.readLines(file, Charsets.UTF_8); + for (String string : lines) { + System.out.println(string); + } + } catch (IOException e) { + e.printStackTrace(); + } + } + + @Test + public void example4() throws Exception{ + File file = new File("src/main/resources/sample.txt"); + List lines = CharStreams.readLines(new FileReader(file)); + for (String string : lines) { + System.out.println(string); + } + } + + + @Test + public void example5() throws Exception{ + File file = new File("src/main/resources/sample.txt"); + //读取内容到字符串中 + String string = CharStreams.toString(new FileReader(file)); + System.out.println(string); + } + + /** + * 使用LineReader + * @throws Exception + */ + @Test + public void example6() throws Exception{ + File file = new File("src/main/resources/sample.txt"); + LineReader lineReader = new LineReader(new FileReader(file)); + for(String line = lineReader.readLine();line!=null;line=lineReader.readLine()){ + System.out.println(line); + } + } + + /** + * 将读取的内容拷贝到一个writer中去 + * @throws Exception + */ + @Test + public void example7() throws Exception{ + File file = new File("src/main/resources/sample.txt"); + StringWriter writer = new StringWriter(); + FileWriter fileWriter = new FileWriter(new File("src/main/resources/sample-copy.txt")); + CharStreams.copy(new FileReader(file), fileWriter);//写文件 + fileWriter.flush(); + fileWriter.close(); + System.out.println(writer.toString()); + } + + + @Test + public void example8() throws Exception{ + + File file = new File("src/main/resources/sample.txt"); + + String string = new String(ByteStreams.toByteArray(new FileInputStream(file))); + + System.out.println(string); + + } + + @Test + public void example9() throws Exception{ + + File file = new File("src/main/resources/sample.txt"); + + //将文件输入流拷贝到PrintStream输出 + ByteStreams.copy(new FileInputStream(file), System.out); + + } + + + /** + * 基本数据类型大小比较 + */ + @Test + public void example10(){ + int compare = Ints.compare(10, 59);// (a < b) ? -1 : ((a > b) ? 1 : 0) + System.out.println(compare); + } + + + /** + * 将List转化为int[] + */ + @Test + public void example11(){ + List list = Lists.newArrayList(1, 2, 3, 4); + int[] is = Ints.toArray(list); + for (int i : is) { + System.out.println(i); + } + } + + + + @Test + public void example12() throws Exception{ + File file = new File("src/main/resources/sample.txt"); + System.out.println(Files.toString(file, Charsets.UTF_8)); + } + + @Test + public void example13() throws Exception{ + File file = new File("src/main/resources/sample.txt"); + System.out.println(new String(Files.toByteArray(file))); + } + + @Test + public void example14() throws Exception{ + File file = new File("src/main/resources/sample.txt"); + Files.write(Files.toString(file, Charsets.UTF_8), new File("src/main/resources/sample-copy.txt"), Charsets.UTF_8); + } + + @Test + public void example15() throws Exception{ + File file = new File("src/main/resources/sample.txt"); + Files.copy(file, System.out); + } + + @Test + public void example16(){ + System.out.println(CharMatcher.DIGIT.retainFrom("some text 89983 and more"));//返回数字部分内容 + System.out.println(CharMatcher.DIGIT.removeFrom("some text 89983 and more"));//去除数字 + System.out.println(CharMatcher.DIGIT.replaceFrom("some text 89983 and more","#"));//将数字替换为# + } + + /** + * 连接 + */ + @Test + public void example17(){ + int[] numbers = { 1, 2, 3, 4, 5 }; + String numbersAsString = Joiner.on(";").join(Ints.asList(numbers)); + System.out.println(numbersAsString); + + String numbersAsStringDirectly = Ints.join(";", numbers); + System.out.println(numbersAsStringDirectly); + } + + /** + * 字符串分割 + */ + @Test + public void example18(){ + String numbsAsString = "1,2,3,4,5"; + Iterable split = Splitter.on(",").split(numbsAsString); + for (String object : split) { + System.out.println(object); + } + } + + /** + * 字符串分割 + */ + @Test + public void example19(){ + String testString = "foo , what,,,more,"; + Iterable split = Splitter.on(",").omitEmptyStrings().trimResults().split(testString); + for (String object : split) { + System.out.println(object); + } + } + + @Test + public void example20(){ + + int[] array1 = { 1, 2, 3, 4, 5 }; + int a = 4; + + boolean contains = Ints.contains(array1, a);//判断数组中是否存在a元素 + + System.out.println(contains); + + int indexOf = Ints.indexOf(array1, a); //获取该元素在数组中的下标 + + System.out.println(indexOf); + + int max = Ints.max(array1);//求数组中的最大值 + + System.out.println(max); + + int min = Ints.min(array1);//求数组中的最小值 + + System.out.println(min); + + int[] array2 = {6, 7, 8, 9, 10}; + + int[] concat = Ints.concat(array1, array2 );//合并数组 + + System.out.println(Arrays.toString(concat)); + + } + + /** + * Set的交集, 并集, 差集的用法 + */ + @Test + public void example21(){ + HashSet setA = Sets.newHashSet(1, 2, 3, 4, 5); + HashSet setB = Sets.newHashSet(4, 5, 6, 7, 8); + + SetView union = Sets.union(setA, setB);//取并集 + System.out.println("并集union:"); + for (Integer integer : union){ + System.out.println(integer); + } + + + SetView difference = Sets.difference(setA, setB);//取差集 (所有属于A不属于B的元素构成的集合) + System.out.println("差集 difference:"); + for (Integer integer : difference){ + System.out.println(integer); + } + + + SetView intersection = Sets.intersection(setA, setB);//取交集 + System.out.println("交集intersection:"); + for (Integer integer : intersection){ + System.out.println(integer); + } + + } + + @Test + public void example22(){ + + Map mapA = ImmutableMap.of(123, "Jack Johnson", 456, "Cindy Lewis", 789, "Terry Peters", 912, "Ethan Nicks", 234, "Sarah Perry"); + Map mapB = ImmutableMap.of(123, "Jack Johnson", 478, "Patrick Ewig", 789, "Cindy Peters", 937, "Jon Lund", 234, "Sarah Perry"); + + MapDifference differenceMap = Maps.difference(mapA, mapB); + + System.out.println("判断两个Map是否相同:"+differenceMap.areEqual()); + + System.out.println("-------------------------------------------------"); + + Map> entriesDiffering = differenceMap.entriesDiffering(); + + System.out.println("键相同,值不同:"+Arrays.toString(entriesDiffering.keySet().toArray())); + + Map entriesOnlyOnLeft = differenceMap.entriesOnlyOnLeft(); + + Set keySet = entriesOnlyOnLeft.keySet(); + + System.out.println("------------------------entriesOnlyOnLeft-------------------------"); + + for (Integer integer : keySet) { + System.out.println(integer+":"+entriesOnlyOnLeft.get(integer)); + } + + System.out.println("------------------------entriesOnlyOnRight-------------------------"); + + Map entriesOnlyOnRight = differenceMap.entriesOnlyOnRight(); + + keySet = entriesOnlyOnRight.keySet(); + + for (Integer integer : keySet) { + System.out.println(integer+":"+entriesOnlyOnRight.get(integer)); + } + + System.out.println("-------------------------entriesInCommon------------------------"); + + Map entriesInCommon = differenceMap.entriesInCommon();//取两个map中键值都相同的元素 + + keySet = entriesInCommon.keySet(); + + for (Integer integer : keySet) { + System.out.println(integer+":"+entriesInCommon.get(integer)); + } + + } + + @Test + public void example23(){ + + } + + @Test + public void example24(){ + + } + + @Test + public void example25(){ + + } + + @Test + public void example26(){ + + } + @Test + public void example27(){ + + } + + @Test + public void example28(){ + + } + + @Test + public void example29(){ + + } + + @Test + public void example30(){ + + } + + @Test + public void example31(){ + + } + + @Test + public void example32(){ + + } + + @Test + public void example33(){ + + } + + @Test + public void example34(){ + + } + + @Test + public void example35(){ + + } + + @Test + public void example36(){ + + } + + @Test + public void example37(){ + + } + + @Test + public void example38(){ + + } + + @Test + public void example39(){ + + } + + @Test + public void example40(){ + + } + + @Test + public void example41(){ + + } + + + +} diff --git a/javase-tutorials/src/main/resources/sample-copy.txt b/javase-tutorials/src/main/resources/sample-copy.txt new file mode 100644 index 0000000..325313f --- /dev/null +++ b/javase-tutorials/src/main/resources/sample-copy.txt @@ -0,0 +1,51 @@ +ZIP4J 是一个支持处理ZIP文件的开源库 +支持创建,修改,添加,删除,解压 压缩文件 +支持读/写密码保护 +支持AES加密 128/256 +支持标准ZIP加密 +支持进度监视器 +自持Unicode 文件名 +支持创建分卷压缩文件 +支持将文件添加到压缩包中但不进行压缩 + + +Create, Add, Extract, Update, Remove files from a Zip file +针对ZIP压缩文件创建、添加、抽出、更新和移除文件 +Read/Write password protected Zip files +(读写有密码保护的Zip文件) +Supports AES 128/256 Encryption +(支持AES 128/256算法加密) +Supports Standard Zip Encryption +(支持标准Zip算法加密) +Supports Zip64 format +(支持zip64格式) +Supports Store (No Compression) and Deflate compression method +(支持Store(非压缩)和Deflate压缩方法---不太明白) +Create or extract files from Split Zip files (Ex: z01, z02,...zip) +(针对分块zip文件创建和抽出文件) +Supports Unicode file names +(支持Unicode编码文件名) +Progress Monitor +(进度监控) + +压缩效率 + + ZIP4J提供5中压缩算法: + + DEFLATE_LEVEL_FASTEST + DEFLATE_LEVEL_FASTEST + DEFLATE_LEVEL_NORMAL + DEFLATE_LEVEL_MAXIMUM + DEFLATE_LEVEL_ULTRA + 根据API提供的几种不同压缩级别进行测试(文件夹压缩),测试结果如下: + + 一个180M的文件夹压缩后 + + WinRAR 30秒 - 78.7 MB ; + NORMAL - 18 秒 91.2 MB ; + FAST 13秒 -93.1 MB; + FASTEST - 最快速10 秒94.7 MB + MAXIMUM 23 秒 - 90.7 MB; + ULTRA -50 90.6 MB + +ZIP4J 项目地址为 :http://www.lingala.net/zip4j/ \ No newline at end of file