java第三方集合使用

Java工具库自带有很多集合,但是有时候并不能方便的满足一些需求,第三方集合很好的满足了我们的需求,说不多说,下面看看代码
 
  public static void main(String[] args) {
        // guava Multimap  一键多值MAP
		Multimap<String, String> multimap = ArrayListMultimap.create();
		multimap.put("水果类", "西瓜");
		multimap.put("水果类", "桃子");
		multimap.put("水果类", "柚子");
		multimap.put("坚果类", "核桃");
		multimap.put("坚果类", "松子");
		System.out.println(multimap);
	    // 运行结果 : {水果类=[西瓜, 桃子, 柚子], 坚果类=[核桃, 松子]}
	
	    // 遍历
		multimap.forEach((k, v) -> {
			System.out.println("key:" + k);
			System.out.println("value:" + v);
		});
	
		// 第二种遍历方式
		Set<String> keySet = multimap.keySet();
		for(String key : keySet ) {
			Collection<String> collection = multimap.get(key);
			System.out.println("key:" + key);
			System.out.println("value:" + collection);
		}

        // guava Multiset 带计数SET
        Multiset<String> multiset = LinkedHashMultiset.create();
		multiset.add("木星");
		multiset.add("木星");
		multiset.add("木星");
		multiset.add("木星");
		System.out.println("数量:"+multiset.count("木星"));
		// 运行结果 :数量:4
		
		// 遍历
		multiset.forEach(e->{
			System.out.println(e);
		});
	}

		// guava Table 可以put三个值的容器 可以代替Map<String,List<String>>这种结构
		Table<String, String, String> table = HashBasedTable.create();
		table.put("动物", "鱼", "比目鱼");
		table.put("动物", "鱼", "大白鲨");
		table.put("动物", "鱼", "鮟鱇");
		table.put("植物", "树", "白杨树");
		table.put("植物", "裸子植物", "百岁兰");
	
		// 遍历
		Set<Cell<String, String, String>> cellSet = table.cellSet();
		for (Table.Cell<String, String, String> cell : cellSet) {
			System.out.println("ColumnKey:" + cell.getColumnKey());
			System.out.println("RowKey:" + cell.getRowKey());
			System.out.println("Value:" + cell.getValue());
		}


        // eclipse-collections LIST
		MutableIntList list = IntLists.mutable.with(1, 2, 3, 4, 5);
		// 创建列表的反向视图
		LazyIntIterable asReverseLazy = list.asReversed();
		System.out.println("asReverseLazy:" + asReverseLazy.toList());

		// 复制list并洗牌
		MutableIntList shuffled = list.toList().shuffleThis();
		System.out.println("shuffled:" + shuffled);

		// 复制list并使用随机种子值进行洗牌
		MutableIntList shuffledPredictable = list.toList().shuffleThis(new Random(3));
		System.out.println("shuffledPredictable:" + shuffledPredictable);

		// 向列表中添加两个值
		MutableIntList listWith = list.with(6).with(7);
		System.out.println("listWith:" + listWith);

		// 向列表中添加三个值
		MutableIntList listWithAll = list.withAll(IntLists.immutable.with(8, 9, 10));
		System.out.println("listWithAll:" + listWithAll);

		// 从列表中删除两个值
		MutableIntList listWithout = list.without(9).without(10);
		System.out.println("listWithout:" + listWithout);

		// 从列表中删除三个值
		MutableIntList listWithoutAll = list.withoutAll(IntLists.immutable.with(6, 7, 8));
		System.out.println("listWithoutAll:" + listWithoutAll);

		// 将列表反转到位
		MutableIntList reverseList = list.reverseThis();
		System.out.println("reverseList:" + reverseList);

		// 将列表排序到位
		MutableIntList sortList = list.sortThis();
		System.out.println("sortList:" + sortList);

		// 筛选列表
		MutableIntList evens = list.select(each -> each % 2 == 0);
		System.out.println("evens:" + evens);

		// 仅筛选列表
		MutableIntList odds = list.reject(each -> each % 2 == 0);
		System.out.println("odds:" + odds);

		// 创建了一个转换后的列表,将每个值乘以2
		MutableIntCollection timesTwo = list.collectInt(each -> each * 2, IntLists.mutable.empty());
		System.out.println("timesTwo:" + timesTwo);

		// 创建了一个转换后的列表,将每个int转换为String
		MutableList<String> collect = list.collect(String::valueOf);
		System.out.println("collect:" + collect);

		// 以相同的顺序返回列表中的不同值
		MutableIntList distinct = IntLists.mutable.with(1, 1, 2, 2, 3, 3, 4, 4, 5, 5).distinct();
		System.out.println("distinct:" + distinct);

		// 将列表一次分为两个元素
		RichIterable<IntIterable> chunk = list.chunk(2);
		System.out.println("chunk:" + chunk);

		// 将两个基本列表压缩成对
		MutableList<IntIntPair> zipped = list.zipInt(IntInterval.zeroTo(4));
		System.out.println("zipped:" + zipped);

		// 将可变列表转换为不可变列表
		ImmutableIntList immutableIntList = list.toImmutable();
		System.out.println("immutableIntList:" + immutableIntList);

		// 转换为其他类型的
		MutableIntSet set = list.toSet();
		System.out.println("set:" + set);

		MutableIntBag bag = list.toBag();
		System.out.println("bag:" + bag);

		MutableIntList sortedList = list.toSortedList();
		System.out.println("sortedList:" + sortedList);

		int[] ints = list.toArray();
		System.out.println("ints:" + Arrays.toString(ints));

		int[] sortedInts = list.toSortedArray();
		System.out.println("sortedInts:" + Arrays.toString(sortedInts));

		String string = list.toString();
		System.out.println("string:" + string);

		String makeString = list.makeString("/");
		System.out.println("makeString:" + makeString);


		// 数学和计算
		// 求和
		long sum = list.sum();
		System.out.println("sum:" + sum);
		// 平均值
		double average = list.averageIfEmpty(0.0);
		System.out.println("average:" + average);
		// 中位数
		double median = list.medianIfEmpty(0.0);
		System.out.println("median:" + median);
		// 最小值
		int min = list.minIfEmpty(0);
		System.out.println("median:" + min);
		// 最大值
		int max = list.maxIfEmpty(0);
		System.out.println("max:" + max);

		IntSummaryStatistics stats = list.summaryStatistics();
		System.out.println(stats.getSum());
		System.out.println(stats.getMin());
		System.out.println(stats.getMax());

	
	    // eclipse-collections MAP
	    MutableIntIntMap map = IntIntMaps.mutable.empty();
		// put / get
		map.put(1, 1);
		System.out.println("map get:" + map.get(1));
		map.put(2, 2);
		System.out.println("map get:" + map.get(2));

		// 添加键值
		MutableIntIntMap map1 = map.withKeyValue(3, 3).withKeyValue(4, 4).withKeyValue(5, 5).withKeyValue(6, 7);
		System.out.println("map1:" + map1);

		// 根据key删除元素
		MutableIntIntMap map2 = map.withoutKey(4).withoutKey(5);
		System.out.println("map2:" + map2);

		// 翻转键值
		MutableIntIntMap flipped = map.flipUniqueValues();
		System.out.println("flipped:" + flipped);

		// 如果没有获取到,则返回指定值
		System.out.println("getIfAbsent:" + map.getIfAbsent(6, 20));

		// 如果没有获取到,则put指定值
		System.out.println("getIfAbsentPut:" + map.getIfAbsentPut(8, 99));

		// 获取全部指定值
		MutableIntIntMap withoutAllKeys = map.withoutAllKeys(IntLists.immutable.with(4, 5, 6, 7));
		System.out.println("withoutAllKeys:" + withoutAllKeys);

		long mapSum = map.sum();
		System.out.println("mapSum:" + mapSum);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值