添加方法:list.add(list2)
获取方法:list.get(index)
按索引删除:list.remove(index)
按元素内容删除:list.remove(Obj o)
清空集合元素数值:list.clear()
List<String> person=new ArrayList<>();
//添加
person.add("jackie");
person.add("peter");
person.add("annie");
person.add("martin");
person.add("marry");
//删除
person.remove(3); //.remove(index)
person.remove("marry"); //.remove(Object o)
//获取 person 集合中索引为 1 的元素
String per = person.get(1);
//清空
person.clear() //此时 person 集合为空集合,相当于刚开始 new 出来的 person 集合
获取长度:list.size()
判断集合是否为空:list.isEmpty()
判断集合中是否包含指定的元素:list.contains(Object object)
List<String> list1 = new ArrayList<>();
List<String> list2 = new ArrayList<>();
List<String> list3 = new ArrayList<>();
list1.add("123456");
list2.add("");
list3.add("annie");
int num = list1.size(); // num = 6
boolean a = list2.isEmpty(); // a = true
boolean b = list3.contains("b"); // b = false
注意 list.set(index, element);和 list.add(index, element);的不同。
String a="白龙马", b="沙和尚", c="八戒", d="唐僧", e="悟空";
List<String> people=new ArrayList<>();
people.add(a);
people.add(b);
people.add(c);
people.set(0,d); //.set(index,element);
people.add(1,e); //.add(index,element)
//增强for循环遍历list
for(String str:people){
System.out.println(str);
}
将集合转换为数组:list.toArray(),这里有两种方法:
List list = new ArrayList();
list.add("1");
list.add("2");
list.add("3");
//第一种
String[] arr1 = new String[list.size()];
list.toArray(arr1);
for (int i = 0; i < arr1.length; i++) {
System.out.println(arr1[i]);
}
//第二种
String[] arr2 = (String[])list.toArray(new String[0]);
for (int i = 0; i < arr2.length; i++) {
System.out.println(arr2[i]);
}
public class Stu {
//编号
private Integer id;
//分数
private Integer score;
public Stu(Integer id, Integer score) {
this.id = id;
this.score = score;
}
// getter、setter大家自己补上
@Override
public String toString() {
return "Stu{" +
"id=" + id +
", score=" + score +
'}';
}
}
List<Stu> stuList = new ArrayList<>();
stuList.add(new Stu(2, 30));
stuList.add(new Stu(1, 30));
stuList.add(new Stu(3, 50));
stuList.add(new Stu(4, 40));
//需求:直接将集合倒序输出
Collections.reverse(list);
//没有创建新的集合,使用的还是原来的集合list
//结果
Stu{id=4, score=40}
Stu{id=3, score=50}
Stu{id=1, score=30}
Stu{id=2, score=30}
//需求:id 升序 对 stuList 中的学生信息进行排序,然后输出结果
stuList.sort((x, y) -> Double.compare(x.getId(), y.getId()));
//结果
Stu{id=1, score=30}
Stu{id=2, score=30}
Stu{id=3, score=50}
Stu{id=4, score=40}
//需求:score 降序,id 升序 对 stuList 中的学生信息进行排序,然后输出结果
List<Stu> stuSortList = stuList.stream().sorted(Comparator.comparing(Stu::getScore).reversed().thenComparing(Stu::getId)).collect(Collectors.toList());
//打印结果
System.out.println(stuSortList);
//结果
Stu{id=3, score=50}
Stu{id=4, score=40}
Stu{id=1, score=30}
Stu{id=2, score=30}
这里使用到了 java 中的 stream 来操作的,stream 中有个 sorted 方法,需传入比较器:Comparator,解释一下代码
//得到 score 升序比较器
Comparator.comparing(Stu::getScore)
//将 score 升序比较器反转,那么就得到 score 降序比较器
Comparator.comparing(Stu::getScore).reversed()
//注意 thenComparing,表示前面的比较器得到结果相同的时候,走 thenComparing 函数指定的比较方式,即按照 Stu 中的 id 升序排序。
Comparator.comparing(Stu::getScore).reversed().thenComparing(Stu::getId)
public class User(){
private String name;
private int age;
private String sex;
}
List<User> list = new ArrayList<>();
User user1 = new User("张三",15,"男");
User user2 = new User("李四",22,"男");
User user3 = new User("王五",25,"男");
User user4 = new User("赵六",30,"男");
User user5 = new User("李世民",28,"男");
User user6 = new User("杨贵妃",18,"女");
User user7 = new User("貂蝉",16,"女");
list.add(user1);
list.add(user2);
list.add(user3);
list.add(user4);
list.add(user5);
list.add(user6);
list.add(user7);
List<User> collect = list.stream()
.filter(user -> !"张三".equals(user.getName()))
.collect(Collectors.toList());
List<User> collect = list.stream()
.filter(user -> user.getAge()<30 && "女".equals(user.getSex()))
.collect(Collectors.toList());
注意:使用该方法有一个大前提:List中的数据没有重复!
public class Main {
public static void main(String[] args) {
List<Integer> list = new ArrayList<>();
for (int i = 0; i <= 105; i++) {
list.add(i);
}
doBatch(list);
}
private static void doBatch(List<Integer> list) {
if (list.size() == 0) {
return;
}
int perCount = 20;
int index = 1;
int times = list.size() / perCount + 1;
System.out.println("总长度:" + list.size() + ", 共需要执行:" + times + "次");
do {
if (list.size() == 0) {
break;
}
System.out.println("第 " + index + " 轮>>, 剩余数量:" + list.size());
List<Integer> tempList;
if (list.size() > perCount) {
tempList = list.subList(0, perCount);
} else {
tempList = list.subList(0, list.size());
}
deal(tempList);
index++;
list.removeAll(tempList);
} while (index <= times);
System.out.println("结束!");
}
private static void deal(List<Integer> list) {
System.out.println(Arrays.toString(list.toArray()));
}
}
//运行结果
总长度:106, 共需要执行:6次
第 1 轮>>, 剩余数量:106
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
第 2 轮>>, 剩余数量:86
[20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39]
第 3 轮>>, 剩余数量:66
[40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59]
第 4 轮>>, 剩余数量:46
[60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79]
第 5 轮>>, 剩余数量:26
[80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]
第 6 轮>>, 剩余数量:6
[100, 101, 102, 103, 104, 105]
结束!
好事定律:每件事最后都会是好事,如果不是好事,说明还没到最后。