开发业务过程中 明显感觉到java语言表达力的不足 就好像没有桌子的概念 所以每次提到桌子都得通过这么一长串的文字--有光滑平板、由腿或其它支撑物固定起来的家具,用以吃饭、写字、工作或玩牌
--来表达桌子的概念 反正开发过程中我是有点晕
下面是几个比较典型的例子
购买某些商品 会给用户发一些优惠券 如2张满100减50优惠券 1张满200减50优惠券等
我提供一个了接口 接收上述券信息
先查询redis判断该券是不是已经存在 如
hmget key 100_50 200_50
如果券不存在 先去创建 然后将其放到redis中 如hmset key 100_50 84678bfd7c1011e6a22b4437e6d0648e
最后得到券编码和张数的映射关系 批量发券
batchSendCouponsToUser(userId,couponCodeCountMap);
String[] descArray = {"aaa", "bbb", "ccc"}; // 券描述 如 满100减50 List codeList = newArrayList("111", null, "333"); // 券编码 // 找出尚不存在code的券 List nullElementList = newArrayList(); for (int i = 0; i < codeList.size(); i++) { if (codeList.get(i) == null) { nullElementList.add(descArray[i]); } } assertThat(nullElementList).containsExactly("bbb");
String[] descArray = {"aaa", "bbb", "ccc"}; // 券描述 List codeList = newArrayList("111", "222", "333"); // 券编码 Map descCouponInfoMap = ImmutableMap.of("aaa", new CouponInfo("aaa", 1), "bbb", new CouponInfo("bbb", 2), "ccc", new CouponInfo("ccc", 3)); // 券描述 -- 券信息 // 得到券编码与发放张数Map Map codeCountMap = new HashMap<>(); for (int i = 0; i < codeList.size(); i++) { codeCountMap.put(codeList.get(i), descCouponInfoMap.get(descArray[i]).getCount()); } assertThat(codeCountMap).containsExactly(new DefaultMapEntry("111",1),new DefaultMapEntry("222",2),new DefaultMapEntry("333",3));
List fooList = newArrayList(new Foo("aaa"), new Foo("bbb"), new Foo("ccc")); List barList = newArrayList(new Bar("111"), new Bar("222"), new Bar("333")); Map descCodeMap = new HashMap<>(); // 券描述 -- 券编码 // 将两个List各抽取一个属性成Map for (int i = 0; i < fooList.size(); i++) { descCodeMap.put(fooList.get(i).getDesc(), barList.get(i).getCode()); } assertThat(descCodeMap).contains(new DefaultMapEntry("aaa","111"),new DefaultMapEntry("bbb","222"),new DefaultMapEntry("ccc","333"));
像第一个还好, 可以提供一个通用的工具类如
static List findNullElementList(List srcList, List destList)
后面两个因为涉及到动态的获取属性值 还不好封装 难道使用java就只能这么麻烦吗?
不知道其他语言针对上述场景是不是一样的麻烦?
如 python javascript ruby 等
I don’t quite understand what you meant by your first paragraph...
My understanding is: Is there any convenient tool that can achieve your needs?
Yes, the most indispensable thing about Java is tools. Basically, as long as you think of the tools you need, search online, and you can find them in 99% of the cases.
It is recommended to use
java8+google guava
, it can be used in any combination, it is better to use both at the same time.Based on the situations you mentioned in these 3 examples, here are the answers one by one:
Then:
A: In the same way, first zip the two arrays into object arrays, then:
A: In the same way, first zip the two arrays into object arrays, then:
You mentioned dynamically obtaining attribute values. Before java8, you could dynamically obtain attribute values through reflection. In java8, you could obtain them through method references. Here I take java8 as an example:
couponInfo::getCount
就是一个supplier
, based on this principle, many functions that could only be realized through reflection in the past can be realized in java8 throughmethod reference.The above, even if you don’t use Java8, you can use guava to elegantly implement these functions.
Reference:
http://ifeve.com/google-guava/
http://www.importnew.com/1036...
http://www.importnew.com/1190...
In fact, Java 8 does not require zip,
Find the corresponding collection that is empty
Related Map
Generate Map
Obviously this is your problem. ticketList;
Do you know what classes and objects are?
Using list map for everything is of course not possible.
You define a coupon class with code and desc attributes
List
for(Ticket ticket:ticketList){
if(ticket.getCode()==null){
}
}
I won’t mention the second and third ones.
The shortcomings of high-rise design cannot be made up for by the bottom floor.
The poster needs to strengthen abstract design and data structure design.
The first scene is a very unstable design based on position. There should be a specific type
It will be easier to find the ones with empty code in the List, but even better, use an unusedList to save all notUsedCoupon.
The second scenario is used as classification statistics. There is a ready-made grouping method in guava. After the grouping is constructed, the number of each group will be known.
In the third scenario, it is not necessary to take an attribute each. In fact, you can construct an object reference to Foo and Bar, and use delegation to
these two objects respectively.
As the respondent mentioned above, there is a lack of high-level design here. When there are three attributes a, b, and c, if you design three Lists respectively, then there will be a search process of
a->b, b->c, and a more complicated operation is a->( b, c). If they are in an object, then a->o(a,b,c) can complete most of the data indexing work.
Python is an interpreted language that can dynamically obtain attributes at runtime
I don’t know Java very well, but there should be several similar reflection mechanisms.
Java is an ancient language. If it is not expressive enough as you said, why has it become the most popular language in the world? It’s just that you didn’t learn it well
Don’t use arrays or lists to separate tickets and codes. There should be a one-to-one relationship between tickets and codes. When building a model, you can create a ticket class with a code attribute in it. Isn’t this very clear?
Java is a verbose language, and the codes written in java are basically quite verbose (before 8).
Java alone is not as elegant as pyhton and other languages in handling basic operations, but it can be compensated for by tool classes and other Groovy.