gs1-epc-tids_GS示例集合-第2部分

2024-02-29 18:10
文章标签 示例 部分 集合 epc gs gs1 tids

本文主要是介绍gs1-epc-tids_GS示例集合-第2部分,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

gs1-epc-tids

在“ 示例GS集合”的第1部分中 ,我演示了使用select和selectWith筛选GS Collections中的集合的几种不同方法。

为了调用select,我们传入了谓词,通常作为lambda。 为了调用selectWith,我们传递了Predicate2,通常作为方法参考。

GS集合中有很多采用谓词的方法,包括选择,拒绝,检测,任何满足,全部满足,不满足,计数和分区。

还有一些采用Predicate2的形式,称为selectWith,rejectWith,detectWith,anySatisfyWith,allSatisfyWith,noneSatisfyWith,countWith和partitionWith。

在第2部分中,我们将更深入地研究谓词的调用,并看到一些“转换”方法的示例(collect,flatCollect,groupBy,groupByEach),这些方法都将Function类型作为参数。

然后,我将展示一些示例,这些示例利用GS Collections的API从对象容器转移到原始容器,以及如何利用原始容器类型上可用的API。 我将使用下面显示的简单域介绍一些示例。

这些示例是单元测试,并且需要Java 8才能运行。

[点击图片放大]

希望到示例结束时,您将有兴趣进一步探索我们在GS Collections中开发的丰富而完整的API。

示例2:集合中的任何元素都匹配给定谓词吗?

使用任何满意度:

@Testpublic void doAnyPeopleHaveCats()
{
Predicate<Person> predicate = person -> person.hasPet(PetType.CAT);
boolean result =
this .people. anySatisfy (predicate);
Assert.assertTrue(result);
boolean result1 =
this .people. anySatisfyWith (Person::hasPet, PetType.CAT);
Assert.assertTrue(result1);
}

示例3:集合中的所有元素都匹配给定谓词吗?

使用全部满意:

@Testpublic void doAllPeopleHaveCats()
{    
boolean result =        
this .people. allSatisfy (person -> person.hasPet(PetType.CAT));    
Assert.assertFalse(result);    
boolean result1 =        
this .people. allSatisfyWith (Person::hasPet, PetType.CAT);    
Assert.assertFalse(result1);
}

示例4:集合的所有元素都不匹配给定谓词吗?

使用none

@Testpublic void doNoPeopleHaveCats()
{
boolean result =
this .people. noneSatisfy (person -> person.hasPet(PetType.CAT));
Assert.assertFalse(result);
boolean result1 =
this .people. noneSatisfyWith (Person::hasPet, PetType.CAT);
Assert.assertFalse(result1);
}

示例5:计算与给定谓词匹配的元素数

使用次数:

@Testpublic void howManyPeopleHaveCats()
{
int count =
this .people.count(person -> person.hasPet(PetType.CAT));
Assert.assertEquals( 2 , count);
int count1 =
this .people.countWith(Person::hasPet, PetType.CAT);
Assert.assertEquals( 2 , count1);
}

示例6:查找与给定谓词匹配的集合的第一个元素

使用检测:

@Testpublic void findPersonNamedMarySmith()
{
Person result =
this .people. detect (person -> person.named( " Mary Smith " ));
Assert.assertEquals( " Mary " , result.getFirstName());
Assert.assertEquals( " Smith " , result.getLastName());
Person result1 =
this .people. detectWith (Person::named, " Mary Smith " );
Assert.assertEquals( " Mary " , result1.getFirstName());
Assert.assertEquals( " Smith " , result1.getLastName());
}

any / all / noneSatisfy和detect方法是短路方法的示例,这些方法可能返回结果而不会遍历整个集合。 例如,anySatisfy的行为类似于逻辑 ; 一旦找到满足谓词或遍历整个集合的元素,则返回true。

以下示例也采用谓词,但不会短路。

示例7:选择集合的元素

我在本系列的第1部分中展示了许多过滤示例。 这是使用人员/宠物域的复习课程。 筛选集合的方法称为select。

