在开发中,我们会用到List集合去传递参数,较为常用的方式是List携带子集map。
List<Map<String, String>> myList = new ArrayList<Map<String, String>>();
我们常常会根据需求对List做一些操作,除了常用的Add()方法,我们也可能用到一些排序、删除的方法。
后面是详细的示例,这里先列出关键方法的代码:
1.按Map元素中子元素的值来排序List
Collections.sort(myList, (Map<String, String> m1, Map<String, String> m2) -> m1.get("score").compareTo(m2.get("score")));
2.翻转List当前的排序
Collections.reverse(myList);
3.随机打乱List顺序 (每次执行的结果 不一样)
Collections.shuffle(myList);
4.根据条件移除list中元素
myList.removeIf(mp -> Integer.parseInt(mp.get("score"))<3);
5.根据索引移除list中的元素
myList.remove(0);
示例代码:
public static void main(String[] args) {
List<Map<String, String>> myList = new ArrayList<Map<String, String>>();
HashMap<String, String> map2 = new HashMap<>();
map2.put("name", "二毛");
map2.put("score", "2");
HashMap<String, String> map3 = new HashMap<>();
map3.put("name", "狗蛋");
map3.put("score", "3");
HashMap<String, String> map = new HashMap<>();
map.put("name", "小明");
map.put("score", "1");
HashMap<String, String> map4 = new HashMap<>();
map4.put("name", "牙牙");
map4.put("score", "4");
myList.add(map2);
myList.add(map3);
myList.add(map);
myList.add(map4);
System.out.println("排序前:"+myList);
//排序前:[{score=2, name=二毛}, {score=3, name=狗蛋}, {score=1, name=小明}, {score=4, name=牙牙}]
//------------------------------------------------------
// TODO :按Map元素里的 score 进行排序
Collections.sort(myList, (Map<String, String> m1, Map<String, String> m2) -> m1.get("score").compareTo(m2.get("score")));
System.out.println("排序后:"+myList);
//排序后:[{score=1, name=小明}, {score=2, name=二毛}, {score=3, name=狗蛋}, {score=4, name=牙牙}]
//------------------------------------------------------
// TODO: 翻转list当前的排序
Collections.reverse(myList);
System.out.println("翻转排序后:"+myList);
//翻转排序后:[{score=4, name=牙牙}, {score=3, name=狗蛋}, {score=2, name=二毛}, {score=1, name=小明}]
//--------------------------------------------------------
// TODO :随机打乱顺序 (每次执行的结果 不一样)
Collections.shuffle(myList);
System.out.println("打乱顺序: "+myList);
//打乱顺序: [{score=2, name=二毛}, {score=1, name=小明}, {score=3, name=狗蛋}, {score=4, name=牙牙}]
//-------------------------------------------------------
// TODO :根据条件移除list中元素: 移除 score小于3的map元素
myList.removeIf(mp -> Integer.parseInt(mp.get("score"))<3);
System.out.println("按条件移除后: "+myList);
//按条件移除后: [{score=4, name=牙牙}, {score=3, name=狗蛋}]
//----------------------------------------------------------------------
// TODO :根据索引移除list中的元素
myList.remove(0);
System.out.println("移除索引0后: "+myList);
//移除索引0后: [{score=4, name=牙牙}]
}
更多关于集合的操作可以参考:
https://www.sxt.cn/util/java_util_collections.html
6.List、Map的深克隆
list和map是引用类型数据结构,常常因为复制了他们,而出现一系列问题。所以在复制的时候,我们要用深克隆来切断他和复制品的联系。
//Map的深克隆(真深克隆,用序列化处理。)
public <T extends Serializable> T clone(Map<String, Map> obj) {
T clonedObj = null;
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(obj);
oos.close();
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bais);
clonedObj = (T) ois.readObject();
ois.close();
} catch (Exception e) {
e.printStackTrace();
}
return clonedObj;
}
//List的深克隆
public <T> List<T> deepCopy(List<T> src) throws Exception{
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(byteOut);
out.writeObject(src);
ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray());
ObjectInputStream in = new ObjectInputStream(byteIn);
@SuppressWarnings("unchecked")
List<T> dest = (List<T>) in.readObject();
return dest;
}