我的java学习第八天

Day8

1.Collection

定义

集合:集合是java中提供的一种容器,可以用来存储多个数据。

与数组区别

数组的长度是固定的。集合的长度是可变的。

数组中存储的是同一类型的元素,可以存储基本数据类型值。集合存储的都是对象。而且对象的类型可以不 一致。在开发中一般当对象多的时候,使用集合进行存储。

image-20210822135649997

常用功能

public boolean add(E e) : 把给定的对象添加到当前集合中 。

public void clear() :清空集合中所有的元素。

public boolean remove(E e) : 把给定的对象在当前集合中删除。

public boolean contains(E e) : 判断当前集合中是否包含给定的对象。

public boolean isEmpty() : 判断当前集合是否为空。

public int size() : 返回集合中元素的个数。

public Object[] toArray() : 把集合中的元素,存储到数组中。

import java.util.ArrayList;
import java.util.Collection;

public class Demo1Collection {
public static void main(String[] args) {
// 创建集合对象
// 使用多态形式
Collection<String> coll = new ArrayList<String>();
// 使用方法
// 添加功能 boolean add(String s)
coll.add("小李广");
coll.add("扫地僧");
coll.add("石破天");
System.out.println(coll);
       
// boolean contains(E e) 判断o是否在集合中存在
System.out.println("判断 扫地僧 是否在集合中"+coll.contains("扫地僧"));
       
//boolean remove(E e) 删除在集合中的o元素
       System.out.println("删除石破天:"+coll.remove("石破天"));
       System.out.println("操作之后集合中元素:"+coll);
       
       // size() 集合中有几个元素
       System.out.println("集合中有"+coll.size()+"个元素");
       
       // Object[] toArray()转换成一个Object数组
       Object[] objects = coll.toArray();
       
       // 遍历数组
       for (int i = 0; i < objects.length; i++) {
      System.out.println(objects[i]);
      }
       
       // void clear() 清空集合
       coll.clear();
       System.out.println("集合中内容为:"+coll);
       
       // boolean isEmpty() 判断是否为空
       System.out.println(coll.isEmpty());
·}
}

2.Iterator接口

定义

java.util.Iterator 。 Iterator 接口也是Java集合中的一员,但它与 Collection 、 Map 接口有所不同, Collection 接口与 Map 接口主要用于存储元素,而 Iterator 主要用于迭代访问(即遍历) Collection 中的元 素,因此 Iterator 对象也被称为迭代器。

方法

public E next() :返回迭代的下一个元素。

public boolean hasNext() :如果仍有元素可以迭代,则返回 true。

public class IteratorDemo {
    public static void main(String[] args) {
       // 使用多态方式 创建对象
       Collection<String> coll = new ArrayList<String>();
       // 添加元素到集合
       coll.add("串串星人");
       coll.add("吐槽星人");
       coll.add("汪星人");
       //遍历
       //使用迭代器 遍历 每个集合对象都有自己的迭代器
       Iterator<String> it = coll.iterator();
       // 泛型指的是 迭代出 元素的数据类型
       while(it.hasNext()){ //判断是否有迭代元素
      String s = it.next();//获取迭代出的元素
      System.out.println(s);
      }
  }
}
原理

3.泛型

定义

格式:修饰符 class 类名<代表泛型的变量> { }

泛型:可以在类或方法中预支地使用未知的类型。

使用
public class MyGenericClass<MVP> {
//没有MVP类型,在这里代表 未知的一种数据类型 未来传递什么就是什么类型
private MVP mvp;
public void setMVP(MVP mvp) {
this.mvp = mvp;
}

public MVP getMVP() {
return mvp;
}
}

//?代表可以接收任意类型

4.List

定义

java.util.List 接口继承自 Collection 接口,是单列集合的一个重要分支,习惯性地会将实现了 List 接口的对 象称为List集合。在List集合中允许出现重复的元素,所有的元素是以一种线性方式进行存储的,在程序中可以通过 索引来访问集合中的指定元素。另外,List集合还有一个特点就是元素有序,即元素的存入顺序和取出顺序一致。

特点
  1. 它是一个元素存取有序的集合。例如,存元素的顺序是11、22、33。那么集合中,元素的存储就是按照11、 22、33的顺序完成的)。

  2. 它是一个带有索引的集合,通过索引就可以精确的操作集合中的元素(与数组的索引是一个道理)。

  3. 集合中可以有重复的元素,通过元素的equals方法,来比较是否为重复的元素。

