集合的进阶

编程入门 行业动态 更新时间:2024-10-23 23:28:25

集合的<a href=https://www.elefans.com/category/jswz/34/1769503.html style=进阶"/>

集合的进阶

集合类体系

1.Collection

1.1 Collection 集合概述和使用

    Collention 集合概述:

            是单例集合的顶层接口,它表示一组对象,这些对象也称为Collention的元素。

           JDK 不提供此接口的任何直接实现,他提供更多具体的子接口(如:Set 和 List )实现。

    创建Collention集合的对象

           多态的方式。

          具体的实现类ArrayList。

Collention<String> c = new ArrayList<String>();

c.add("hello");

sout(c);

1.2 Collention 常用方法。

 1.3 Collention 集合的遍历

Iterator:迭代器,集合的专用遍历方式。

    Iterator<E> iterator():返回此集合中元素的迭代器,通过集合的iterator()方法得到。

    迭代器是通过集合的iterator()方法得到的,所以我们说它是依赖于集合而存在的。

Iterator中常用的方法

     E next() :返回迭代器中下一个元素。

     bollean hasNext(): 如果迭代器具有更多的元素,则返回true。

Iterator <String> it = c.iterator();

while(it.hasNext()){

      String s = it.next();

      sout(s);

}

2.List 

2.1 List集合概述和特点

List 集合概述

       有序集合(也称为序列)用户可以精确控制列表中的每一个元素的插入位置,用户可以通过整数索引访问元素,并搜索列表中的元素。

      于Set集不同,列表元素通常是允许重复的元素。

List 集合的特点:

     有序:存储和取出的元素顺序一致。

     可重复:存储的元素可以重复。

2.2 List集合特有方法

2.3并发修改异常

并发修改异常: ConcurrentModificationExcption

产生原因: 迭代器遍历的过程中,通过集合对象修改了集合中的元素,造成了迭代器获取元素中判断的预期修改值和实际修改值不一致。

解决方案:用for循环遍历,然后用集合做对应的操作即可。

2.4ListIterator

ListIterator:列表迭代器

      通过List集合的ListIterator()方法得到,所以说他是List集合的特有迭代器。

     用于允许程序员沿着任何一方向遍历列表迭代器,在迭代期间修改列表,并获取列表中迭代器的当前位置。

2.4.1ListIterator中的常用方法。

2.5增强for循环

增强for循环: 简化数组和Conllenction集合的遍历

     实现Iterable接口的类允许其对象成为增强型for语句的目标。

     它是JDK5之后出现的,其内部原理是一个Iterator迭代器。

 2.6 数据结构

数据结构是计算机存储,组织数据的方式,是指相互之间存在一种或多种特定的关系的数据数据元素的集合,通常情况下,精心选择的数据结构可以带来更高的运行或者存储效率。

 2.6.1常见数据结构之栈

  2.6.2常见数据结构之队列 

 2.6.3 常见数据结构之数组

2.6.4 常见数据结构之链表

 

2.7List集合子类特点

    List集合常用子类:ArrayList,LinkedList

       ArrayList :底层数据结构是数组,查询快,增删慢。

       LinkedList: 底层数据结构是链表,查询慢,增删快。

2.7.1 LinkedList集合的特有功能

3. Set

3.1 Set集合概述和特点

Set 集合特点

      不包含重复元素。

      没有索引的方法,所以不能使用普通for循环遍历。

  3.2 哈希值

哈希值:是JDK根据对象地址或者字符串或者数字算出来的int类型的值。

object类中有一个方法可以获得对象的哈希值。

   public int hashCode(): 返回对象的哈希码值。

对象的哈希值特点

   同一个对象多次调用hashCode()方法返回的哈希值是相同。

    默认情况下,不同对象的哈希值是不同的,而重写hashCode()方法,可以实现让不同对象的哈希值相同。

 3.3 HashSet集合概述和特点

  HashSet集合特点

       底层数据结构是哈希表。

       对集合的迭代顺序不做任何保证,也就是说不保证存储和取出的元素顺序一致。

       没有带索引的方法,所以不能使用普通for循环遍历。

       由于是Set集合,所以不包含重复元素。

3.4 HashSet集合保证元素唯一性源码分析

HashSet集合添加一个元素的过程:

 HashSet集合存储元素:

  要保证元素的唯一性,需要重写hashC ode()和equals()

 3.5 常见数据结构之哈希表

取余的值相同,就看哈希值,如果哈希值相同,就比较字符串,相同就不存储。

3.6 LinkedHashSet集合概述和特点

LinkedHashSet 集合特点:

        哈希表和链表实现的Set接口,具有可预测的迭代次序。

        由链表保证元素的有序,也就是说元素的存储和取出的顺序是一致的

        由哈希表保证元素唯一,也就是没有重复的元素。

 3.7 TreeSet集合概述和特点

   TreeSet集合特点:

         元素有序:不是指存储和取出的数据一致,而是按照一定的规则进行排序,具体的排序方法取决于构造方法。

                  TreeSet():根据其元素的自然排序进行排序。

                  TreeSet(Comparator comparator):根据指定的比较器进行比较。

      没有带索引的方法,所以不能使用普通for循环遍历。

      由于是Set集合,所以不包含重复元素的集合。

  3.8 自然排序Comparable的使用。

      存储学生对象并遍历,创建TreeSet集合使用无参构造方法。

     要求:按照年龄从小到大排序,年龄相同时,按照姓名的字母顺序排序。

