Guava非常好用的工具类

1、对数组的操作

 

                //获取数组中某个值得索引
		int[] content = {1,3,4};
		int in = Ints.indexOf(content, 3); // 1 guava api
		System.out.println(in);
		System.out.println(Arrays.binarySearch(content, 4));// java api
		
		//合并两个数组
		int[] arr = Ints.concat(new int[] {1,2}, new int[]{3, 4});  // 1,2,3,4
		System.out.println(Arrays.toString(arr));
		
		
		//是否包含
		boolean result = Ints.contains(new int[]{10,20,30,40}, 20); // true
		System.out.println(result);
		
		//最小值
		int min = Ints.min(new int[]{10,20,30,40}); // 10
		System.out.println(min);
		
		//最小值
		int max = Ints.max(10,20,30,40); // 10
		System.out.println(max);
				
		//java中也有类似的api, 只能比较两个值
		System.out.println(Integer.max(1, 9));

2、集合创建和初始化

                List slist = new ArrayList<>();
		List slist1 = Lists.newArrayList();
		
		Set set = Sets.newHashSet("one", "two", "three");
		List list = Lists.newArrayList("one", "two", "three");
		
		/**
		 * 
		 */
		Map> map = new HashMap>();
		List>> listnormal = new ArrayList>>();

		Map> map1 = Maps.newHashMap();
		List>> list1 = Lists.newArrayList();
		
		
		/**
		 * 不可变list
		 */
		ImmutableList of = ImmutableList.of("a", "b", "c", "d");
		System.out.println(of);
		
		
		ImmutableMap immutablemap = ImmutableMap.of("key1", "value1", "key2", "value2");
		ImmutableSet numbers = ImmutableSet.of(10, 20, 30, 40, 50);

字符串的拆分与合并

                Joiner joiner = Joiner.on("; ").skipNulls();
		System.out.println(joiner.join("ay", null, "al", "love"));// ay; al;love

		List joinListStr = new ArrayList<>();
		joinListStr.add("ay");
		joinListStr.add(null);
		joinListStr.add("al");
		joinListStr.add("love");
		Joiner joiner2 = Joiner.on("; ").skipNulls();
		System.out.println(joiner2.join(joinListStr));// ay; al; love
		
		
		// trimResults 和 omitEmptyStrings可以去空,去空白
		Iterable splitterArray = Splitter.on(',').trimResults().omitEmptyStrings().split("ay,al,,   love");
		System.out.println(splitterArray.toString());// [ay, al, love]

		// 传统的字符串分割
		String[] originArray = "ay,al,,   love".split(",");
		System.out.println(Arrays.toString(originArray));// [ay, al, , love]
		System.out.println(originArray.length);// 4(长度为)
		for (int i = 0; i < originArray.length; i++) {
			System.out.println(originArray[i]);
		}
		
		

		Map cityDistMapper = ImmutableMap.of("海淀区", "北京", "朝阳区", "北京", "昌平区", "北京");
		String cityDistMapperStr = Joiner.on("|").withKeyValueSeparator("-").join(cityDistMapper);
		System.out.println(cityDistMapperStr);

		Map cityDistMapper1 = Maps.newHashMap();
		String beijingDistricts = " 海淀区:北京|   朝阳区:北京| 东城区:北京 ||西城区:北京|昌平区:北京  |   |";
		cityDistMapper1.putAll(
				Splitter.on("|").omitEmptyStrings().trimResults().withKeyValueSeparator(":").split(beijingDistricts));
		System.out.println(cityDistMapper1.entrySet().toString());

新集合

                // 一种key可以重复的map,子类有ListMultimap和SetMultimap,对应的通过key分别得到list和set
		Multimap multimap = ArrayListMultimap.create();
		multimap.put("abc", 1);
		multimap.put("abc", 2);
		multimap.put("abc", 3);
		multimap.put("abc", 4);
		multimap.put("abcd", 5);
		multimap.put("abcde", 6);
		for (Integer num : multimap.get("abc")) {
			System.out.println(num);
		}

		// 严格的说不是集合,可以增加重复的元素,并且可以统计出重复元素的个数,例子如下:
		Multiset multiSet = HashMultiset.create();
		multiSet.add(10);
		multiSet.add(30);
		multiSet.add(30);
		multiSet.add(40);
		System.out.println(multiSet.count(30)); // 2
		System.out.println(multiSet.size()); // 4

		// 是一个一一映射,可以通过key得到value,也可以通过value得到key; 双向map
		BiMap biMap = HashBiMap.create();
		biMap.put(1, "hello");
		biMap.put(2, "helloa");
		biMap.put(3, "world");
		biMap.put(4, "worldb");
		biMap.put(5, "my");
		biMap.put(6, "myc");

		System.out.println(biMap.get(2));
		int value = biMap.inverse().get("my");
		System.out.println("my --" + value);

字符串处理

                /**
		 * null to "";
		 */
                //String activityId = paramMap.get("activityId") != null ? paramMap.get("activityId").toString() : "";
		System.out.println(Strings.nullToEmpty(null));// ""
		System.out.println(Strings.nullToEmpty("ay"));// "ay"
		System.out.println(Strings.emptyToNull(""));// null
		System.out.println(Strings.emptyToNull("ay"));// "ay"
		
		/**
		 * 共同前缀
		 */
		String a = "com.jd.coo.Hello";
		String b = "com.jd.coo.Hi";
		String ourCommonPrefix = Strings.commonPrefix(a, b);
		System.out.println("a,b common prefix is " + ourCommonPrefix);
		
		/**
		 * 共同后缀
		 */
		// Strings.commonSuffix(a,b) demo
		String c = "com.google.Hello";
		String d = "com.jd.Hello";
		String ourSuffix = Strings.commonSuffix(c, d);
		System.out.println("c,d common suffix is " + ourSuffix);

 

你可能感兴趣的:(Java基础)