常用方法

public void add(int index, E element) : 将指定的元素,添加到该集合中的指定位置上。

public E get(int index) :返回集合中指定位置的元素。

public E remove(int index) : 移除列表中指定位置的元素, 返回的是被移除的元素。

public E set(int index, E element) :用指定元素替换集合中指定位置的元素,返回值的更新前的元素。

public class ListDemo {
   public static void main(String[] args) {
       // 创建List集合对象
       List<String> list = new ArrayList<String>();
       
       // 往 尾部添加 指定元素
       list.add("图图");
       list.add("小美");
       list.add("不高兴");
       System.out.println(list);
       
       // add(int index,String s) 往指定位置添加
       list.add(1,"没头脑");
       System.out.println(list);
       
       // String remove(int index) 删除指定位置元素 返回被删除元素
       // 删除索引位置为2的元素
       System.out.println("删除索引位置为2的元素");
       System.out.println(list.remove(2));
       System.out.println(list);
       
       // String set(int index,String s)
       // 在指定位置 进行 元素替代(改)
       // 修改指定位置元素
       list.set(0, "三毛");
       System.out.println(list);
       
       // String get(int index) 获取指定位置元素
       // 跟size() 方法一起用 来 遍历的
       for(int i = 0;i<list.size();i++){
      System.out.println(list.get(i));
      }
       
       //还可以使用增强for
       for (String string : list) {
           System.out.println(string);
      }
  }
}
子类ArrayList

java.util.ArrayList 集合数据存储的结构是数组结构。元素增删慢,查找快,由于日常开发中使用最多的功能为 查询数据、遍历数据,所以 ArrayList 是最常用的集合。 许多程序员开发时非常随意地使用ArrayList完成任何需求,并不严谨,这种用法是不提倡的。

子类LinkedList

常用方法

public void addFirst(E e) :将指定元素插入此列表的开头。

public void addLast(E e) :将指定元素添加到此列表的结尾。

public E getFirst() :返回此列表的第一个元素。

public E getLast() :返回此列表的最后一个元素。

public E removeFirst() :移除并返回此列表的第一个元素。

public E removeLast() :移除并返回此列表的最后一个元素。

public E pop() :从此列表所表示的堆栈处弹出一个元素。

public void push(E e) :将元素推入此列表所表示的堆栈。

public boolean isEmpty() :如果列表不包含元素,则返回true。

public class LinkedListDemo {
   public static void main(String[] args) {
       LinkedList<String> link = new LinkedList<String>();
       //添加元素
       link.addFirst("abc1");
       link.addFirst("abc2");
       link.addFirst("abc3");
       System.out.println(link);
       // 获取元素
       System.out.println(link.getFirst());
       System.out.println(link.getLast());
       // 删除元素
       System.out.println(link.removeFirst());
       System.out.println(link.removeLast());
       while (!link.isEmpty()) { //判断集合是否为空
      System.out.println(link.pop()); //弹出集合中的栈顶元素
      }
       System.out.println(link);
  }
}

5.Set

定义

java.util.Set 接口和 java.util.List 接口一样,同样继承自 Collection 接口,它与 Collection 接口中的方 法基本一致,并没有对 Collection 接口进行功能上的扩充,只是比 Collection 接口更加严格了。与 List 接口不 同的是, Set 接口中元素无序,并且都会以某种规则保证存入的元素不出现重复。

子类HashSet

java.util.HashSet 是 Set 接口的一个实现类,它所存储的元素是不可重复的,并且元素都是无序的(即存取顺序 不一致)。 java.util.HashSet 底层的实现其实是一个 java.util.HashMap 支持,由于我们暂时还未学习,先做了 解。

public class HashSetDemo {
   public static void main(String[] args) {
  //创建 Set集合
       HashSet<String> set = new HashSet<String>();
       //添加元素
       set.add(new String("cba"));
       set.add("abc");
       set.add("bac");
       set.add("cba");
       //遍历
       for (String name : set) {
           System.out.println(name);
      }
  }
}
HashSet子类LinkedHashSet

在HashSet下面有一个子类 java.util.LinkedHashSet ,它是链表和哈希表组合的一个数据存储结构。可以保证元素有序

