linq在java中的开源介绍.ppt
《linq在java中的开源介绍.ppt》由会员分享,可在线阅读,更多相关《linq在java中的开源介绍.ppt(21页珍藏版)》请在三一办公上搜索。
1、LINQ for Java,Integrating Query,Sets and Transform operations in J2EEOlmo del Corral,LINQ for Java,The LINQ Project is a set of extensions to the.NET Framework for language-integrated query,set,and transform operations.Extends C#width native language syntax and class libraries for queries.We will sh
2、ow a way to do it in Java without making any change in the Java language.,Query Example,This is the way a C#3 Query look like.It sorts,group and proyect the already defined collection:IEnumerable people;Seems a SQL Query,but is just syntax sugar.So you can do the same without using it,var query2=fro
3、m p in people where p.Age 20 orderby p.Age descending,p.Name group new p.Name,Senior=p.Age 30,FirstJob=p.Jobs0 by p.CanCode;,Without Query Expressions,Without the cosmetics,it starts feeling standard Java code.In the 5th line,Name=is omitted.The compiler do it for you.But it makes confusing.It is wo
4、rth?,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 Object Inicializators,A step further in code normalization.But,what new means?Its a Anonymous Type.Just like a Java Ano
5、nymous Class but only with Fields,PropertiesThe 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 Types,Now we have PersonResu
6、me class with a well-known syntax!Again,what kind of constructor have braces instead of()?Its a Object Inizializator,this expression calls the default constructor and then iterates setting every involver property.In Java we should create a specific constructor:,class PersonResume string _name;bool _
7、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 PersonResume Name=p.Name,
8、Senior=p.Age 30,FirstJob=p.Jobs0);,Without Objects Inicializators,PersonResume class has a constructor with every field.Much more Java-like!You said people is a IEnumerable,so Are Where,OrderBy,GroupBy methods of IEnumerable?Not really.Those Are Extensional Methods,act as it those where in IEnumerab
9、le,but in fact they are out.Confusing,right?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._name=name;this._senior=senior;this._firstJob=firstJob;string Name ge
10、t 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 PersonResume(p.Name,p.Age 30,p.Jobs0);,Without Extensional M
11、ethods,Select,OrderBy,GroupBy should be static methods in Java.Right!The only missing thing is the=operator.Some kind of pointer?No,its the syntax for Lambda Expressions.A very sort definition for methods.These have an expression body,but a function should have the return statement to be clear!.,cla
12、ss 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 _firstJob;
13、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 30,p.Jobs0);,With Explicit Typed Lambda Expressions,Now,were certa
14、in of the parameters type in lambda expression?But the=operator stills there!.This is so obscure!.If a function return a value,why there is no return statement?Well maybe using return lambda expressions we will address this problem.,class PersonResume string _name;bool _senior;Job _firstJob;public P
15、ersonResum(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(people,(Person p)=p.Ag
16、e 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 Expressions,Lambda expressions look simpler wi
17、th this syntax!So,What really these lambda expressions are?.Just a Anonymous Method!Why use another syntax 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=fi
18、rstJob;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(people,(Person p)=return p.Age 20;);var qorderage=Linq.OrderByDescending(qwhere,(Person p,Person q)=return p.Age.CompareTo(
19、q.Age););var qthenbt=Linq.ThenBy(qorderage,(Person p,Person q)=return p.Name.CompareTo(q.Name););var groupy=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 Expressions,Look how you can do the same query in C#
20、2.0Width anonymous methods you can write in-line functions.Its a bit faster,but a lot of people dont understand how they work.You can improve clarity by declaring explicit methods:,class PersonResume string _name;bool _senior;Job _firstJob;public PersonResum(string name,bool senior,Job firstJob)this
21、._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(people,delegate(Person p)return p.Age 20;);var qorderage=Linq.OrderByDesce
22、nding(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=Linq.GroupBy(qthenbt,(Person p)=return p.CanCode;,delegate(Person p)return new PersonResume(p.Name,p.Age 30,p.Jobs0););,Wi
23、thout anonymous methods,Explicit methods are easier than in-line functions!Just write the method name to make a delegate?Sound simple!Well,the correct way is to instantiate the delegate manually:,class PersonResume string _name;bool _senior;Job _firstJob;public PersonResum(string name,bool senior,Jo
24、b 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)ret
25、urn 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(qwhe
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- linq java 中的 介绍
链接地址:https://www.31ppt.com/p-6511116.html