1集合的概念
- 把集合看做是一个容器,集合不是一个类,是一套集合框架,框架体系包含很多的集合类,java api提供了
- 集合存储任意类型(基本包装类型,引用类型)
- 集合对元素的增删查改操作起来非常方便的,专门提供很多方便操作的方法
- 开发中经常集合来操作数据的,开发地位比较高的,考虑对数据的操作效率上的问题
2 集合的父类Collection接口
2.1常用方法
- boolean add(E e) 添加某个元素
- addAll(元素) 添加一个集合
- remove(object) 删除一个元素
- removeAll(collection) 删除一个集合
- c.clear(); //移除所有
- isEmpty() 判断是否为空
public static void main(String[] args) {
Collection c = new ArrayList();
c.add("张三");
c.add(10);
c.add(false);
c.add('a');
System.out.println(c);
Collection c2 = new ArrayList();
c2.add("hello");
c2.add("world");
c2.add("java");
c.addAll(c2);
System.out.println(c);
System.out.println("删除张三元素是否成功:" + c.remove("张三2"));
System.out.println(c);
c.removeAll(c2);
System.out.println("移除c2集合:" + c);
System.out.println(c.isEmpty());
System.out.println(c.contains("张三222"));
System.out.println("获取集合中的元素个数:" + c.size());
}
2.2遍历
- 使用foreach 遍历
- 使用迭代器遍历
- 使用toArray()方法转成数组,再遍历.
- 使用for遍历
public static void main(String[] args) {
Collection c = new ArrayList();
c.add("张三");
c.add("李四");
c.add("王五");
System.out.println(c);
System.out.println("-------------1");
for(Object obj : c){
System.out.println(obj);
}
System.out.println("-------------2");
Iterator it = c.iterator();
while(it.hasNext()){
Object obj = it.next();
System.out.println(obj);
}
System.out.println("-------------3");
Object[] objs = c.toArray();
for(Object o : objs){
System.out.println(o);
}
System.out.println("-------------4");
for(Iterator iterator = c.iterator();iterator.hasNext();){
Object obj = iterator.next();
System.out.println(obj);
}
System.out.println("-------------5");
for(Iterator iterator = c.iterator();iterator.hasNext(); System.out.println(iterator.next())){}
}
3 List接口
List接口继承了Collection接口,所以Collection接口的方法List都可以使用.
3.1List接口常用方法
添加
- boolean add(E e) 添加一个元素
- void add(int index, E element) 添加某个元素到指定位置
boolean addAll(Collection<? extends E> c) 添加一个集合 - boolean-addAll(int index, Collection<? extends E> c) 添加一个集合到指定位置
删除
- E remove(int index) 删除指定位置的元素
- boolean remove(Object o) 删除某个元素
- boolean removeAll(Collection<?> c) 删除一个集合
修改
- E set(int index, E element)
获取
- E get(int index)
- int size()
- 其他
- int indexOf(Object o)
- int lastIndexOf(Object o)
- List subList(int fromIndex, int toIndex)
3.2遍历
- E get(int index) + int size()
- ListIterator listIterator()
- ListIterator listIterator(int index)
员工类:
package listdemo;
import java.util.Objects;
public class Employee {
private Integer id;
private String name;
private Integer age;
private Double salary;
public Employee() {
}
public Employee(Integer id, String name, Integer age, Double salary) {
this.id = id;
this.name = name;
this.age = age;
this.salary = salary;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Double getSalary() {
return salary;
}
public void setSalary(Double salary) {
this.salary = salary;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Employee employee = (Employee) o;
return Objects.equals(id, employee.id) &&
Objects.equals(name, employee.name) &&
Objects.equals(age, employee.age) &&
Objects.equals(salary, employee.salary);
}
@Override
public int hashCode() {
return Objects.hash(id, name, age, salary);
}
@Override
public String toString() {
return "Employee{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", salary=" + salary +
'}';
}
}
public static void main(String[] args) {
List list = new ArrayList();
list.add("hello");
list.add("java");
list.add(1,"world");
list.add("html");
System.out.println(list);
List list2 = new ArrayList();
list2.add("test1");
list2.add("test2");
list2.add("test3");
list.addAll(2,list2);
System.out.println(list);
System.out.println(list.remove("java"));
System.out.println(list);
Object obj = list.remove(1);
System.out.println(obj);
System.out.println(list);
list.removeAll(list2);
System.out.println("删除新集合之后:" + list);
Object o = list.set(1, "编程语言");
System.out.println(o);
System.out.println("修改之后:" + list);
List list3 = new ArrayList();
list3.add("张三");
list3.add("李四");
list3.add("王五");
list3.add("赵六");
list3.add("小编");
int i = list3.indexOf("李四2222");
System.out.println("李四的位置:" + i);
System.out.println("找王五的下标位置:" + list3.lastIndexOf("王五"));
System.out.println(list3);
List newsList = list3.subList(1, 4);
System.out.println(newsList);
}
List的遍历
- E get(int index) + int size()
- ListIterator listIterator()
- ListIterator listIterator(int index)
- iterator();
- toArray();
- foreach
- toString
- for()
- for()
package listdemo;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
public class ListDemo02 {
public static void main(String[] args) {
List list = new ArrayList();
list.add("张三");
list.add("李四");
list.add("王五");
ListIterator listIterator1 = list.listIterator(1);
if (listIterator1.hasNext()){
System.out.println("从第一个元素开始取出:" +listIterator1.next());
}
if (listIterator1.hasNext()){
System.out.println("继续取下一个元素:" +listIterator1.next());
}
System.out.println(list.size());
for (int i = 0; i < list.size(); i++) {
Object o1 = list.get(i);
System.out.println(o1);
}
List list2 = new ArrayList();
list2.add("hello");
list2.add("world");
list2.add("java");
ListIterator listIterator = list2.listIterator();
while (listIterator.hasNext()) {
Object o = listIterator.next();
System.out.println(o);
}
System.out.println("------------------------");
while(listIterator.hasPrevious()){
Object obj = listIterator.previous();
System.out.println(obj);
}
}
}
4 集合中元素的去重
package listdemo;
import java.util.ArrayList;
import java.util.List;
public class ListDemo03 {
public static void main(String[] args) {
List list = new ArrayList();
list.add(new Employee(101,"张三",20,2000.0));
list.add(new Employee(102,"李四",30,3000.0));
list.add(new Employee(103,"王五",20,2000.0));
list.add(new Employee(103,"王五",20,2000.0));
List newList = new ArrayList();
for(Object obj : list){
if (!newList.contains(obj)){
newList.add(obj);
}
}
list = newList;
for(Object obj : list){
System.out.println(obj);
}
for (int i = 0; i < list.size(); i++) {
for (int j = i+1; j < list.size(); j++) {
if (list.get(i).equals(list.get(j))){
list.remove(j);
j--;
}
}
}
for(Object obj : list){
System.out.println(obj);
}
}
}
|