引言:更多相关请看 Java其它
概述:可根据List的对象的属性进行排序,代码如下:
public class SortList<E> {
public void Sort(List<E> list, final String method, final String sort){
Collections.sort(list, new Comparator() {
public int compare(Object a, Object b) {
int ret = 0;
try{
Method m1 = ((E)a).getClass().getMethod(method, null);
Method m2 = ((E)b).getClass().getMethod(method, null);
if(sort != null && "desc".equals(sort))//倒序
ret = m2.invoke(((E)b), null).toString().compareTo(m1.invoke(((E)a), null).toString());
else//正序
ret = m1.invoke(((E)a), null).toString().compareTo(m2.invoke(((E)b), null).toString());
}catch(NoSuchMethodException ne){
System.out.println(ne);
}catch(IllegalAccessException ie){
System.out.println(ie);
}catch(InvocationTargetException it){
System.out.println(it);
}
return ret;
}
});
}
}
测试类:
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class test {
public static void main(String[] args) throws Exception {
// List<UserInfo> list = new ArrayList<UserInfo>();
//
// SimpleDateFormat formater = new SimpleDateFormat("yyyy-MM-dd");
//
// list.add(new UserInfo(3,"b",formater.parse("1980-12-01"),11));
// list.add(new UserInfo(1,"c",formater.parse("1980-10-01"),30));
// list.add(new UserInfo(2,"a",formater.parse("1973-10-01"),11));
//
// System.out.println("-------原来序列-------------------");
// for(UserInfo user : list){
// System.out.println(user.toString());
// }
//
// //调用排序通用类
// SortList<UserInfo> sortList = new SortList<UserInfo>();
//
//
// List<UserInfo> list1 = new ArrayList<>();
//// list1.Sort
//
// //按userId排序
// sortList.Sort(list, "getUserId", "desc");
// System.out.println("--------按userId倒序------------------");
// for(UserInfo user : list){
// System.out.println(user.toString());
// }
//
// //按username排序
// sortList.Sort(list, "getUsername", null);
// System.out.println("---------按username排序-----------------");
// for(UserInfo user : list){
// System.out.println(user.toString());
// }
//
// //按birthDate排序
// sortList.Sort(list, "getBirthDatestr", null);
// System.out.println("---------按birthDate排序-----------------");
// for(UserInfo user : list){
// System.out.println(user.toString());
// }
//
// BigDecimal bg = BigDecimal.valueOf(1);
// BigDecimal om2 = BigDecimal.valueOf(25);
// System.out.println(om2.divideAndRemainder(bg)[1]);//取余
/**
* list中有150条数据,每次只拿20条数据,直到拿完所有数据
*/
// List<String> urlList = new ArrayList<String>();
// for (int i = 0; i < 36784; i++) {
// urlList.add(i+"");
// }
//
//
// for(int i = 0;i < urlList.size();i+=50000){
// List<String> param = new ArrayList<String>();//用户存放每次获取到的二十条数据
// if( i + 50000 < urlList.size()){
// for (int j = i; j < i + 50000; j++) {
// param.add(urlList.get(j));
// }
// }else {
// for (int j = i; j < urlList.size() ; j++) {
// param.add(urlList.get(j));
// }
// }
// System.out.println("每次获取到的20条数据:" + param);
// }
//根据map的value获取map的key
// Map<String, String> map = new HashMap<>();
// map.put("1", "a");
// map.put("2", "b");
// map.put("3", "a");
// map.put("4", "c");
//
// System.out.println("输出的key为:" + getKey(map, "a"));
//
// }
//
// public static Object getKey(Map map, Object value){
// Set set = map.entrySet(); //通过entrySet()方法把map中的每个键值对变成对应成Set集合中的一个对象
// Iterator<Map.Entry<Object, Object>> iterator = set.iterator();
// ArrayList<Object> arrayList = new ArrayList();
// while(iterator.hasNext()){
// //Map.Entry是一种类型,指向map中的一个键值对组成的对象
// Map.Entry<Object, Object> entry = iterator.next();
// if(entry.getValue().equals(value)){
// arrayList.add(entry.getKey());
// }
// }
// return arrayList;
// }
// public static Object getKey(Map map, Object value){
// List<Object> keyList = new ArrayList<>();
// for(Object key: map.keySet()){
// if(map.get(key).equals(value)){
// keyList.add(key);
// }
// }
// return keyList;
// }
List<String> list = new ArrayList<>();
for (int i = 0; i < 5300; i++) {
list.add("" + i);
}
int threadSize = 500;//每500条数据开启一个线程
int remainder = list.size() % threadSize;
int threadNum = 0; //线程数
if (remainder == 0) {
threadNum = list.size() / threadSize;
} else {
threadNum = list.size() / threadSize + 1;
}
ExecutorService eService = Executors.newFixedThreadPool(threadNum);//创建一个线程池
List<Callable<String>> cList = new ArrayList<>();
Callable<String> task = null;
List<String> sList = null;
for (int i = 0; i < threadNum; i++) {
if (i == threadNum - 1) {
sList = list.subList(i * threadSize, list.size());
} else {
sList = list.subList(i * threadSize, (i + 1) * threadSize);
}
final List<String> nowList = sList;
task = new Callable<String>() {
@Override
public String call() throws Exception {
StringBuffer sb = new StringBuffer();
for (int j = 0; j < nowList.size(); j++) {
sb.append("" + nowList.get(j));
}
return sb.toString();
}
};
cList.add(task);
}
List<Future<String>> results = eService.invokeAll(cList);
for (Future<String> str : results) {
System.out.println(str.get());
}
eService.shutdown();
}
}