@Testpublic void getPeopleWithCats()
{
MutableList<Person> peopleWithCats =
this .people. select (person -> person.hasPet(PetType.CAT))
Verify.assertSize( 2 , peopleWithCats);
MutableList<Person> peopleWithCats1 =
this .people. selectWith (Person::hasPet, PetType.CAT);
Verify.assertSize( 2 , peopleWithCats1);
}

示例8:查找集合中与给定谓词不匹配的元素

使用拒绝:

@Testpublic void getPeopleWhoDontHaveCats()
{
MutableList<Person> peopleWithNoCats =
this .people. reject (person -> person.hasPet(PetType.CAT));
Verify.assertSize( 5 , peopleWithNoCats);
MutableList<Person> peopleWithNoCats1 =
this .people. rejectWith (Person::hasPet, PetType.CAT);
Verify.assertSize( 5 , peopleWithNoCats1);
}

示例9:将集合的元素划分为与给定谓词匹配和不匹配的元素

使用分区:

@Testpublic void partitionPeopleByCatOwnersAndNonCatOwners()
{
    PartitionMutableList <Person> catsAndNoCats =
this .people. partition (person -> person.hasPet(PetType.CAT));
Verify.assertSize( 2 , catsAndNoCats. getSelected ());
Verify.assertSize( 5 , catsAndNoCats. getRejected ());
    PartitionMutableList <Person> catsAndNoCats1 =
this .people. partitionWith (Person::hasPet, PetType.CAT);
Verify.assertSize( 2 , catsAndNoCats1. getSelected ());
Verify.assertSize( 5 , catsAndNoCats1. getRejected ());
}

在此示例中,返回了特殊类型PartitionMutableList。 该接口的父级是PartitionIterable。 此接口有两种方法:getSelected和getRejected。 这些方法在PartitionIterable的子类型中是协变的。 因此,在PartitionMutableList中,这两种方法的返回类型均为MutableList。 在父类型PartitionIterable中,返回类型将定义为RichIterable。

总结了有关使用Predicate / Predicate2参数的API的部分。 让我们继续使用Function / Function2参数的API。

示例10:将集合从一种类型转换为另一种类型

使用收集:

@Testpublic void getTheNamesOfBobSmithPets()
{
Person person =
this .people.detectWith(Person::named, " Bob Smith " );
MutableList<Pet> pets = person.getPets();
MutableList<String> names =
pets.collect(Pet::getName);
Assert.assertEquals( " Dolly , Spot " , names.makeString());
}

在上面的示例中,我们找到一个名为“鲍勃·史密斯”的人,获取他的宠物列表,并将其转换为MutableList <String>。 我们从集合中收集宠物名称列表。

示例11:根据属性展平集合

如果要收集集合的属性,该属性还指向单个展平集合中的集合,请使用flatCollect。

@Testpublic void getAllPets()
{
Function<Person, Iterable<PetType>> function = person ->
person.getPetTypes();
Assert.assertEquals(
UnifiedSet.newSetWith(PetType.values()),
this .people. flatCollect (function).toSet()
);
Assert.assertEquals(
UnifiedSet.newSetWith(PetType.values()),
this .people. flatCollect (Person::getPetTypes).toSet()
);
}

在第一个示例中,我将lambda提取到一个单独的变量中,以说明flatCollect期望的类型。 collect方法采用Function <? 超级T ,? 扩展V>。 flatCollect方法采用Function <? 超级T ,? 扩展Iterable <V >>。 换句话说,传递给flatCollect的Function必须返回一些可迭代的类型。

示例12:基于某些功能对集合进行分组

使用groupBy:

@Testpublic void groupPeopleByLastName()
{
    Multimap < String , Person > byLastName =
this .people. groupBy (Person::getLastName);
Verify.assertIterableSize( 3 , byLastName.get( " Smith " ));
}

