linq在java中的开源介绍.ppt

上传人:wuy****n92 文档编号:70799621 上传时间:2023-01-28 格式:PPT 页数:21 大小:270KB
返回 下载 相关 举报
linq在java中的开源介绍.ppt_第1页
第1页 / 共21页
linq在java中的开源介绍.ppt_第2页
第2页 / 共21页
点击查看更多>>
资源描述

《linq在java中的开源介绍.ppt》由会员分享,可在线阅读,更多相关《linq在java中的开源介绍.ppt(21页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。

1、LINQ for JavaIntegrating Query,Sets and Integrating Query,Sets and Transform operations in J2EETransform operations in J2EEOlmo del CorralOlmo del Corral LINQ for Javan nThe LINQ Project is a set of extensions to the.NET Framework for The LINQ Project is a set of extensions to the.NET Framework for

2、language-integrated query,set,and transform operations.language-integrated query,set,and transform operations.n nExtends C#width native language syntax and class libraries for Extends C#width native language syntax and class libraries for queries.queries.n nWe will show a way to do it in Java withou

3、t making any change in We will show a way to do it in Java without making any change in the Java language.the Java language.Query Examplen nThis is the way a C#3 Query look like.This is the way a C#3 Query look like.n nIt sorts,group and proyect the already defined collection:It sorts,group and proy

4、ect the already defined collection:IEnumerable people;IEnumerable people;n nSeems a SQL Query,but is just syntax sugar.Seems a SQL Query,but is just syntax sugar.n nSo you can do the same without using itSo you can do the same without using itvar query2=from p in people where p.Age 20 orderby p.Age

5、descending,p.Name group new p.Name,Senior=p.Age 30,FirstJob=p.Jobs0 by p.CanCode;Without Query Expressionsn nWithout the cosmetics,it starts feeling standard Java code.Without the cosmetics,it starts feeling standard Java code.n nIn the 5th line,In the 5th line,Name=Name=is omitted.is omitted.n nThe

6、 compiler do it for you.The compiler do it for you.n nBut it makes confusing.It is worth?But it makes confusing.It is worth?var query2=people.Where(p=p.Age 20).OrderByDescending(p=p.Age).ThenBy(p=p.Name).GroupBy(p=p.CanCode,p=new p.Name,Senior=p.Age 30,FirstJob=p.Jobs0 );Without Anonymous Fields in

7、Object Inicializatorsn nA step further in code normalization.A step further in code normalization.n nBut,what But,what new new means?means?n nIts a Anonymous Type.Just like a Java Anonymous Class Its a Anonymous Type.Just like a Java Anonymous Class but only with Fields,Propertiesbut only with Field

8、s,Propertiesn nThe correct way of doing its by building your own class.The correct way of doing its by building your own class.var query2=people.Where(p=p.Age 20).OrderByDescending(p=p.Age).ThenBy(p=p.Name).GroupBy(p=p.CanCode,p=new Name=p.Name,Senior=p.Age 30,FirstJob=p.Jobs0 );Without Anonymous Ty

9、pesn nNow we have PersonResume class with a well-known syntax!Now we have PersonResume class with a well-known syntax!n nAgain,what kind of constructor have braces instead of()?Again,what kind of constructor have braces instead of()?n nIts a Object Inizializator,this expression calls the default Its

10、 a Object Inizializator,this expression calls the default constructor and then iterates setting every involver property.constructor and then iterates setting every involver property.n nIn Java we should create a specific constructor:In Java we should create a specific constructor:class PersonResume

11、string _name;bool _senior;Job _firstJob;string Name get return _name;set _name=value;bool Senior get return _senior;set _senior=value;Job FirstJob get return _firstJob;set _firstJob=value;var query2=people.Where(p=p.Age 20).OrderByDescending(p=p.Age).ThenBy(p=p.Name).GroupBy(p=p.CanCode,p=new Person

12、Resume Name=p.Name,Senior=p.Age 30,FirstJob=p.Jobs0 );Without Objects Inicializatorsn nPersonResume class has a constructor with every field.Much more PersonResume class has a constructor with every field.Much more Java-like!Java-like!n nYou said people is a You said people is a IEnumerable,IEnumera

13、ble,so Are so Are WhereWhere,OrderByOrderBy,GroupBy GroupBy methods ofmethods of IEnumerable IEnumerable?n nNot really.Those Are Extensional Methods,act as it those where Not really.Those Are Extensional Methods,act as it those where in in IEnumerable,IEnumerable,but in fact they are out.Confusing,r

14、ight?but in fact they are out.Confusing,right?n nSun people prefer simplicity.A static method is a better approach.Sun people prefer simplicity.A static method is a better approach.class PersonResume string _name;bool _senior;Job _firstJob;public PersonResum(string name,bool senior,Job firstJob)this

15、._name=name;this._senior=senior;this._firstJob=firstJob;string Name get return _name;set _name=value;bool Senior get return _senior;set _senior=value;Job FirstJob get return _firstJob;set _firstJob=value;var query2=people.Where(p=p.Age 20).OrderByDescending(p=p.Age).ThenBy(p=p.Name).GroupBy(p=p.CanC

16、ode,p=new PersonResume(p.Name,p.Age 30,p.Jobs0);Without Extensional Methodsn nSelectSelect,OrderByOrderBy,GroupByGroupBy should be static methods in Java.should be static methods in Java.n nRight!The only missing thing is the Right!The only missing thing is the=operator.Some kind of pointer?operator

17、.Some kind of pointer?n nNo,its the syntax for Lambda Expressions.A very sort definition for No,its the syntax for Lambda Expressions.A very sort definition for methods.methods.n nThese have an expression body,but a function should have the return These have an expression body,but a function should

18、have the return statement to be clear!.statement to be clear!.class PersonResume string _name;bool _senior;Job _firstJob;public PersonResum(string name,bool senior,Job firstJob)this._name=name;this._senior=senior;this._firstJob=firstJob;string Name get return _name;set _name=value;bool Senior get re

19、turn _senior;set _senior=value;Job FirstJob get return _firstJob;set _firstJob=value;var qwhere=Linq.Where(people,p=p.Age 20);var qorderage=Linq.OrderByDescending(qwhere,p=p.Age);var qthenbt=Linq.ThenBy(qorderage,p=p.Name);var groupy=Linq.GroupBy(qthenbt,p=p.CanCode,p=new PersonResume(p.Name,p.Age 3

20、0,p.Jobs0);With Explicit Typed Lambda Expressionsn nNow,were certain of the parameters type in lambda expression?Now,were certain of the parameters type in lambda expression?n nBut the But the=operator stills there!.This is so obscure!.operator stills there!.This is so obscure!.n nIf a function retu

21、rn a value,why there is no If a function return a value,why there is no returnreturn statement?statement?n nWell maybe using Well maybe using returnreturn lambda expressions we will address this lambda expressions we will address this problem.problem.class PersonResume string _name;bool _senior;Job

22、_firstJob;public PersonResum(string name,bool senior,Job firstJob)this._name=name;this._senior=senior;this._firstJob=firstJob;string Name get return _name;set _name=value;bool Senior get return _senior;set _senior=value;Job FirstJob get return _firstJob;set _firstJob=value;var qwhere=Linq.Where(peop

23、le,(Person p)=p.Age 20);var qorderage=Linq.OrderByDescending(qwhere,(Person p)=p.Age);var qthenbt=Linq.ThenBy(qorderage,(Person p)=p.Name);var groupy=Linq.GroupBy(qthenbt,(Person p)=p.CanCode,(Person p)=new PersonResume(p.Name,p.Age 30,p.Jobs0);With Explicit Body Lambda Expressionsn nLambda expressi

24、ons look simpler with this syntax!Lambda expressions look simpler with this syntax!n nSo,What really these lambda expressions are?.So,What really these lambda expressions are?.n nJust a Anonymous Method!Just a Anonymous Method!n nWhy use another syntax if C#2.0 has one for it?Why use another syntax

25、if C#2.0 has one for it?class PersonResume string _name;bool _senior;Job _firstJob;public PersonResum(string name,bool senior,Job firstJob)this._name=name;this._senior=senior;this._firstJob=firstJob;string Name get return _name;set _name=value;bool Senior get return _senior;set _senior=value;Job Fir

26、stJob get return _firstJob;set _firstJob=value;var qwhere=Linq.Where(people,(Person p)=return p.Age 20;);var qorderage=Linq.OrderByDescending(qwhere,(Person p,Person q)=return p.Age.CompareTo(q.Age););var qthenbt=Linq.ThenBy(qorderage,(Person p,Person q)=return p.Name.CompareTo(q.Name););var groupy=

27、Linq.GroupBy(qthenbt,(Person p)=return p.CanCode;,(Person p)=return new PersonResume(p.Name,p.Age 30,p.Jobs0););Without any kind of Lambda Expressionsn nLook how you can do the same query in C#2.0Look how you can do the same query in C#2.0n nWidth anonymous methods you can write in-line functions.Wi

28、dth anonymous methods you can write in-line functions.n nIts a bit faster,but a lot of people dont understand how they work.Its a bit faster,but a lot of people dont understand how they work.n nYou can improve clarity by declaring explicit methods:You can improve clarity by declaring explicit method

29、s:class PersonResume string _name;bool _senior;Job _firstJob;public PersonResum(string name,bool senior,Job firstJob)this._name=name;this._senior=senior;this._firstJob=firstJob;string Name get return _name;set _name=value;bool Senior get return _senior;set _senior=value;Job FirstJob get return _firs

30、tJob;set _firstJob=value;var qwhere=Linq.Where(people,delegate(Person p)return p.Age 20;);var qorderage=Linq.OrderByDescending(qwhere,delegate(Person p,Person q)return p.Age.CompareTo(q.Age););var qthenbt=Linq.ThenBy(qorderage,delegate(Person p,Person q)return p.Name.CompareTo(q.Name););var groupy=L

31、inq.GroupBy(qthenbt,(Person p)=return p.CanCode;,delegate(Person p)return new PersonResume(p.Name,p.Age 30,p.Jobs0););Without anonymous methodsn nExplicit methods are easier than in-line functions!Explicit methods are easier than in-line functions!n nJust write the method name to make a delegate?Sou

32、nd simple!Just write the method name to make a delegate?Sound simple!n nWell,the correct way is to instantiate the delegate manually:Well,the correct way is to instantiate the delegate manually:class PersonResume string _name;bool _senior;Job _firstJob;public PersonResum(string name,bool senior,Job

33、firstJob)this._name=name;this._senior=senior;this._firstJob=firstJob;string Name get return _name;set _name=value;bool Senior get return _senior;set _senior=value;Job FirstJob get return _firstJob;set _firstJob=value;bool WhereCondition(Person p)return p.Age 20;int CompareAge(Person p,Person q)retur

34、n p.Age.CompareTo(q.Age);int CompareName(Person p,Person q)return p.Name.CompareTo(q.Name);bool CanCode(Person p)return p.CanCode;PersonResume DoResume(Persona p)return new PersonResume(p.Name,p.Age 30,p.Jobs0 );var qwhere=Linq.Where(people,WhereCondition);var qorderage=Linq.OrderByDescending(qwhere

35、,CompareAge);var qthenbt=Linq.ThenBy(qorderage,CompareName);var groupy=Linq.GroupBy(qthenbt,CanCode,DoResume);Without delegate inferencen nExplicit methods are easier than in-line functions!Explicit methods are easier than in-line functions!n nFunc?Only Func?Func?Only Func?n nMmm Its a generic deleg

36、ate,but type information flow automatically,Mmm Its a generic delegate,but type information flow automatically,same as same as varvarn nMaybe do it explicit is better for clarityMaybe do it explicit is better for clarityclass PersonResume string _name;bool _senior;Job _firstJob;public PersonResum(st

37、ring name,bool senior,Job firstJob)this._name=name;this._senior=senior;this._firstJob=firstJob;string Name get return _name;set _name=value;bool Senior get return _senior;set _senior=value;Job FirstJob get return _firstJob;set _firstJob=value;bool WhereCondition(Person p)return p.Age 20;int CompareA

38、ge(Person p,Person q)return p.Age.CompareTo(q.Age);int CompareName(Person p,Person q)return p.Name.CompareTo(q.Name);bool CanCode(Person p)return p.CanCode;PersonResume DoResume(Persona p)return new PersonResume(p.Name,p.Age 30,p.Jobs0 );var qwhere=Linq.Where(people,new Func(WhereCondition);var qord

39、erage=Linq.OrderByDescending(qwhere,new Func(CompareAge);var qthenbt=Linq.ThenBy(qorderage,new Func(CompareName);var groupy=Linq.GroupBy(qthenbt,new Func(CanCode),new Func(DoResume);Without type inferencen nNow type information are explicit on delegates(Func)and weve removed Now type information are

40、 explicit on delegates(Func)and weve removed i implicitly typed local variables mplicitly typed local variables(var)(var)n nOught,there is a lot of code here!Its because generics!Remove it!Ought,there is a lot of code here!Its because generics!Remove it!n nAre you sure?C#2.0 generics are grate for c

41、larity,performance and type safety!Are you sure?C#2.0 generics are grate for clarity,performance and type safety!n nYeah!Go ahead!Yeah!Go ahead!class PersonResume string _name;bool _senior;Job _firstJob;public PersonResum(string name,bool senior,Job firstJob)this._name=name;this._senior=senior;this.

42、_firstJob=firstJob;string Name get return _name;set _name=value;bool Senior get return _senior;set _senior=value;Job FirstJob get return _firstJob;set _firstJob=value;bool WhereCondition(Person p)return p.Age 20;int CompareAge(Person p,Person q)return p.Age.CompareTo(q.Age);int CompareName(Person p,

43、Person q)return p.Name.CompareTo(q.Name);bool CanCode(Person p)return p.CanCode;PersonResume DoResume(Persona p)return new PersonResume(p.Name,p.Age 30,p.Jobs0 );IEnumerable qwhere=Linq.Where(people,new Func(WhereCondition);SortedSequence qorderage=Linq.OrderByDescending(qwhere,new Func(CompareAge);

44、SortedSequence qthenbt=Linq.ThenBy(qorderage,new Func(CompareName);IEnumerableGrouping groupy=Linq.GroupBy(qthenbt,new Func(CanCode),new Func(DoResume);Without genericsn nWeve to renounce to C#2.0 generics to use LINQ on Java.Weve to renounce to C#2.0 generics to use LINQ on Java.(what Java 1.5 has

45、cant be called generics(what Java 1.5 has cant be called generics )n nWell,were now in C#1.0.Again,you can do the same query!.Well,were now in C#1.0.Again,you can do the same query!.n nIts time for the final jump to Java!.Its time for the final jump to Java!.n nWe will start with delegates,why use d

46、elegates instead of interfaces?We will start with delegates,why use delegates instead of interfaces?class PersonResume string _name;bool _senior;Job _firstJob;public PersonResum(string name,bool senior,Job firstJob)this._name=name;this._senior=senior;this._firstJob=firstJob;string Name get return _n

47、ame;set _name=value;bool Senior get return _senior;set _senior=value;Job FirstJob get return _firstJob;set _firstJob=value;bool WhereCondition(object p)return(Person)p).Age 20;int CompareAge(object p,object q)return(Person)p).Age.CompareTo(Person)q).Age);int CompareName(object p,object q)return(Pers

48、on)p).Name.CompareTo(Person)q).Name);object CanCode(object p)return(Person)p).CanCode;object DoResume(object o)Person p=(Person)p;return new PersonResume(p.Name,p.Age 30,p.Jobs0 );IEnumerable qwhere=Linq.Where(people,new WhereFunc(WhereCondition);SortedSequence qorderage=Linq.OrderByDescending(qwher

49、e,new Comparer(CompareAge);SortedSequence qthenbt=Linq.ThenBy(qorderage,new Comparer(CompareName);IEnumerable groupy=Linq.GroupBy(qthenbt,new Func(CanCode),new Func(DoResume);Without delegatesWithout delegatesn nInterfaces are more object-oriented than delegates!Interfaces are more object-oriented t

50、han delegates!n nOur work is almost complete!Our work is almost complete!n nTo target Java we have to remove Properties and Indexers,witch are To target Java we have to remove Properties and Indexers,witch are less object oriented and dont really exist!less object oriented and dont really exist!clas

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 教育专区 > 大学资料

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号© 2020-2023 www.taowenge.com 淘文阁