原文地址:http://www.thinksaas.cn/topics/0/83/83436.html
package info.yiwen.data; /** * 测试用bean对象,产品 * @author 乔学士 * */ public class Product { private int id; private String name; private double price; private double weight; private String type; public String getName() { return name; } public void setName(String name) { this.name = name; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public double getWeight() { return weight; } public void setWeight(double weight) { this.weight = weight; } public String getType() { return type; } public void setType(String type) { this.type = type; } public int getId() { return id; } public void setId(int id) { this.id = id; } @Override public String toString() { return"Product [id="+ id +", name="+ name +", price="+ price +", type="+ type +", weight="+ weight +"]"; } }
package info.yiwen.data; import java.util.ArrayList; import java.util.List; import java.util.Random; /** * 生产产品的工厂 * @author 乔学士 * */ public class ProductFactory { public static List<Product> genRandomProduct(int amount){ if(amount < 0 ){ amount = 1; } Random random = new Random(); List<Product> ps = new ArrayList<Product>(amount); for(int i = 0; i < amount; i++){ Product p = new Product(); int randomNum = random.nextInt(100); p.setId(i+1); p.setId(i+1); p.setName("name"+ randomNum); p.setPrice(Math.round(random.nextDouble() * 1000)); p.setWeight(Math.round(random.nextDouble() * 1000)); p.setType("type"+ randomNum); ps.add(p); } return ps; } }
package info.yiwen.test; import java.util.Iterator; import java.util.List; import java.util.Map; import ch.lambdaj.function.closure.Closure; import info.yiwen.data.Product; import info.yiwen.data.ProductFactory; import static ch.lambdaj.Lambda.*; import static ch.lambdaj.collection.LambdaCollections.*; import static org.hamcrest.Matchers.*; /** * 测试类,方便起见,没有用jUnit * @author 乔学士 * */ public class Test { public static void main(String[] args) { List<Product> ps = ProductFactory.genRandomProduct(10); printl(1); //1.简单例子 //对每个product都设置price为100 //forEach(ps).setPrice(100); //对每个product都设置setPrice为100 //按重量从小到大进行排序 ps = sort(ps, on(Product.class).getWeight()); print(ps); printl(2); //2.joinFrom 连接某个属性,中间默认用逗号加空格(","), 可以自定义分隔符 //连接每个product对象的name String names = joinFrom(ps).getName(); //连接每个product对象的name,间隔符为":" String names2 = joinFrom(ps,":").getName(); System.out.println(names); System.out.println(names2); printl(3); //3.select //从ps中选择出重量大于等于500的 List<Product> ps2 = select(ps, //注意下面greaterThanOrEqualTo方法中参数的类型一定要和getWeight严格相等 having(on(Product.class).getWeight(), greaterThanOrEqualTo(new Double(500))) ); System.out.println(ps2); printl(4); //4.selectMin找到最小的, also selectMax //找到重量最轻的产品 Product p = selectMin(ps, on(Product.class).getWeight()); System.out.println(p); printl(5); //5.max and maxFrom, also min and minFrom //找到最重的产品的重量(两种方法) //5.1 double weight1 = max(ps, on(Product.class).getWeight()); //5.2 double weight2 = maxFrom(ps).getWeight(); System.out.println(weight1 +","+ weight2); printl(6); //6.extract,抽取所有对象的某一列返回值 //得到每个产品的id //6.1 使用on List<Integer> ids = extract(ps, on(Product.class).getId()); System.out.println(ids); //6.2 使用属性名 List<?> ids2 = extractProperty(ps,"id"); System.out.println(ids2); printl(7); //7.index 返回一个map,value是每个对象,key是对象的某个属性 //返回一个key为id,value是对象的Map Map<Integer, ?> map = index(ps, on(Product.class).getId()); print(map); printl(8); //8.with 使用更为流畅的方法 //找到重量大于400的产品的id列表,并排序 //8.1原始方法 List<Double> prices1 = sort( extract( select(ps, having(on(Product.class).getWeight(), greaterThan(400.0))), on(Product.class).getPrice() ), on(Double.class).doubleValue() ); //8.2使用with的方法 List<Double> prices2 = with(ps) .retain(having(on(Product.class).getWeight(), greaterThan(400.0))) .extract(on(Product.class).getPrice()) .sort(on(Double.class).doubleValue()); System.out.println(prices1); System.out.println(prices2); printl(9); //9.闭包 ,of(T t) 返回T类型 Closure println = closure(); //创建一个closure,放到ThreadLocal中 of(System.out).println(var(String.class)); //of方法从ThreadLocal中得到这个Closure println.apply("hello"); println.each("hello","oschina"); } static void print(Iterable<?> ps){ Iterator<?> it = ps.iterator(); while(it.hasNext()){ System.out.println(it.next()); } } static void print(Map<?, ?> map){ for(Map.Entry<?, ?> e :map.entrySet()){ System.out.println(e.getKey() +":"+ e.getValue()); } } static void printl(int itemNo){ System.out.println("--------------------"+ itemNo +"---------------------"); } }
--------------------1--------------------- Product [id=4, name=name22, price=56.0, type=type22, weight=193.0] Product [id=6, name=name24, price=35.0, type=type24, weight=194.0] Product [id=9, name=name48, price=371.0, type=type48, weight=417.0] Product [id=8, name=name95, price=981.0, type=type95, weight=434.0] Product [id=5, name=name22, price=566.0, type=type22, weight=594.0] Product [id=2, name=name83, price=249.0, type=type83, weight=764.0] Product [id=10, name=name17, price=359.0, type=type17, weight=855.0] Product [id=7, name=name30, price=203.0, type=type30, weight=877.0] Product [id=3, name=name94, price=897.0, type=type94, weight=981.0] Product [id=1, name=name55, price=714.0, type=type55, weight=991.0] --------------------2--------------------- name22, name24, name48, name95, name22, name83, name17, name30, name94, name55 name22:name24:name48:name95:name22:name83:name17:name30:name94:name55 --------------------3--------------------- [Product [id=5, name=name22, price=566.0, type=type22, weight=594.0], Product [id=2, name=name83, price=249.0, type=type83, weight=764.0], Product [id=10, name=name17, price=359.0, type=type17, weight=855.0], Product [id=7, name=name30, price=203.0, type=type30, weight=877.0], Product [id=3, name=name94, price=897.0, type=type94, weight=981.0], Product [id=1, name=name55, price=714.0, type=type55, weight=991.0]] --------------------4--------------------- Product [id=4, name=name22, price=56.0, type=type22, weight=193.0] --------------------5--------------------- 991.0,991.0 --------------------6--------------------- [4, 6, 9, 8, 5, 2, 10, 7, 3, 1] [4, 6, 9, 8, 5, 2, 10, 7, 3, 1] --------------------7--------------------- 1:Product [id=1, name=name55, price=714.0, type=type55, weight=991.0] 2:Product [id=2, name=name83, price=249.0, type=type83, weight=764.0] 3:Product [id=3, name=name94, price=897.0, type=type94, weight=981.0] 4:Product [id=4, name=name22, price=56.0, type=type22, weight=193.0] 5:Product [id=5, name=name22, price=566.0, type=type22, weight=594.0] 6:Product [id=6, name=name24, price=35.0, type=type24, weight=194.0] 7:Product [id=7, name=name30, price=203.0, type=type30, weight=877.0] 8:Product [id=8, name=name95, price=981.0, type=type95, weight=434.0] 9:Product [id=9, name=name48, price=371.0, type=type48, weight=417.0] 10:Product [id=10, name=name17, price=359.0, type=type17, weight=855.0] --------------------8--------------------- [203.0, 249.0, 359.0, 371.0, 566.0, 714.0, 897.0, 981.0] [203.0, 249.0, 359.0, 371.0, 566.0, 714.0, 897.0, 981.0] --------------------9--------------------- hello hello oschina
时间: 2024-10-15 14:01:34