在上面的示例中,根据姓氏对人员进行了分组。 测试显示,有3个人的姓氏为Smith。 groupBy的结果是一个Multimap。 可以将Multimap视为等效于Map <K,Iterable <V >>。 GS集合中的Multimap有很多专长(列表,集合,袋,SsortedBag,SortedSet),并且有可变形式和不可变形式。 在此示例中,我仅使用称为Multimap的父类型,但我可以专门使其使用ListMultimap或MutableListMultimap。

示例13:基于返回多个键的函数对集合进行分组

使用groupByEach:

@Testpublic void groupPeopleByTheirPets()
{
Multimap <PetType, Person> peopleByPets =
this .people. groupByEach (Person::getPetTypes);
RichIterable<Person> catPeople = peopleByPets.get(PetType.CAT);
Assert.assertEquals(
" Mary , Bob " ,
catPeople. collect (Person::getFirstName). makeString ()
);
RichIterable<Person> dogPeople = peopleByPets.get(PetType.DOG);
Assert.assertEquals(
" Bob , Ted " ,
dogPeople. collect (Person::getFirstName). makeString ()
);
}

与flatCollect相似,groupByEach方法采用Function <? 超级T ,? 扩展Iterable <V >>。 返回的Multimap <K,T>实际上是一个多索引,其中每个值可以具有多个键。 在此示例中,我们根据人们拥有的宠物的类型对其进行分组。 一个人可以养多个宠物,这就是为什么“鲍勃”出现在两条弦上的原因。 我使用collect将每个Person转换为他们的名字,然后使用makeString将其转换为逗号分隔值的字符串。 makeString的重载将使用分隔符作为参数以及开始和结束字符。

示例14:将方法返回的原始属性的值求和

使用RichIterable上可用的四个sumOf方法之一:sumOfInt,sumOfFloat,sumOfLong和sumOfDouble。

@Testpublic void getTotalNumberOfPets()
{
long numberOfPets = this .people.sumOfInt(Person::getNumberOfPets);
Assert.assertEquals( 9 , numberOfPets);
}

在上面的示例中,我们对每个人拥有的宠物数的值求和,从而得出了所有人的宠物总数。 为了对整数和浮点数求和,将返回较宽类型的long或double。 sumOfInt方法采用一种特殊的Function形式,称为IntFunction。

public interface IntFunction<T>
extends Serializable
{
int intValueOf(T anObject);
}

GS集合中的所有过程,函数和谓词都扩展了可序列化。 这使它们可以安全地序列化到磁盘或通过电线远程发送,而无需开发人员创建自己的Serializable扩展。

示例15:对象集合和原始集合之间的流利性

如果要从对象集合转换为原始集合,可以使用8种专用原始集合形式之一(collectInt / Float / Long / Double / Byte / Short / Char / Boolean)。 只需使用collect即可将原始集合转换回对象集合。 使用API​​时,此功能可以使用户更加流畅。

@Testpublic void getAgesOfPets()
{
IntList sortedAges =
this .people
. asLazy ()
. flatCollect (Person::getPets)
. collectInt (Pet::getAge)
. toSortedList ();
IntSet uniqueAges = sortedAges. toSet ();
IntSummaryStatistics stats = new IntSummaryStatistics();
sortedAges. forEach (stats:: accept );
Assert.assertTrue(sortedAges. allSatisfy (IntPredicates.greaterThan( 0 )));
Assert.assertTrue(sortedAges. allSatisfy (i -> i > 0 ));
Assert.assertFalse(sortedAges. anySatisfy (i -> i == 0 ));
Assert.assertTrue(sortedAges. noneSatisfy (i -> i < 0 ));
Assert.assertEquals( IntHashSet .newSetWith( 1 , 2 , 3 , 4 ), uniqueAges);
Assert.assertEquals( 2.0d , sortedAges. median (), 0.0 );
Assert.assertEquals(stats. getMin (), sortedAges. min ());
Assert.assertEquals(stats. getMax (), sortedAges. max ());
Assert.assertEquals(stats. getSum (), sortedAges. sum ());
Assert.assertEquals(stats. getAverage (), sortedAges. average (), 0.0 );
Assert.assertEquals(stats. getCount (), sortedAges. size ());
}