package com.ListWeek;import java.util.TreeSet;public class Week03 {public static void main(String[] args) {TreeSet<Student> ts = new TreeSet<>();Student s1 = new Student("xishi",29);Student s2 = new Student("wanczhaojun",28);Student s3 = new Student("diaochan",30);Student s4 = new Student("yangyuhuan",33);Student s5 = new Student("lingqingxia",33);ts.add(s1);ts.add(s2);ts.add(s3);ts.add(s4);ts.add(s5);for (Student s : ts){System.out.println(s.getName()+" ,"+s.getAge());}}
}
class Student
implements Comparable<Student>
{private String name;private int age;public Student() {}public Student(String name, int age) {this.name = name;this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}@Overridepublic int compareTo(Student s) {//return 0;//元素相等。//return 1;//升序// return -1;//降序//int num  = s.age - this.age;//降序int num = this.age - s.getAge();//升序//当年龄相同比较名字。int num2 = num==0?this.namepareTo(s.name):num;return num2;}}

  3.9比较器排序Comparator的使用

存储学生对象并遍历,创建TreeSet集合使用带参构造的方法。

要求:按照年龄从小到大排序,年龄相同就比较名字字母的顺序。

package com.ListWeek;import java.util.Comparator;
import java.util.TreeSet;public class Week03 {public static void main(String[] args) {TreeSet<Student> ts = new TreeSet<Student>(new Comparator<Student>() {@Overridepublic int compare(Student s1, Student s2) {//this.age- s.age//s1-s2int num = s1.getAge()-s2.getAge();int num2 = num==0? s1.getName()pareTo(s2.getName()):num;return num2;}});Student s1 = new Student("xishi",29);Student s2 = new Student("wanczhaojun",28);Student s3 = new Student("diaochan",30);Student s4 = new Student("yangyuhuan",33);Student s5 = new Student("lingqingxia",33);ts.add(s1);ts.add(s2);ts.add(s3);ts.add(s4);ts.add(s5);for (Student s : ts){System.out.println(s.getName()+" ,"+s.getAge());}}
}
class Student {private String name;private int age;public Student() {}public Student(String name, int age) {this.name = name;this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}}

4. 泛型

4.1泛型的概述

 

 4.2泛型的好处

       把运行时期的问题提前到了编译时期。

       避免了类型强制转换。

 4.3 泛型类

 4.4泛型方法

 public class Generic{

        public <T> void show(T t){

                sout(t);

        }

}

Generic g = new Generic();

g.show("林青霞");

g.show(29);

g.show(true);

.............

4.4泛型接口

接口:

 public interface Generic<T>{

void show(T t);

}

实现类:

public class GenericImpi<T> implements Generic<T>{

        public void show(T t){

                sout(t);

}

测试类:

 4.5类型通配符

 4.6可变参数

4.7可变参数的使用

5. Map

5.1Map集合概述和使用

 5.2集合的基本功能

5.3Map集合的获取

 Set<K> keySet = map.keySet();

for(String key : keySet){

sout(key);

}

Collection<String> values = map. Values();

for(String value : values){

sout(value);

}

6.Collections

6.1 Collections概述和使用

概述:是针对集合的操作的工具类。

 

 

package com.ListWeek;import java.util.*;public class PokerDome {public static void main(String[] args) {HashMap<Integer,String> hm = new HashMap<>();ArrayList<Integer> array = new ArrayList<>();String[] colors = {"♦","♠","♥","♣"};String[] numbers = {"3","4","5","6","7","8","9","10","J","Q","K","A","2"};int index = 0;for (String number : numbers){for (String color : colors){hm.put(index,color+number);array.add(index);index++;}}hm.put(index,"小王");array.add(index);index++;hm.put(index,"大王");array.add(index);//洗牌(洗的是编号)Collections.shuffle(array);//发牌(发的也是编号)TreeSet<Integer> lqxSet = new TreeSet<>();TreeSet<Integer> lySet = new TreeSet<>();TreeSet<Integer> fqySet = new TreeSet<>();TreeSet<Integer> dpSet = new TreeSet<>();//遍历下标,存入下标for (int i =0;i<array.size();i++){int x = array.get(i);if (i>=array.size()-3){dpSet.add(x);}else if (i%3==0){lqxSet.add(x);}else if (i%3==1){lySet.add(x);}else if (i%3==2){fqySet.add(x);}}lookPoker("林青霞",lqxSet,hm);lookPoker("柳岩",lySet,hm);lookPoker("风清扬",fqySet,hm);lookPoker("底牌",dpSet,hm);}//看牌public static void lookPoker(String name,TreeSet<Integer> ts,HashMap<Integer,String> hm){System.out.println(name + "的牌是");for (Integer key :ts){String poker = hm.get(key);System.out.print(poker+" ");}System.out.println();}
}

补充内容重点:

​​​​​​​ 

 

 

 

 

 

 

 

更多推荐

集合的进阶

本文发布于:2024-02-14 10:55:02,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1763320.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:进阶

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!