linq在java中的开源介绍.ppt
《linq在java中的开源介绍.ppt》由会员分享,可在线阅读,更多相关《linq在java中的开源介绍.ppt(21页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
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
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- linq java 中的 介绍
限制150内