linq for java_java 的 linq,不要再写令人讨厌的 for 了!

packagecom.ly.linq;importjava.util.ArrayList;importjava.util.HashMap;importjava.util.Iterator;public classEnm {public static ArrayList notNull(Iterablesource) {return where(source, new Pre() {

@Overridepublic booleancheck(T t) {return t != null;

}

});

}public static ArrayList where(T[] ts, Premapper) {returnwhere(toList(ts), mapper);

}public static ArrayList where(Iterable source, Premapper) {

ArrayList r = new ArrayList();for(T t : source) {if(mapper.check(t)) {

r.add(t);

}

}returnr;

}public static String toString(T[] source, Object s) {returntoString(toList(source), s);

}public static String toString(T[] source, Object s, FuncTmapper) {returntoString(toList(source), s, mapper);

}public static String toString(Iterablesource, Object s) {return toString(source, s, null);

}public static String toString(Iterable source, Object spliter, FuncTmapper) {

String s= spliter == null ? "": spliter.toString();

StringBuilder sb= newStringBuilder();for(T ti : source) {

String str= mapper == null ?ti.toString() : mapper.get(ti);

sb.append(str+s);

}if (sb.length() > 0) {

sb.setLength(sb.length()-s.length());returnsb.toString();

}else{return "";

}

}public static HashMap toHashMap(T[] ts, FuncT>pair) {returntoHashMap(toList(ts), pair);

}public static HashMap toHashMap(Iterable source, FuncT>pair) {

HashMap r = new HashMap();for(T t : source) {

Pair p =pair.get(t);

r.put(p.getKey(), p.getValue());

}returnr;

}public static ArrayList toList(Iterablesource) {

ArrayList r = new ArrayList();for(T t : source) {

r.add(t);

}returnr;

}public static ArrayListtoList(T[] ts) {

ArrayList r = new ArrayList();for(T t : ts) {

r.add(t);

}returnr;

}public static ArrayList except(Iterable it0, Iterableit1) {

ArrayList r = new ArrayList();for(T t : it0) {if (!r.contains(t)) {

r.add(t);

}

}for (int i = r.size() - 1; i > -1; i--) {

T t=r.get(i);if(contains(it1, t)) {

r.remove(t);

}

}returnr;

}public static ArrayList union(Iterable it0, Iterableit1) {

ArrayList r = new ArrayList();for(T t : it0) {if (!r.contains(t)) {

r.add(t);

}

}for(T t : it1) {if (!r.contains(t)) {

r.add(t);

}

}returnr;

}public static ArrayList intersect(Iterable it0, Iterableit1) {

ArrayList r = new ArrayList();for(T t0 : it0) {for(T t1 : it1) {if (t0 == null) {if (t1 == null) {if (!r.contains(t0)) {

r.add(t0);

}

}

}else{if(t0.equals(t1)) {if (!r.contains(t0)) {

r.add(t0);

}

}

}

}

}returnr;

}public static ArrayList intersect(Class outCls, Iterable it0, Iterable it1, FuncT mapper0, FuncTmapper1) {

ArrayList r = new ArrayList();for(T0 t0 : it0) {

TOut t0out=mapper0.get(t0);for(T1 t1 : it1) {

TOut t1out=mapper1.get(t1);if (t0out == null) {if (t1out == null) {if (!r.contains(t0out)) {

r.add(null);

}

}

}else{if(t0out.equals(t1out)) {if (!r.contains(t0out)) {

r.add(t0out);

}

}

}

}

}returnr;

}public static booleanexistsIn(T t, T... ts) {for(T t2 : ts) {if(t.equals(t2)) {return true;

}

}return false;

}public static boolean contains(Iterablesource, T target) {for(T t : source) {if (t == null) {if (target == null) {return true;

}

}else{if(t.equals(target)) {return true;

}

}

}return false;

}public static boolean any(Prepre, T t, T... ts) {if(pre.check(t)) {return true;

}returnany(toList(ts), pre);

}public static boolean any(T[] ts, Prepre) {returnany(toList(ts), pre);

}public static boolean any(Iterable source, Prepre) {for(T i : source) {if(pre.check(i)) {return true;

}

}return false;

}public static ArrayList sort(ArrayList source, FuncTmapper) {int[] indexes =sortIndexes(source, mapper);

ArrayList r = new ArrayList();for (inti : indexes) {

r.add(source.get(i));

}returnr;

}public static int[] sortIndexes(Iterable source, FuncTmapper) {

ArrayList ints = new ArrayList();

Iterator it =source.iterator();while(it.hasNext()) {

Integer integer=mapper.get(it.next());

ints.add(integer);

}int[] r = new int[ints.size()];for (int i = 0; i < r.length; i++) {

r[i]=ints.get(i);

}returnsortIndexes(r);

}public static int[] sortIndexes(T[] source, FuncTmapper) {int[] ints = new int[source.length];for (int i = 0; i < source.length; i++) {

ints[i]=mapper.get(source[i]);

}returnsortIndexes(ints);

}public static int[] sortIndexes(int[] source) {

ArrayList decre = new ArrayList();for (inti : source) {

decre.add(i);

}

ArrayList incre = new ArrayList();while (incre.size()

s=i;

v=e;

}

}

decre.remove(s);for (int i = 0; i < source.length; i++) {if (source[i] != v ||incre.contains(i)) {continue;

}

incre.add(i);break;

}

}int[] r = new int[incre.size()];for (int i = 0; i < incre.size(); i++) {

r[i]=incre.get(i);

}returnr;

}public static ArrayList select(TIn[] source, FuncTmapper) {returnselect(toList(source), mapper);

}public static ArrayList select(Iterable source, FuncTmapper) {

ArrayList r = new ArrayList();for(TIn ti : source) {

r.add(mapper.get(ti));

}returnr;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值