概述
guava处理java.util.List的工具类
guava处理java.util.Set的工具类
guava处理java.util.Map的工具类
Lists常用方法
package com.me.guava.collect;
import com.google.common.collect.Lists;
import java.util.ArrayList;
import java.util.List;
/**
* @author yanyugang
* @description
* @date 2019-11-09 11:44
*/
public class ListsTest {
public static void main(String[] args){
coreFunList();
}
// Lists主要方法
public static void coreFunList(){
System.out.println("---------字符串转集合--------------");
String str="10010";
ArrayList<String> list=Lists.newArrayList(str);
list.forEach(n -> System.out.print(n + " "));
System.out.println();
System.out.println("----------数组转集合-------------");
String[] array={"1", "2", "3", "4"};
ArrayList<String> strings=Lists.newArrayList(array);
strings.forEach(n -> System.out.print(n + " "));
System.out.println();
System.out.println("----------数组反转-------------");
List<String> reverse=Lists.reverse(strings);
reverse.forEach(n -> System.out.print(n + " "));
System.out.println();
System.out.println("----------添加新元素-------------");
//return an unmodifiable list containing the specified elements
List<String> addList=Lists.asList("0", array);
addList.forEach(n -> System.out.print(n + " "));
System.out.println();
System.out.println("----------获取子集合-------------");
//a list of consecutive sublists
List<List<String>> partition=Lists.partition(addList, 2);
partition.forEach(n -> System.out.print(n + " "));
System.out.println();
}
}
Sets常用方法
package com.me.guava.collect;
import com.google.common.collect.Sets;
import java.util.HashSet;
import java.util.Set;
/**
* @author yanyugang
* @description
* Static utility methods pertaining to {@link Set} instances.
* @date 2019-11-09 15:01
*/
public class SetsTest {
public static void main(String[] args){
coreFunList();
}
// Sets主要方法
public static void coreFunList(){
System.out.println("---------字符串转集合--------------");
HashSet<String> set1=Sets.newHashSet("1","8");
set1.forEach(n -> System.out.print(n + " "));
System.out.println();
HashSet<String> set2=Sets.newHashSet("1","2");
set2.forEach(n -> System.out.print(n + " "));
System.out.println();
System.out.println("---------set1和set2并集--------------");
Sets.SetView<String> union=Sets.union(set1, set2);
union.forEach(n -> System.out.print(n + " "));
System.out.println();
System.out.println("---------set1和set2交集--------------");
Sets.SetView<String> intersection=Sets.intersection(set1, set2);
intersection.forEach(n -> System.out.print(n + " "));
System.out.println();
System.out.println("---------set1和set2差集--------------");
//差集 1中有而2中没有的
Sets.SetView<String> difference=Sets.difference(set1, set2);
difference.forEach(n -> System.out.print(n + " "));
System.out.println();
System.out.println("---------set1和set2相对差集--------------");
//相对差集 1中有2中没有
2中有1中没有的 取出来做结果
Sets.SetView<String> symmetricDifference=Sets.symmetricDifference(set1, set2);
symmetricDifference.forEach(n -> System.out.print(n + " "));
System.out.println();
}
}
Maps常用方法
package com.me.guava.collect;
import com.google.common.base.Function;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* @author yanyugang
* @description
* Static utility methods pertaining to {@link Map} instances
* @date 2019-11-09 16:52
*/
public class MapsTest {
static Book book=new Book.BookBuilder().isbn("ISBN123").title("book1").build();
static Book book2=new Book.BookBuilder().isbn("ISBN456").title("book2").build();
static Book book3=new Book.BookBuilder().isbn("ISBN789").title("book3").build();
static List<Book> books=Lists.newArrayList(book, book2, book3);
public static void main(String[] args){
setToMap();
}
/**
* List 转 Map
*/
public static void listToMap(){
Map<Object, Book> bookMap=Maps.uniqueIndex(books, book1 -> book1.getIsbn());
bookMap.forEach((key, value) -> {
System.out.println(key);
System.out.println(value.getTitle());
});
}
/**
* Set 转 Map
* Returns a live {@link Map} view whose keys are the contents of {@code set}
* and whose values are computed on demand using {@code function}.
*/
public static void setToMap(){
Set<Book> bookSet=Sets.newHashSet(books);
Map<Book, String> bookMap=Maps.asMap(bookSet, input -> input.getIsbn());
System.out.println(bookMap.size());
bookMap.forEach((key, value) -> {
System.out.println(key.getTitle());
System.out.println(value);
});
}
}
教程
- Guava教程
最后
以上就是安详煎饼为你收集整理的guava之Lists、Sets、Maps用法的全部内容,希望文章能够帮你解决guava之Lists、Sets、Maps用法所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复