*1,什么是集合(Collection)
* 集合是一个大小可变的容器
* 容器中的每个数据称为一个元素,数据==元素
* 集合的特点是,类型可以不确定,大小不固定,集合有很多种类,不同集合特点和使用场景* 不同
* 数组:类型和长度一旦定义出就固定了
* 2,集合作用:
* 在开发过程中,很多时候元素的个数是不确定的
* 而且经常要进行元素的增删改查错做,集合都是非常合适的
* 在开发中集合用的更多
* java中集合的代表:Collection 是java中的集合的祖宗类
* Collection<>(接口)
* / \
* Set<E>(接口) List<E>(接口)
* / \ / \
* HashSet<E>(实现类) TreeSet<> ArrayList<E> LinkedList<E> Vector
* / (实现类) (实现类) (实现类)
* LinkedHashSet<>(实现类)
*
*3,集合的特点
* Set系列集合:添加的元素是无序的,不重复,无索引
* --HashSet:无序的,不重复,无索引
* --LinkedHashSet:有序,不重复,无索引
* --TreeSet:按照大小默认升序,不重复,无索引
* List系列集合:添加的元素是有序,不重复,无索引
* --ArrayList:
* --LinkedList:
* Collection是集合的祖宗类,Collection集合的功能是一切集合都可以直接使用的
*4,功能:
* public boolean add(): 把给定的对象添加到当前集合中
* public void clear(): 清空集合中的所有元素
* public boolean remove(): 把给定的对象在当前集合中删除//list如果重复默认删除** 第一个
* public boolean isEmpty(): 判断当前集合是否为空
* public int size(): 返回集合中元素的个数
* public Object[] toArray(): 把集合中的元素,存储到数组中
*5,Collection集合遍历的三种方式
* >迭代器
* >foreach(增强for循环)
* >jdk1.8开始之后的Lambda表达式
* a,迭代器遍历集合
* public Iterator iterator();获取集合对应的迭代器,u哦你过来遍历集合中的元素
* E next():获取下一个元件的值
* Boolean hasNext():判断是否有下一个元素,有就返回true
* 流程:
* 1,Iterator<String> it = list.iterator();
* 2定义一个while循环,通过it.hasNext()询问是否有下一个元素,有就通过it.next()去** 除下一个元素
* b,foreach(增强for循环)
* foreach是一种遍历形式,可以遍历集合或者数组
* foreach遍历集合实际上是迭代器遍历的减缓写法
* foreach关键是记住格式
* for(被便利集合或者数组中的元素的类型 变量名称 : 被遍历集合或者数组){
*
* }
* 缺点:foreach遍历无法知道遍历到了哪个元素,因为没有索引
*
public class DemoCollection {
public static void main(String[] args) {
//HashSet:无序的,不重复,无索引
Collection<String> string = new HashSet<>();
string.add("Mysql");
string.add("Mysql");
string.add("Java");
string.add("Java");
System.out.println(string);//[Java, Mysql]
//ArrayList:添加的元素是有序,不重复,无索引
List<String> list = new ArrayList<>();
list.add("mysql");
list.add("mysql");
list.add("java");
list.add("java");
System.out.println(list);//[mysql, mysql, java, java]
Object[] arrs = list.toArray();
//String[] arr = list.toArray(String::new);方法引用:指定转换的数组类型
System.out.println("数组"+ Arrays.toString(arrs));
//1,得到集合的迭代器对象
Iterator<String> it = list.iterator();
while (it.hasNext()){
System.out.println(it.next());
}
//2,foreach
for (String s : list) {
System.out.println(s);
}
for (Object arr : arrs) {
System.out.println(arr);
}
//3,Lambda
list.forEach(s -> System.out.println());
list.forEach(System.out::println);
}
}
java.utils.Collections:是集合工具类
* 并不属于集合,是用来操作集合的工具类
* 常用API
* --public static <T> boolean addAll(Collection<? super ?> c,T...elements):给* 集合对象批量添加元素
* --public static void shuffle(List<?> list):打乱集合顺序
* --public static <T> void sort(List<T> list):将集合中的元素按照默认规则排序
* 将集合中元素按照默认规则排序
* 对于自定义的引用类型的排序直接报错
* 不报错,提供比较规则Com[arable
* --public static <T> list,Comparator<? super ?>:将集合中元素按照指定规则排序,* 自带比较器
* 将集合中元素按照指定规则排序,自带比较器
* 注意:如果类又比较规则,优先使用比较器
public class DemoCollections {
public static void main(String[] args) {
//1,给集合批量添加元素
Collection<String> name = new ArrayList<>();
Collections.addAll(name, "王五", "李四", "赵六","张三");//被添加元素的集合,可变参数
System.out.println(name);//[王五, 李四, 赵六, 张三]
//2,打乱集合的顺序,只能打乱有序List集合
List<String> names = new ArrayList<>();
Collections.addAll(names, "张三", "李四", "王二","麻子");
Collections.shuffle(names);
System.out.println(names);//[王二, 李四, 张三, 麻子]
//3,将集合中的元素按照默认规则排序
ArrayList<Double> scores = new ArrayList<>();
Collections.addAll(scores, 9.9,1.1,10.1,0.001,0.0001);
Collections.sort(scores);
System.out.println(scores);//[1.0E-4, 0.001, 1.1, 9.9, 10.1]
//4,将集合中元素按照指定规则排序,自带比较器
List<Employee> employees1 = new ArrayList<>();
//自定义类型排序
//优先使用方法自带的比较器对象Comparator而不会用类的比较规则
Employee aa = new Employee("张三", 250, 150);
Employee bb = new Employee("李四", 50, 140);
Employee cc = new Employee("王五", 150, 180);
Employee dd = new Employee("赵六", 520, 10);
Collections.addAll(employees1, aa,bb,cc,dd);
Collections.sort(employees1, new Comparator<Employee>() {
@Override
public int compare(Employee o1, Employee o2) {
//o1比较者,o2被比较者
return o1.getWeight() - o2.getWeight();
}
});
System.out.println(employees1);
//[Orange{name='赵六', sales=520.0, weight=10}, Orange{name='李四', sales=50.0,
weight=140}, Orange{name='张三', sales=250.0, weight=150}, Orange{name='王五',
sales=150.0, weight=180}]
}
}
class Employee implements Comparable<Employee>{
private String name;
private double sales;
private int weight;
public Employee(String name, double sales, int weight) {
this.name = name;
this.sales = sales;
this.weight = weight;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getSales() {
return sales;
}
public void setSales(double sales) {
this.sales = sales;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
@Override
public String toString() {
return "Orange{" +
"name='" + name + '\'' +
", sales=" + sales +
", weight=" + weight +
'}';
}
//重写比较方法 比较者:this,被比较者:o
@Override
public int compareTo(Employee o) {
return this.weight - o.weight;
}
}
还没有评论,来说两句吧...