lambdaj工具类中的 Lambda.maxFrom方法的实现

下面示例一个 lambdaj 工具类Lambda.maxFrom的使用(首先得导包哦)

方法说明; 输出集合中的指定对象属性最大值.


<span style="font-size:18px;">List<Person> persons = new ArrayList<>();
persons.add(new Person("Hanyu", "20"));
persons.add(new Person("King", "22"));
persons.add(new Person("Tom", "21"));
persons.add(new Person("Bob", "24"));
System.out.println(Lambda.maxFrom(persons).getAge());
System.out.println(Lambda.maxFrom(persons).getName());</span>


输出结果: 24
Tom


由于好奇, 自己动手把它实现了, 此为个人思考结果, 没有查看其源代码(其实是没有找到源代码大哭), 下面是关于 Lambda.maxFrom方法的实现.微笑


代码:

--------Person.java------------

package test;

public class Person {
private String name;
private String age;


public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getAge() {
return age;
}


public void setAge(String age) {
this.age = age;
}


public Person(String name, String age) {
this.name = name;
this.age = age;
}

public Person() {
// TODO Auto-generated constructor stub
}
}
---------end of Person.java-----------
---------MyLambda.java--------------
package test;


import java.io.File;
import java.io.FileReader;
import java.io.LineNumberReader;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;


public class MyLambda {
private static Iterable iterable1;
private static String method = null;


public static <T> T maxFrom(Iterable<T> iterable) {
setMethod();
method = method.trim();
String methodName = method.substring(method.lastIndexOf(".") + 1, method.lastIndexOf("("));
Object maxResult = findMaxVal(iterable, methodName);
T first = iterable.iterator().next();
try {
String fieldName = methodName.substring(methodName.indexOf("t") + 1);
Field field = first.getClass().getDeclaredField(fieldName.toLowerCase());
field.setAccessible(true);


// 为该集合的第一个对象相比较的字段赋值, 然后返回
field.set(first, maxResult);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
iterable1 = iterable;
return first;
}


private static List<String> fileLines = new ArrayList<>();
private static String fileName;


public static void setMethod() {
StackTraceElement[] ste = new Exception().getStackTrace();
setFileLines(ste);
int lineNumber = ste[2].getLineNumber();
method = fileLines.get(lineNumber - 1);
}


private static void setFileLines(StackTraceElement[] ste) {
// 追踪堆栈查找是哪一个方法调用了 maxFrom 方法
String className = getDirectory(ste[2].getClassName());
fileName = className;


// 得到调用 maxFrom 方法的java文件的流, 然后将文件的行一次加到 List集合中,
// 方便下次读取(用空间换时间)
if ((fileLines != null && fileLines.size() == 0) || !fileName.equals(className)) {
File file = new File("src/" + className + ".java");
try (LineNumberReader lnr = new LineNumberReader(new FileReader(file))) {
String s = null;
while ((s = lnr.readLine()) != null) {
fileLines.add(s);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}


public static <E> Object findMaxVal(Iterable<E> iterable, String methodName) {
java.util.Iterator<E> it = iterable.iterator();
E first = it.next();
Object max = null;
try {
max = first.getClass().getDeclaredMethod(methodName).invoke(first);
} catch (Exception e) {
e.printStackTrace();
}
Object retVal = null;
while (it.hasNext()) {
E e = it.next();
try {
retVal = e.getClass().getDeclaredMethod(methodName).invoke(e);
if (retVal != null && max != null) {
if (retVal instanceof Comparable) {
if (((Comparable) retVal).compareTo(max) > 0) {
max = retVal;
}
}
// todo...基本类型呢
}
} catch (Exception e1) {
e1.printStackTrace();
}
}
return max;
}


/**
* 
* @param className
* @return
*/
public static String getDirectory(String className) {
className = className.replace(".", "/");
return className;
}
}


---------end of Person.java-----------

---------MyLambda.java--------------

package test;


import java.io.File;
import java.io.FileReader;
import java.io.LineNumberReader;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;


public class MyLambda {
private static Iterable iterable1;
private static String method = null;


public static <T> T maxFrom(Iterable<T> iterable) {
setMethod();
method = method.trim();
String methodName = method.substring(method.lastIndexOf(".") + 1, method.lastIndexOf("("));
Object maxResult = findMaxVal(iterable, methodName);
T first = iterable.iterator().next();
try {
String fieldName = methodName.substring(methodName.indexOf("t") + 1);
Field field = first.getClass().getDeclaredField(fieldName.toLowerCase());
field.setAccessible(true);


// 为该集合的第一个对象相比较的字段赋值, 然后返回
field.set(first, maxResult);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
iterable1 = iterable;
return first;
}


private static List<String> fileLines = new ArrayList<>();
private static String fileName;


public static void setMethod() {
StackTraceElement[] ste = new Exception().getStackTrace();
setFileLines(ste);
int lineNumber = ste[2].getLineNumber();
method = fileLines.get(lineNumber - 1);
}


private static void setFileLines(StackTraceElement[] ste) {
// 追踪堆栈查找是哪一个方法调用了 maxFrom 方法
String className = getDirectory(ste[2].getClassName());
fileName = className;


// 得到调用 maxFrom 方法的java文件的流, 然后将文件的行一次加到 List集合中,
// 方便下次读取(用空间换时间)
if ((fileLines != null && fileLines.size() == 0) || !fileName.equals(className)) {
File file = new File("src/" + className + ".java");
try (LineNumberReader lnr = new LineNumberReader(new FileReader(file))) {
String s = null;
while ((s = lnr.readLine()) != null) {
fileLines.add(s);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}


public static <E> Object findMaxVal(Iterable<E> iterable, String methodName) {
java.util.Iterator<E> it = iterable.iterator();
E first = it.next();
Object max = null;
try {
max = first.getClass().getDeclaredMethod(methodName).invoke(first);
} catch (Exception e) {
e.printStackTrace();
}
Object retVal = null;
while (it.hasNext()) {
E e = it.next();
try {
retVal = e.getClass().getDeclaredMethod(methodName).invoke(e);
if (retVal != null && max != null) {
if (retVal instanceof Comparable) {
if (((Comparable) retVal).compareTo(max) > 0) {
max = retVal;
}
}
// todo...基本类型呢
}
} catch (Exception e1) {
e1.printStackTrace();
}
}
return max;
}


/**
* 
* @param className
* @return
*/
public static String getDirectory(String className) {
className = className.replace(".", "/");
return className;
}
}


--------end of MyLambda.java------------


--------测试---------

<span style="font-size:18px;">@Test
public void test2() {
List<Person> persons = new ArrayList<>();
persons.add(new Person("Hanyu", "20"));
persons.add(new Person("King", "22"));
persons.add(new Person("Tom", "21"));
persons.add(new Person("Bob", "24"));
System.out.println(MyLambda.maxFrom(persons).getName());
}


@Test
public void test1() {
List<Person> persons = new ArrayList<>();
persons.add(new Person("Hanyu", "20"));
persons.add(new Person("King", "22"));
persons.add(new Person("Tom", "21"));
persons.add(new Person("Bob", "24"));
System.out.println(MyLambda.maxFrom(persons).getAge());
}</span>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值