Collection与Collections

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

发表评论

表情:
评论列表 (有 0 条评论,89人围观)

还没有评论,来说两句吧...

相关阅读

    相关 collections

    出自:[廖雪峰的博客][Link 1] collections是Python内建的一个集合模块,提供了许多有用的集合类。 OrderedDict是dict的子类,它记住了内