public class LinkedHashSetDemo {
   public static void main(String[] args) {
       Set<String> set = new LinkedHashSet<String>();
       set.add("bbb");
       set.add("aaa");
       set.add("abc");
       set.add("bbc");
       Iterator<String> it = set.iterator();
       while (it.hasNext()) {
      System.out.println(it.next());
      }
  }
}

6.可变函数

格式

修饰符 返回值类型 方法名(参数类型... 形参名){ }

完全等价与

修饰符 返回值类型 方法名(参数类型[] 形参名){ }

只是后面这种定义,在调用时必须传递数组,而前者可以直接传递数据即可。

7.Collections

方法

public static boolean addAll(Collection c, T... elements) :往集合中添加一些元素。

public static void shuffle(List list) 打乱顺序 :打乱集合顺序。

public static void sort(List list) :将集合中元素按照默认规则排序。

public static void sort(List list,Comparator ) :将集合中元素按照指定规则排 序。

public class CollectionsDemo {
   public static void main(String[] args) {
       ArrayList<Integer> list = new ArrayList<Integer>();
       //原来写法
       //list.add(12);
       //list.add(14);
       //list.add(15);
       //list.add(1000);
       //采用工具类 完成 往集合中添加元素
       Collections.addAll(list, 5, 222, 1,2);
       System.out.println(list);
       //排序方法
       Collections.sort(list);
       System.out.println(list);
  }
}

Comparator比较器

JAVA中提供了两种比较实现的方式:

一种是比较死板的 采用 java.lang.Comparable 接口去实现,

一种是灵活的当我需要做排序的时候在去选择的 java.util.Comparator 接口完成。

 public static  void sort(List list) 

这个方法完成的排序,实际上要求了被排序的类型 需要实现Comparable接口完成比较的功能

