本文主要是介绍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部分的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!