在此示例中,我回答了有关人员列表中所带宠物年龄的一些问题。 首先,我使用asLazy()方法。 这是我做出的明智决定,因为我想减少瞬态收集的数量。 我可以删除asLazy()调用,并且代码仍然可以正常工作。 在这种情况下,asLazy()仅仅是内存优化。 接下来,我叫flatCollect,它将所有人们的宠物收集到一个扁平化的集合中。 然后我调用collectInt,它将LazyIterable <Pet>转换为IntIterable。 使用getAge方法将每个Pet转换为其年龄。 如果我以前没有使用过asLazy方法,则应该将MutableList <Pet>转换为IntList。 最后,我调用toSortedList,它接受IntIterable并将其转换为IntList并对int进行排序。 然后,我在IntList上调用toSet并存储唯一的int年龄集。

从那里,我展示了GS Collections中原始集合的丰富性。 最小值,最大值,总和,平均值,中位数等统计方法可直接在集合中使用。 我还通过使用对IntSumaryStatistics :: accept的方法引用(该方法采用了int),将Java 8中名为IntSummaryStatistics的新统计类之一与IntList结合使用。 我还展示了您在先前示例中看到的方法,例如any / all / noneSatisfy,只有它们现在与原始集合一起使用。

示例16:计算集合中某项的出现次数

如果您想快速获得多个物品的数量,可以将一个集合转换为一个包。 Bag大致等效于Map <K,Integer>,其中Integer是项K出现的次数的计数。Bag类似于任何其他集合,因为它支持add,remove等。它还具有专门的功能。有效地查询,添加或删除项目出现的方法。 袋就像一套,允许重复并保留发生次数。

@Testpublic void getCountsByPetType()
{
Bag <PetType> counts =
this .people
. asLazy ()
. flatCollect (Person::getPets)
. collect (Pet::getType)
. toBag ();
Assert.assertEquals( 2 , counts. occurrencesOf (PetType.CAT));
Assert.assertEquals( 2 , counts.occurrencesOf(PetType.DOG));
Assert.assertEquals( 2 , counts.occurrencesOf(PetType.HAMSTER));
Assert.assertEquals( 1 , counts.occurrencesOf(PetType.SNAKE));
Assert.assertEquals( 1 , counts.occurrencesOf(PetType.TURTLE));
Assert.assertEquals( 1 , counts.occurrencesOf(PetType.BIRD));
}

今天在JDK中没有Bag的等效项,就像没有PartitionIterable或Multimap等效项一样。 在Collector类的Java 8方法中,使用Map <K,Integer>(Collectors.counting),Map <Boolean,List <V>(Collectors.partitioning)和Map <K,List <V>来模拟这些类型。 >(Collectors.groupingBy)。

示例17:计算集合中原始值的出现次数

如果要获取原始值的计数,可以使用原始Bag。

@Testpublic void getCountsByPetAge()
{
IntBag counts =
this .people
. asLazy ()
. flatCollect (Person::getPets)
. collectInt (Pet::getAge)
. toBag ();
Assert.assertEquals( 4 , counts. occurrencesOf ( 1 ));
Assert.assertEquals( 3 , counts.occurrencesOf( 2 ));
Assert.assertEquals( 1 , counts.occurrencesOf( 3 ));
Assert.assertEquals( 1 , counts.occurrencesOf( 4 ));
Assert.assertEquals( 0 , counts.occurrencesOf( 5 ));
}

在此示例中,我根据所有宠物的年龄创建了一个IntBag。 这使我可以使用IntBag上的encesOfOf方法来查找每个年龄段的计数。 这是第2部分中的最后一个示例。

这些示例旨在为您提供一些示例,说明您可以使用GS Collections中的API来完成的工作。 今天,RichIterable上提供了一百多种方法。 这为Java开发人员在处理集合时提供了非常丰富的功能。

在JavaOne 2014上,我和克雷格·莫特林 ( Craig Motlin)在一个名为“ GS集合和Java8:功能,流畅,友好和有趣!”的会议中,提供了几个示例,比较了如何在Java 8中使用Streams进行操作与如何使用GS Collections进行操作。 演讲的幻灯片可在JavaOne 2014网站上找到,也可以在GS Collections GitHub Wiki上找到 。