public final class String implements java.io.Serializable, Comparable<String>, CharSequence {

如果更改排序可以用这个方法

public static <T> void sort(List<T> list,Comparator<? super T> )

这就涉及到了Cinoarator接口

方法为

public int compare(String o1, String o2) :比较其两个参数的顺序。

两个对象比较的结果有三种:大于,等于,小于。 如果要按照升序排序, 则o1 小于o2,返回(负数),相等返回0,01大于02返回(正数) 如果要按照 降序排序 则o1 小于o2,返回(正数),相等返回0,01大于02返回(负数)

public class CollectionsDemo3 {
   public static void main(String[] args) {
       ArrayList<String> list = new ArrayList<String>();
       list.add("cba");
       list.add("aba");
       list.add("sba");
       list.add("nba");
       //排序方法 按照第一个单词的降序
       Collections.sort(list, new Comparator<String>() {
           @Override
           public int compare(String o1, String o2) {
          return o2.charAt(0) o1.charAt(0);
          }
        });
        System.out.println(list);
  }
}

Comparable与Comparator的区别

Comparable:强行对实现它的每个类的对象进行整体排序。这种排序被称为类的自然排序,类的compareTo方法 被称为它的自然比较方法。只能在类中实现compareTo()一次,不能经常修改类的代码实现自己想要的排序。实现 此接口的对象列表(和数组)可以通过Collections.sort(和Arrays.sort)进行自动排序,对象可以用作有序映射中 的键或有序集合中的元素,无需指定比较器。

Comparator强行对某个对象进行整体排序。可以将Comparator 传递给sort方法(如Collections.sort或 Arrays.sort),从而允许在排序顺序上实现精确控制。还可以使用Comparator来控制某些数据结构(如有序set或 有序映射)的顺序,或者为那些没有自然顺序的对象collection提供排序。

8.Map集合

定义

Collection 中的集合,元素是孤立存在的(理解为单身),向集合中存储元素采用一个个元素的方式存储。

Map 中的集合,元素是成对存在的(理解为夫妻)。每个元素由键与值两部分组成,通过键可以找对所对应的 值。

Collection 中的集合称为单列集合, Map 中的集合称为双列集合。 需要注意的是, Map 中的集合不能包含重复的键,值可以重复;每个键只能对应一个值。

常用方法

public V put(K key, V value) : 把指定的键与指定的值添加到Map集合中。

public V remove(Object key) : 把指定的键 所对应的键值对元素 在Map集合中删除,返回被删除元素的 值。

public V get(Object key) 根据指定的键,在Map集合中获取对应的值。

public Set keySet() : 获取Map集合中所有的键,存储到Set集合中。

public Set> entrySet() : 获取到Map集合中所有的键值对对象的集合(Set集合)

public class MapDemo {
   public static void main(String[] args) {
       //创建 map对象
       HashMap<String, String> map = new HashMap<String, String>();
       //添加元素到集合
       map.put("黄晓明", "杨颖");
       map.put("文章", "马伊琍");
       map.put("邓超", "孙俪");
       System.out.println(map);
       //String remove(String key)
       System.out.println(map.remove("邓超"));
       System.out.println(map);
       // 想要查看 黄晓明的媳妇 是谁
       System.out.println(map.get("黄晓明"));
       System.out.println(map.get("邓超"));
  }
}

Entry键值对对象

Map 中存放的是两种对象,一种称为key(键),一种称为value(值),它们在在 Map 中是一一对应关 系,这一对对象又称做 Map 中的一个 Entry(项) 。 Entry 将键值对的对应关系封装成了对象。即键值对对象

public K getKey() :获取Entry对象中的键。

public V getValue() :获取Entry对象中的值。

public Set> entrySet() : 获取到Map集合中所有的键值对对象的集合(Set集合)。

遍历键找值的方式

ublic class MapDemo01 {
   public static void main(String[] args) {
       //创建Map集合对象
       HashMap<String, String> map = new HashMap<String,String>();
       //添加元素到集合
       map.put("胡歌", "霍建华");
       map.put("郭德纲", "于谦");
       map.put("薛之谦", "大张伟");
       //获取所有的键 获取键集
       Set<String> keys = map.keySet();
       // 遍历键集 得到 每一个键
       for (String key : keys) {
           //key 就是键
           //获取对应值
           String value = map.get(key);
           System.out.println(key+"的CP是:"+value);
      }
  }
}

遍历键值对的方式

public class MapDemo02 {
   public static void main(String[] args) {
       // 创建Map集合对象
       HashMap<String, String> map = new HashMap<String,String>();
       // 添加元素到集合
       map.put("胡歌", "霍建华");
       map.put("郭德纲", "于谦");
       map.put("薛之谦", "大张伟");
       // 获取 所有的 entry对象 entrySet
       Set<Entry<String,String>> entrySet = map.entrySet();
       // 遍历得到每一个entry对象
       for (Entry<String, String> entry : entrySet) {
           // 解析
           String key = entry.getKey();
           String value = entry.getValue();
           System.out.println(key+"的CP是:"+value);
      }
}
}

子类HashMap

学生类
public 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;
  }
   
   @Override
   public boolean equals(Object o) {
   if (this == o)
  return true;
   if (o == null || getClass() != o.getClass())
       return false;
       Student student = (Student) o;
       return age == student.age && Objects.equals(name, student.name);
  }
   
   @Override
   public int hashCode() {
  return Objects.hash(name, age);
  }
}
测试类
public class HashMapTest {
   public static void main(String[] args) {
       //1,创建Hashmap集合对象。
       Map<Student,String>map = new HashMap<Student,String>();
       //2,添加元素。
       map.put(newStudent("lisi",28), "上海");
       map.put(newStudent("wangwu",22), "北京");
       map.put(newStudent("zhaoliu",24), "成都");
       map.put(newStudent("zhouqi",25), "广州");
       map.put(newStudent("wangwu",22), "南京");
       //3,取出元素。键找值方式
       Set<Student>keySet = map.keySet();
       for(Student key: keySet){
           Stringvalue = map.get(key);
           System.out.println(key.toString()+"....."+value);
      }
  }
}

HashMap子类LinkedHashMap

保证元素唯一并有序

public class LinkedHashMapDemo {
   public static void main(String[] args) {
       LinkedHashMap<String, String> map = new LinkedHashMap<String, String>();
       map.put("邓超", "孙俪");
       map.put("李晨", "范冰冰");
       map.put("刘德华", "朱丽倩");
       Set<Entry<String, String>> entrySet = map.entrySet();
       for (Entry<String, String> entry : entrySet) {
      System.out.println(entry.getKey() + " " + entry.getValue());
      }
  }
}
posted @ 2021-08-24 10:26  恶龙咆哮~~  阅读(69)  评论(0)    收藏  举报