一、Map实用方法
1.1同时获取key与value做对应操作
(1)采用for循环
//对应泛型中的key与value按具体业务来
Map<String,Integer>map=Maps.newHashMap();
map.put("测试1", 2);
map.put("测试2", 1);
String key = null;
Integer value = null;
for(Map.Entry<String,
Integer> entry : map.entrySet()){
key = entry.getKey();
value = entry.getValue();
//处理逻辑
}
(2)采用Iterator遍历(不推荐)
Map<String,Integer>map=Maps.newHashMap();
map.put("测试1", 2);
map.put("测试2", 1);
Iterator<Entry<String,
Integer>> iterator = map.entrySet().iterator();
String key = null;
Integer value = null;
while(iterator.hasNext()){
Entryentry = iterator.next();
key = entry.getKey();
value = entry.getValue();
//处理逻辑
}
1.2只获取map中的key
Map<String,Integer>map=Maps.newHashMap();
map.put("测试1", 2);
map.put("测试2", 1);
//只获取key
for(String key : map.keySet()){
//处理逻辑
}
1.3只获取map中的value
Map<String,Integer>map=Maps.newHashMap();
map.put("测试1", 2);
map.put("测试2", 1);
//只获取value
for(Integer value : map.values()){
//处理逻辑
}
二、字符串拼接
//StringBuffer拼接字符串方式
StringBuffer sr = new StringBuffer();
for (int i = 0; i < 2; i++) {
sr.append(String.valueOf(i)).append(",");
}
//转换字符串
String srStr = sr.toString();
//去掉最后拼接在逗号
srStr = srStr.substring(0, srStr.length()-1);
三、static final常量List、Map
/**
*对应描述
*/
public static final List<String> TEST_LIST= Arrays.asList("57", "58");
/**
*对应描述
*/
public static final Map<Integer, String> TEST_MAP= Collections.unmodifiableMap(new HashMap<Integer, String>() {
private static final long serialVersionUID = 1L;
{
put(0,"1");
put(1,"2");
}
});
四、适用于批量切割逻辑的Lists.partition
对应引入jar地址:
importcom.google.common.collect.Lists;
List<String> list= Lists.newArrayList();
list.add("1");
list.add("2");
//按每次1个分
List<List<String>> partList=Lists.partition(list, 1);
for(List<String> part : partList) {
//处理逻辑
}
五、JSONObject的转换
对应引入jar地址:
importcom.alibaba.fastjson.JSONObject;
转换单个对象:
(1)
TestEntity test = new TestEntity();
test.setId("123");
//对象转JSON格式
String jsonStr= JSONObject.toJSONString(test);
//JSON转对象
test = JSONObject.parseObject(jsonStr,TestEntity.class);
//对象不为空
if(test != null) {
System.out.print("sssssssssssssss====="+test.getId());
}
(2)
//接口获取的json格式字符,obj对应的内容为对象格式
String jsonStr = "{\"success\":true,\"status\":false,\"code\":\"0\",\"obj\":{\"id\":\"123\"}}";
JSONObject json= JSONObject.parseObject(jsonStr);
TestEntity test = null;
if(json != null && json.get("obj") != null) {
test = json.getObject("obj",TestEntity.class); System.out.print("sssssssssssssss====="+test.getId());
}
转换集合对象:
(1)
TestEntity test = new TestEntity();
test.setId("123");
List<TestEntity> list= Lists.newArrayList();
list.add(test);
//集合对象转JSON格式
String jsonStr= JSONObject.toJSONString(list);
List<TestEntity> testList= JSONObject.parseArray(jsonStr, TestEntity.class);
if(CollectionUtils.isNotEmpty(testList)) {
System.out.print("sssssssssssssss====="+testList.get(0).getId());
}
(2)
//接口获取的json格式字符,obj对应的内容为集合对象格式
String jsonStr = "{\"success\":true,\"status\":false,\"code\":\"0\",\"obj\":[{\"id\":\"123\"}]}";
JSONObject json= JSONObject.parseObject(jsonStr);
List<TestEntity> testList = null;
if(json != null && json.getJSONArray("obj") != null) {
testList = json.getJSONArray("obj").toJavaList(TestEntity.class);
if(CollectionUtils.isNotEmpty(testList)) {
System.out.print("sssssssssssssss====="+testList.get(0).getId());
}
}
****************************************
Gson的转换
对应引入jar地址:
import com.google.gson.Gson;
转换单个对象:
(1)
TestEntitytest = new TestEntity();
test.setId("123");
Gson gson = newGson();
//对象转JSON格式
String jsonStr = gson.toJson(test);
//JSON转对象
test = gson.fromJson(jsonStr, TestEntity.class);
//对象不为空
if(test != null) {
System.out.println("sssssssssssssss====="+test.getId());
}
(2)
//接口获取的json格式字符,obj对应的内容为对象格式
StringjsonStr = "{\"success\":true,\"status\":false,\"code\":\"0\",\"obj\":{\"id\":\"123\"}}";
Gsongson = new Gson();
JsonObjectobj = gson.fromJson(jsonStr, JsonObject.class);
if(obj != null && obj.get("obj") != null) {
TestEntitytest = gson.fromJson(obj.get("obj").toString(), TestEntity.class);
System.out.println("sssssssssssssss====="+test.getId());
}
转换集合对象:
(1)
TestEntitytest = new TestEntity();
test.setId("123");
Listlist= Lists.newArrayList();
list.add(test);
Gsongson = new Gson();
//集合对象转JSON格式
StringjsonStr = gson.toJson(list);
ListtestList = gson.fromJson(jsonStr, new TypeToken<List<TestEntity>>()
{}.getType());
if(CollectionUtils.isNotEmpty(testList)) {
System.out.println("sssssssssssssss====="+testList.get(0).getId());
}
(2)
//接口获取的json格式字符,obj对应的内容为集合对象格式
String jsonStr = "{\"success\":true,\"status\":false,\"code\":\"0\",\"obj\":[{\"id\":\"123\"}]}";
Gson gson = new Gson();
JsonObjectobj = gson.fromJson(jsonStr, JsonObject.class);
if(obj != null && obj.get("obj") != null) {
List testList = gson.fromJson(obj.get("obj").toString(), new TypeToken<List<TestEntity>>()
{}.getType());
if(CollectionUtils.isNotEmpty(testList)) {
System.out.println("sssssssssssssss====="+testList.get(0).getId());
}
}
六、Java8排序
TestEntity test = newTestEntity();
test.setId("222");
test.setName("a");
test.setSort(1);
Listlist = Lists.newArrayList();
list.add(test);
test = newTestEntity();
test.setId("123");
test.setName("b");
test.setSort(2);
list.add(test);
test = new TestEntity();
test.setId("333");
test.setName("c");
test.setSort(2);
list.add(test);
// 按sort升序
ComparatorsortASC= Comparator.comparing(TestEntity::getSort);
list = list.stream().sorted(sortASC).collect(Collectors.toList());
//按name降序
ComparatornameDESC= Comparator.comparing(TestEntity::getName).reversed();
list = list.stream().sorted(nameDESC).collect(Collectors.toList());
// 按sort与name联合排序
ComparatorunionComparator = sortASC.thenComparing(nameDESC);
list = list.stream().sorted(unionComparator).collect(Collectors.toList());
for(TestEntityty : list) {
System.out.println(ty.getId());
}
七、Java8获取集合对象中字段集合
TestEntity test = newTestEntity();
test.setId("222");
Listlist = Lists.newArrayList();
list.add(test);
test = newTestEntity();
test.setId("123");
list.add(test);
//获取id集合
List<String> ids = list.stream().map(TestEntity::getId).collect(Collectors.toList());
for(String str : ids) {
System.out.println(str);
}
八、Java8按字段分组
TestEntity test = newTestEntity();
test.setId("222");
test.setName("sss");
Listlist = Lists.newArrayList();
list.add(test);
test = newTestEntity();
test.setId("123");
test.setName("sss");
list.add(test);
test = new TestEntity();
test.setId("3");
test.setName("3");
list.add(test);
//按name分组
Map<String, List<TestEntity>> collect = list.stream()
.collect(Collectors.groupingBy(TestEntity::getName));
for(Map.Entry<String, List<TestEntity>> entry : collect.entrySet()){
System.out.println(entry.getKey());
}
九、分页参数查询循环处理
注意点:如果涉及更新查询出来的数据,则更新的字段不能为条件里面的字段
boolean tag = false;
int pageno = 0;
int start = 0;
// 一次查询的条数
int end= 1000;
pageno = 0;
start = 0;
// 查询需要处理的数据
List<TestEntity> list = null;
do {
MapparaMap= Maps.newHashMap();
paraMap.put("beginIndex", start);
paraMap.put("endIndex", end);
try {
// 查询需要处理的数据
list = testMapper.selectUntreatedList(paraMap);
if(CollectionUtils.isNotEmpty(list)) {
//处理逻辑
//判断更新变量
if (list.size() == end) {
pageno = pageno + 1;
start = (pageno) * end;
tag = true;
}else {
tag = false;
}
}else {
tag = false;
}
}catch (Exception e) {
//错误处理
}
} while (tag);