欢迎来到三一办公! | 帮助中心 三一办公31ppt.com(应用文档模板下载平台)
三一办公
全部分类
  • 办公文档>
  • PPT模板>
  • 建筑/施工/环境>
  • 毕业设计>
  • 工程图纸>
  • 教育教学>
  • 素材源码>
  • 生活休闲>
  • 临时分类>
  • ImageVerifierCode 换一换
    首页 三一办公 > 资源分类 > PPT文档下载  

    linq在java中的开源介绍.ppt

    • 资源ID:6511116       资源大小:270KB        全文页数:21页
    • 资源格式: PPT        下载积分:15金币
    快捷下载 游客一键下载
    会员登录下载
    三方登录下载: 微信开放平台登录 QQ登录  
    下载资源需要15金币
    邮箱/手机:
    温馨提示:
    用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)
    支付方式: 支付宝    微信支付   
    验证码:   换一换

    加入VIP免费专享
     
    账号:
    密码:
    验证码:   换一换
      忘记密码?
        
    友情提示
    2、PDF文件下载后,可能会被浏览器默认打开,此种情况可以点击浏览器菜单,保存网页到桌面,就可以正常下载了。
    3、本站不支持迅雷下载,请使用电脑自带的IE浏览器,或者360浏览器、谷歌浏览器下载即可。
    4、本站资源下载后的文档和图纸-无水印,预览文档经过压缩,下载后原文更清晰。
    5、试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。

    linq在java中的开源介绍.ppt

    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 show 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=from 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 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 Object Inicializators,A step further in code normalization.But,what new means?Its a Anonymous Type.Just like a Java Anonymous 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 PersonResume 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 _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,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 IEnumerable,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 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(p.Name,p.Age 30,p.Jobs0);,Without Extensional Methods,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!.,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 _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 30,p.Jobs0);,With Explicit Typed Lambda Expressions,Now,were certain 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 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(people,(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 Expressions,Lambda expressions look simpler with 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=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)=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=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#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._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.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=Linq.GroupBy(qthenbt,(Person p)=return p.CanCode;,delegate(Person p)return new PersonResume(p.Name,p.Age 30,p.Jobs0););,Without 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,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 CompareAge(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,WhereCondition);var qorderage=Linq.OrderByDescending(qwhere,CompareAge);var qthenbt=Linq.ThenBy(qorderage,CompareName);var groupy=Linq.GroupBy(qthenbt,CanCode,DoResume);,Without delegate inference,Explicit methods are easier than in-line functions!Func?Only Func?Mmm Its a generic delegate,but type information flow automatically,same as varMaybe do it explicit is better for clarity,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 _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,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 qorderage=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 inference,Now type information are explicit on delegates(Func)and weve removed implicitly typed local variables(var)Ought,there is a lot of code here!Its because generics!Remove it!Are you sure?C#2.0 generics are grate for clarity,performance and type safety!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._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,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);SortedSequence qthenbt=Linq.ThenBy(qorderage,new Func(CompareName);IEnumerable groupy=Linq.GroupBy(qthenbt,new Func(CanCode),new Func(DoResume);,Without generics,Weve to renounce to C#2.0 generics to use LINQ on Java.(what Java 1.5 has cant be called generics)Well,were now in C#1.0.Again,you can do the same query!.Its time for the final jump to Java!.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 _name;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(Person)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(qwhere,new Comparer(CompareAge);SortedSequence qthenbt=Linq.ThenBy(qorderage,new Comparer(CompareName);IEnumerable groupy=Linq.GroupBy(qthenbt,new Func(CanCode),new Func(DoResume);,Without delegates,Interfaces are more object-oriented than delegates!Our work is almost complete!To target Java we have to remove Properties and Indexers,witch are less object oriented and dont really exist!,class WhereCondition:IWhere bool Where(Person p)return p.Age 20;class CompareByAge:IComparer int Compare(object p,object q)return(Person)p).Age.CompareTo(Person)q).Age);class CompareByName:IComparer int Compare(object p,object q)return(Person)p).Name.CompareTo(Person)q).Name);class CodeGetter:IGetSomething object GetSomething(object p)return(Person)p).CanCode;class ResumeGetter:IGetSomething object GetSomething(object o)Person p=(Person)p;return new PersonResume(p.Name,p.Age 30,p.Jobs0);,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 _firstJob;set _firstJob=value;,IEnumerable qwhere=Linq.Where(people,new WhereCondition();SortedSequence qorderage=Linq.OrderByDescending(qwhere,new CompareByAge();SortedSequence qthenbt=Linq.ThenBy(qorderage,new CompareByName();IEnumerable groupy=Linq.GroupBy(qthenbt,new CodeGetter(),new ResumeGetter();,Without properties and indexers,Much simplier!Just fields and methods!Only one step more!A good object oriented design should use one file for each class!,class WhereCondition:IWhere bool Where(Person p)return p.getAge()20;class CompareByAge:IComparer int Compare(object p,object q)return(Person)p).getAge().CompareTo(Person)q).getAge();class CompareByName:IComparer int Compare(object p,object q)return(Person)p).getName().CompareTo(Person)q).getName();class CodeGetter:IGetSomething object GetSomething(object p)return(Person)p).getCanCode();class ResumeGetter:IGetSomething object GetSomething(object o)Person p=(Person)p;return new PersonResume(p.getName(),p.getAge()30,p.getJobs().elementAt(0);,class PersonResume string _name;bool _senior;Job _firstJob;public PersonResume(string name,bool senior,Job firstJob)this._name=name;this._senior=senior;this._firstJob=firstJob;string getName()return _name;void setName(string value)_name=value;bool getSenior()return _senior;void setSenior(bool senior)_senior=value;Job getFirstJob()return _firstJob;void setFirstJob()_firstJob=value;,IEnumerable qwhere=Linq.Where(people,new WhereCondition();SortedSequence qorderage=Linq.OrderByDescending(qwhere,new CompareByAge();SortedSequence qthenbt=Linq.ThenBy(qorderage,new CompareByName();IEnumerable groupy=Linq.GroupBy(qthenbt,new CodeGetter(),new ResumeGetter();,One class,One file,The Wonderful world of Java!,package stuff;public class WhereCondition implements IWhere bool Where(Person p)return p.getAge()20;,package stuff;public class PersonResume string _name;bool _senior;Job _firstJob;public PersonResume(string name,bool senior,Job firstJob)this._name=name;this._senior=senior;this._firstJob=firstJob;string getName()return _name;void setName(string value)_name=value;bool getSenior()return _senior;void setSenior(bool senior)_senior=value;Job getFirstJob()return _firstJob;void setFirstJob()_firstJob=value;,IEnumerable qwhere=Linq.Where(people,new WhereCondition();SortedSequence qorderage=Linq.OrderByDescending(qwhere,new CompareByAge();SortedSequence qthenbt=Linq.ThenBy(qorderage,new CompareByName();IEnumerable groupy=Linq.GroupBy(qthenbt,new CodeGetter(),new ResumeGetter();,package stuff;public class CompareByAge implements IComparer int Compare(object p,object q)return(Person)p).getAge().CompareTo(Person)q).getAge();,package stuff;public class ResumeGetter implements IGetSomething object GetSomething(object o)Person p=(Person)p;return new PersonResume(p.getName(),p.getAge()30,p.getJobs().elementAt(0);,package stuff;public class CompareByName impleme

    注意事项

    本文(linq在java中的开源介绍.ppt)为本站会员(牧羊曲112)主动上传,三一办公仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知三一办公(点击联系客服),我们立即给予删除!

    温馨提示:如果因为网速或其他原因下载失败请重新下载,重复下载不扣分。




    备案号:宁ICP备20000045号-2

    经营许可证:宁B2-20210002

    宁公网安备 64010402000987号

    三一办公
    收起
    展开