作为参考,这是我用于测试设置和域类的代码。

import  com.gs.collections.api.RichIterable;
import  com.gs.collections.api.bag.Bag;
import  com.gs.collections.api.bag.MutableBag;
import  com.gs.collections.api.bag.primitive.IntBag;
import  com.gs.collections.api.block.function.Function;
import  com.gs.collections.api.block.predicate.Predicate;
import  com.gs.collections.api.list.MutableList;
import  com.gs.collections.api.list.primitive.IntList;
import  com.gs.collections.api.multimap.Multimap;
import  com.gs.collections.api.partition.list.PartitionMutableList;
import  com.gs.collections.api.set.primitive.IntSet;
import  com.gs.collections.impl.bag.mutable.HashBag;
import  com.gs.collections.impl.block.factory.Predicates2;
import  com.gs.collections.impl.block.factory.primitive.IntPredicates;
import  com.gs.collections.impl.list.mutable.FastList;
import  com.gs.collections.impl.set.mutable.UnifiedSet;
import  com.gs.collections.impl.set.mutable.primitive.IntHashSet;
import  com.gs.collections.impl.test.Verify
import  org.junit.Assert;
import  org.junit.Before;
import  org.junit.Test;
import  java.util.IntSummaryStatistics;
public class PersonTest
{
MutableList<Person> people;
@Before
public void setUp() throws Exception
{
this .people = FastList.newListWith(
new  Person( " Mary " , " Smith " ).addPet(PetType.CAT, " Tabby " , 2 ),
new  Person( " Bob " , " Smith " ).addPet(PetType.CAT, " Dolly " ,
3 ).addPet(PetType.DOG, " Spot " , 2 ),
new  Person( " Ted " , " Smith " ).addPet(PetType.DOG, " Spike " , 4 ),
new  Person( " Jake " , " Snake " ).addPet(PetType.SNAKE, " Serpy " , 1 ),
new  Person( " Barry " , " Bird " ).addPet(PetType.BIRD, " Tweety " , 2 ),
new  Person( " Terry " , " Turtle " ).addPet(PetType.TURTLE, " Speedy " ,
1 )
new  Person( " Harry " , " Hamster " ).addPet(PetType.HAMSTER, " Fuzzy " ,
1 ).addPet(PetType.HAMSTER, " Wuzzy " , 1 )
);
}
public class Person
{
private  String firstName;
private  String lastName;
private  MutableList<Pet> pets = FastList.newList();
private  Person(String firstName, String lastName)
{
this .firstName = firstName;
this .lastName = lastName;
public String getFirstName()
{
return this .firstName;
}
public  String getLastName()
{
return this .lastName;
}
public boolean named(String name)
{
return  name.equals( this .getFirstName() + " " +
this .getLastName());
}
public boolean hasPet(PetType petType)
{
return
this .pets.anySatisfyWith(Predicates2.attributeEqual(Pet::getType), petType);
}
public MutableList<Pet> getPets()
{
return this .pets;
}
public  MutableBag<PetType> getPetTypes()
{
return this .pets.collect(Pet::getType, HashBag.newBag());
}
public  Person addPet(PetType petType, String name, int age)
{
this .pets.add( new  Pet(petType, name, age));
return this ;
}
public int getNumberOfPets()
{
return this .pets.size();
}
}
public class Pet
{
private  PetType type;
private  String name;
private int age;
public  Pet(PetType type, String name, int  age)
this .type = type;
this .name = name;
this .age = age;
}
public  PetType getType()
{
return this .type;
}
public  String getName()
{
return this .name;
}
public int getAge()
{
return this .age;
}
}
public enum PetType
{
CAT, DOG, HAMSTER, TURTLE, BIRD, SNAKE
}
}

翻译自: https://www.infoq.com/articles/GS-Collections-by-Example-2/?topicPageSponsorship=c1246725-b0a7-43a6-9ef9-68102c8d48e1

