如果面对一大块代码不知如何下手,那么就从小处着手,先动起来。 对于如下代码,了解 Java8 Stream api 的同学肯定知道怎么做了:
public List getData(List keys) {
// maybe xxxService.getData(keys);
List result = new ArrayList();
for (String key: keys) {
result.add(Integer.valueOf(key) % 1000000000);
}
return result;
}
for (String part: parts) {
int start = Integer.parseInt(part.split(":")[0]);
int end = Integer.parseInt(part.split(":")[1]);
if (end > allKeys.size()) {
end = allKeys.size();
}
final List tmpRowkeyList = allKeys.subList(start, end);
// submit tasks
}
package zzz.study.function.refactor.result;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
import zzz.study.function.refactor.CatchUtil;
import zzz.study.function.refactor.ExecutorUtil;
import zzz.study.function.refactor.ForeachUtil;
import zzz.study.function.refactor.StreamUtil;
/**
* Created by shuqin on 17/6/23.
*/
public class ConcurrentDataHandlerFrameRefactored {
public static void main(String[] args) {
List allData = getAllData(DataSupplier::getKeys, GetTradeData::getData);
consumer(allData, System.out::println);
List handledData = handleAllData(allData,
(numbers) -> StreamUtil.map(numbers, (num) -> Math.sqrt(num)) );
consumer(handledData, System.out::println);
List objs = StreamUtil.map(DataSupplier.getKeys(), s->Double.valueOf(s));
List handledData2 =
handleAllData((numbers) -> StreamUtil.map(numbers, (num) -> Math.pow((double)num,2))).apply(objs);
consumer(handledData2, System.out::println);
Function, List> func = (numbers) -> StreamUtil.map(numbers, (num) -> Integer.parseInt(num)*2);
List handledData3 =
handleAllData(DataSupplier::getKeys).apply(func);
consumer(handledData3, System.out::println);
}
/**
* 获取所有业务数据
*
* 回调的替换
*/
public static List getAllData(Supplier> getAllKeysFunc, Function, List> iGetBizDataFunc) {
return getAllData(getAllKeysFunc.get(), iGetBizDataFunc);
}
public static List getAllData(List allKeys, Function, List> iGetBizDataFunc) {
return handleAllData(allKeys, iGetBizDataFunc);
}
public static List handleAllData(Supplier> getAllKeysFunc, Function, List> handleBizDataFunc) {
return handleAllData(getAllKeysFunc.get(), handleBizDataFunc);
}
/**
* 传入一个数据处理函数,返回一个可以并发处理数据集的函数, 该函数接受一个指定数据集
* Java 模拟柯里化: 函数工厂
*/
public static Function, List> handleAllData(Function, List> handleBizDataFunc) {
return ts -> handleAllData(ts, handleBizDataFunc);
}
/**
* 传入一个数据提供函数,返回一个可以并发处理获取的数据集的函数, 该函数接受一个数据处理函数
* Java 模拟柯里化: 函数工厂
*/
public static Function, List>, List> handleAllData(Supplier> getAllKeysFunc) {
return handleBizDataFunc -> handleAllData(getAllKeysFunc.get(), handleBizDataFunc);
}
public static List handleAllData(List allKeys, Function, List> handleBizDataFunc) {
return ExecutorUtil.exec(allKeys, handleBizDataFunc);
}
public static void consumer(List data, Consumer consumer) {
data.forEach( (t) -> CatchUtil.tryDo(t, consumer) );
}
public static class DataSupplier {
public static List getKeys() {
// foreach code refining
return ForeachUtil.foreachAddWithReturn(2000, (ind -> Arrays.asList(String.valueOf(ind))));
}
}
/** 获取业务数据具体实现 */
public static class GetTradeData {
public static List getData(List keys) {
// maybe xxxService.getData(keys);
return StreamUtil.map(keys, key -> Integer.valueOf(key) % 1000000000); // stream replace foreach
}
}
}
ExecutorUtil
package zzz.study.function.refactor;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.CompletionService;
import java.util.concurrent.ExecutorCompletionService;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import java.util.function.Function;
/**
* Created by shuqin on 17/6/25.
*/
public class ExecutorUtil {
private ExecutorUtil() {}
private static final int CORE_CPUS = Runtime.getRuntime().availableProcessors();
private static final int TASK_SIZE = 1000;
// a throol pool may be managed by spring
private static ExecutorService executor = new ThreadPoolExecutor(
CORE_CPUS, 10, 60L, TimeUnit.SECONDS,
new ArrayBlockingQueue<>(60));
/**
* 根据指定的列表关键数据及列表数据处理器,并发地处理并返回处理后的列表数据集合
* @param allKeys 列表关键数据
* @param handleBizDataFunc 列表数据处理器
* @param 待处理的数据参数类型
* @param 待返回的数据结果类型
* @return 处理后的列表数据集合
*
* NOTE: 类似实现了 stream.par.map 的功能,不带延迟计算
*/
public static List exec(List allKeys, Function, List> handleBizDataFunc) {
List parts = TaskUtil.divide(allKeys.size(), TASK_SIZE);
//System.out.println(parts);
CompletionService>
completionService = new ExecutorCompletionService<>(executor);
ForeachUtil.foreachDone(parts, (part) -> {
final List tmpRowkeyList = TaskUtil.getSubList(allKeys, part);
completionService.submit(
() -> handleBizDataFunc.apply(tmpRowkeyList)); // lambda replace inner class
});
// foreach code refining
List result = ForeachUtil.foreachAddWithReturn(parts.size(), (ind) -> get(ind, completionService));
return result;
}
/**
* 根据指定的列表关键数据及列表数据处理器,并发地处理
* @param allKeys 列表关键数据
* @param handleBizDataFunc 列表数据处理器
* @param 待处理的数据参数类型
*
* NOTE: foreachDone 的并发版
*/
public static void exec(List allKeys, Consumer> handleBizDataFunc) {
List parts = TaskUtil.divide(allKeys.size(), TASK_SIZE);
//System.out.println(parts);
ForeachUtil.foreachDone(parts, (part) -> {
final List tmpRowkeyList = TaskUtil.getSubList(allKeys, part);
executor.execute(
() -> handleBizDataFunc.accept(tmpRowkeyList)); // lambda replace inner class
});
}
public static List get(int ind, CompletionService> completionService) {
// lambda cannot handler checked exception
try {
return completionService.take().get();
} catch (Exception e) {
e.printStackTrace(); // for log
throw new RuntimeException(e.getCause());
}
}
}
TaskUtil
package zzz.study.function.refactor;
import java.util.ArrayList;
import java.util.List;
/**
* Created by shuqin on 17/1/5.
*/
public class TaskUtil {
private TaskUtil() {}
public static List divide(int totalSize, int persize) {
List parts = new ArrayList();
if (totalSize <= 0 || persize <= 0) {
return parts;
}
if (persize >= totalSize) {
parts.add("0:" + totalSize);
return parts;
}
int num = totalSize / persize + (totalSize % persize == 0 ? 0 : 1);
for (int i=0; i totalSize) {
end = totalSize;
}
parts.add(start + ":" + end);
}
return parts;
}
public static List getSubList(List allKeys, String part) {
int start = Integer.parseInt(part.split(":")[0]);
int end = Integer.parseInt(part.split(":")[1]);
if (end > allKeys.size()) {
end = allKeys.size();
}
return allKeys.subList(start, end);
}
}
package zzz.study.function.refactor;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
/**
* Created by shuqin on 17/6/24.
*
* foreach 代码通用模板
*/
public class ForeachUtil {
public static List foreachAddWithReturn(int num, Function> getFunc) {
List result = new ArrayList();
for (int i=0; i< num; i++) {
result.addAll(CatchUtil.tryDo(i, getFunc));
}
return result;
}
public static void foreachDone(List data, Consumer doFunc) {
for (T part: data) {
CatchUtil.tryDo(part, doFunc);
}
}
}
CatchUtil
package zzz.study.function.refactor;
import java.util.function.Consumer;
import java.util.function.Function;
/**
* Created by shuqin on 17/6/24.
*/
public class CatchUtil {
public static R tryDo(T t, Function func) {
try {
return func.apply(t);
} catch (Exception e) {
e.printStackTrace(); // for log
throw new RuntimeException(e.getCause());
}
}
public static void tryDo(T t, Consumer func) {
try {
func.accept(t);
} catch (Exception e) {
e.printStackTrace(); // for log
throw new RuntimeException(e.getCause());
}
}
}
StreamUtil
package zzz.study.function.refactor;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
* Created by shuqin on 17/6/24.
*/
public class StreamUtil {
public static List map(List data, Function mapFunc) {
return data.stream().map(mapFunc).collect(Collectors.toList()); // stream replace foreach
}
}
androi中提到了布尔数组;
布尔数组默认的是false, 并且只会打印false或者是true
布尔数组的例子; 根据字符数组创建布尔数组
char[] c = {'p','u','b','l','i','c'};
//根据字符数组的长度创建布尔数组的个数
boolean[] b = new bool
文章摘自:http://blog.csdn.net/yangwawa19870921/article/details/7553181
在编写HQL时,可能会出现这种代码:
select a.name,b.age from TableA a left join TableB b on a.id=b.id
如果这是HQL,那么这段代码就是错误的,因为HQL不支持
1. 简单的for循环
public static void main(String[] args) {
for (int i = 1, y = i + 10; i < 5 && y < 12; i++, y = i * 2) {
System.err.println("i=" + i + " y="
异常信息本地化
Spring Security支持将展现给终端用户看的异常信息本地化,这些信息包括认证失败、访问被拒绝等。而对于展现给开发者看的异常信息和日志信息(如配置错误)则是不能够进行本地化的,它们是以英文硬编码在Spring Security的代码中的。在Spring-Security-core-x
近来工作中遇到这样的两个需求
1. 给个Date对象,找出该时间所在月的第一天和最后一天
2. 给个Date对象,找出该时间所在周的第一天和最后一天
需求1中的找月第一天很简单,我记得api中有setDate方法可以使用
使用setDate方法前,先看看getDate
var date = new Date();
console.log(date);
// Sat J
MyBatis的update元素的用法与insert元素基本相同,因此本篇不打算重复了。本篇仅记录批量update操作的
sql语句,懂得SQL语句,那么MyBatis部分的操作就简单了。 注意:下列批量更新语句都是作为一个事务整体执行,要不全部成功,要不全部回滚。
MSSQL的SQL语句
WITH R AS(
SELECT 'John' as name, 18 as