gs1-epc-tids

这篇关于gs1-epc-tids_GS示例集合-第2部分的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/759605

相关文章

uva 11178 计算集合模板题

题意: 求三角形行三个角三等分点射线交出的内三角形坐标。 代码: #include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstring>#include <cmath>#include <stack>#include <vector>#include <

poj 2976 分数规划二分贪心(部分对总体的贡献度) poj 3111

poj 2976: 题意: 在n场考试中,每场考试共有b题,答对的题目有a题。 允许去掉k场考试,求能达到的最高正确率是多少。 解析: 假设已知准确率为x,则每场考试对于准确率的贡献值为: a - b * x,将贡献值大的排序排在前面舍弃掉后k个。 然后二分x就行了。 代码: #include <iostream>#include <cstdio>#incl

zeroclipboard 粘贴板的应用示例, 兼容 Chrome、IE等多浏览器

zeroclipboard单个复制按钮和多个复制按钮的实现方法 最近网站改版想让复制代码功能在多个浏览器上都可以实现,最近看网上不少说我们的代码复制功能不好用的,我们最近将会增加代码高亮等功能,希望大家多多支持我们 zeroclipboard是一个跨浏览器的库类 它利用 Flash 进行复制,所以只要浏览器装有 Flash 就可以运行,而且比 IE 的

基于SpringBoot的宠物服务系统+uniapp小程序+LW参考示例

系列文章目录 1.基于SSM的洗衣房管理系统+原生微信小程序+LW参考示例 2.基于SpringBoot的宠物摄影网站管理系统+LW参考示例 3.基于SpringBoot+Vue的企业人事管理系统+LW参考示例 4.基于SSM的高校实验室管理系统+LW参考示例 5.基于SpringBoot的二手数码回收系统+原生微信小程序+LW参考示例 6.基于SSM的民宿预订管理系统+LW参考示例 7.基于

Spring Roo 实站( 一 )部署安装 第一个示例程序

转自:http://blog.csdn.net/jun55xiu/article/details/9380213 一:安装 注:可以参与官网spring-roo: static.springsource.org/spring-roo/reference/html/intro.html#intro-exploring-sampleROO_OPTS http://stati

笔记整理—内核!启动!—kernel部分(2)从汇编阶段到start_kernel

kernel起始与ENTRY(stext),和uboot一样,都是从汇编阶段开始的,因为对于kernel而言,还没进行栈的维护,所以无法使用c语言。_HEAD定义了后面代码属于段名为.head .text的段。         内核起始部分代码被解压代码调用,前面关于uboot的文章中有提到过(eg:zImage)。uboot启动是无条件的,只要代码的位置对,上电就工作,kern

Java基础回顾系列-第六天-Java集合

Java基础回顾系列-第六天-Java集合 集合概述数组的弊端集合框架的优点Java集合关系图集合框架体系图java.util.Collection接口 List集合java.util.List接口java.util.ArrayListjava.util.LinkedListjava.util.Vector Set集合java.util.Set接口java.util.HashSetjava

【408数据结构】散列 (哈希)知识点集合复习考点题目

苏泽  “弃工从研”的路上很孤独,于是我记下了些许笔记相伴,希望能够帮助到大家    知识点 1. 散列查找 散列查找是一种高效的查找方法,它通过散列函数将关键字映射到数组的一个位置,从而实现快速查找。这种方法的时间复杂度平均为(

Java http请求示例

使用HttpURLConnection public static String httpGet(String host) {HttpURLConnection connection = null;try {URL url = new URL(host);connection = (HttpURLConnection) url.openConnection();connection.setReq

项目实战系列三: 家居购项目 第四部分

购物车 🌳购物车🍆显示购物车🍆更改商品数量🍆清空购物车&&删除商品 🌳生成订单 🌳购物车 需求分析 1.会员登陆后, 可以添加家居到购物车 2.完成购物车的设计和实现 3.每添加一个家居,购物车的数量+1, 并显示 程序框架图 1.新建src/com/zzw/furns/entity/CartItem.java, CartItem-家居项模型 /***