简单了解结集合

集合

集合概念: 对象的容器,定义了多个对象进行操作的常用方法。可实现数组的功能。

集合和数组的区别:

(1)数组长度固定,集合长度不固定。

(2)数组可以存储基本类型和引用类型,集合只能存储引用类型

位置: java.util.*;

Collection体系集合

Collection父接口

特点:代表任意类型的对象,无序、无下标。不能重复

方法:

boolean add(Object obj)添加一个对象
boolean addALL(Collection c)将一个集合中的所有对象添加到此集合中
void clear()清空此集合中的所有对象
boolean contains(Object o)检查此集合中是否包含o对象
boolean equals(Object o)比较此集合是否包含o对象
boolean isEmpty()判断此集合是否为空
boolean remove(Object o)在此集合中移除o对象
int size()返回此集合中的元素个数
Object[] toArray()将此集合转换成数组
Collection使用
//创建集合
        Collection collection = new ArrayList();
//         (1)添加元素
        collection.add("苹果");
        collection.add("西瓜");
        collection.add("榴莲");
        System.out.println("元素个数:" + collection.size());
        System.out.println(collection);
//         (2)删除元素
        //collection.remove("榴莲");
//        collection.clear();
//        System.out.println("元素个数:"+collection.size());
//          (3)遍历元素[重点]
        //3.1使用增强for  (无下标不能使用for)
//        System.out.println("-----------3.1 增强for--------------");
//        for (Object object : collection){
//            System.out.println(object);
        //       }
        //3.2 使用迭代器(迭代器是专门用来遍历集合的一种方式)
        //hashNext();有没有下一个元素
        //next();获取下一个元素
        //remove(); 删除当前元素
        System.out.println("---------------3.2使用增强for---------------------");
        Iterator it = collection.iterator();
        while(it.hasNext()){
            String s = (String)it.next();
            System.out.println(s);
          //  collection.remove(s); // 报错  ConcurrentModificationException
            it.remove();
        }
        System.out.println("元素个数" + collection.size());
//          (4)判断
        System.out.println(collection.contains("西瓜"));   //判断是否存在
        System.out.println(collection.isEmpty());   //判断集合是否为空
public static void main(String[] args) {
        //新建Collection对象
        Collection collection= new ArrayList();
        Student s1 = new Student("张三",20);
        Student s2 = new Student("李四",18);
        Student s3 = new Student("王二",22);


        //1. 添加数据
        collection.add(s1);
        collection.add(s2);
        collection.add(s3);
        collection.add(s3);//可重复添加

        System.out.println("元素个数"+collection.size());
        System.out.println(collection.toString());

        //2.删除
       // collection.remove(s1);
       // collection.remove(new Student("王二",22));//现new的删不了
       // collection.clear();

        //System.out.println("删除之后"+collection.size());

        //3.1增强for
//        for (Object objedt : collection){
//            System.out.println(objedt.toString());
//        }
        //3.2迭代器
//        Iterator it = collection.iterator();
//        while (it.hasNext()){
//            System.out.println(it.hasNext());
//        }

        //4.判断


        System.out.println(collection.contains(s1));
        System.out.println(collection.isEmpty());
List集合

特点:有序、有下标、元素可以重复

方法

void add(int index,Object o )在index位置插入对象o
boolean addAll(int index,Collenction c )将一个集合中的元素添加到此集合中的index位置
//先创建集合对象
        List list = new ArrayList<>();

        //1.添加元素
        list.add("苹果");
        list.add("华为");
        list.add(0,"小米");
        System.out.println("元素个数为"+list.size());
        System.out.println(list.toString());

        //2.删除元素
        //list.remove("苹果");
        list.remove(0);
        //list.clear();
        System.out.println("元素个数为"+list.size());

        //3.遍历元素
        //3.1强化for
//        for (Object object : list){
//            System.out.println(object);
//        }


//        }
//        System.out.println("删除后的元素个数为"+list.size());

        //3.2for循环
//        for (int i = 0;i < list.size();i++){
//            System.out.println(list.get(i));
//        }

        //3.3迭代器
//        Iterator it = list.iterator();
//        while (it.hasNext()){
//            System.out.println(it.next());
//        }

        //3.4列表迭代器  和Iterator区别  ListIterator可以向前向后遍历,还可以添加,删除,修改元素
        ListIterator lit = list.listIterator();
        //从前往后
        while (lit.hasNext()){
            //nextIndex() 获取下标
            System.out.println(lit.nextIndex() + ":" + lit.next());
        }
        //从后往前
        while (lit.hasPrevious()){
            System.out.println(lit.previousIndex() + ":" + lit.previous());
        }



        //4.判断
        System.out.println(list.isEmpty());
        System.out.println(list.contains("小米"));

        //5.获取位置
        System.out.println(list.indexOf("华为"));
List实现类
  • ArrayList【重点】
    • 数组结构实现,查询快、增删慢
    • JDK1.2版本,运行效率快、线程不安全
  • Vector:
    • 数组结构实现,查询快、增删慢
    • JDK1.0版本,运行效率慢,线程安全
  • LinkedList
    • 链表结构实现,增删快,查询慢

ArrayList使用

//创建集合
        ArrayList arrayList = new ArrayList();

        //1.添加元素
        Student s1 = new Student("刘德华",20);
        Student s2 = new Student("郭富城",19);
        Student s3 = new Student("吴彦祖",18);
        arrayList.add(s1);
        arrayList.add(s2);
        arrayList.add(s3);

        System.out.println("元素个数"+arrayList.size());
        System.out.println(arrayList.toString());

        //2.删除元素
        arrayList.remove(new Student("刘德华",20));
        //若果想直接用对应Student对象删除 remove会调用equals方法   所以只需要重写equals方法

        System.out.println("元素个数"+arrayList.size());
        System.out.println(arrayList.toString());


        //3.遍历元素【重点】
        //3.1使用迭代器
//        System.out.println("------------3.1使用迭代器--------------");
//        Iterator it = arrayList.iterator();
//        while (it.hasNext()){
//            Student s =(Student) it.next();
//            System.out.println(s.toString());
//        }

        //3.2列表迭代器
        ListIterator lit = arrayList.listIterator();
        System.out.println("------------3.2列表迭代器----------------");
        while (lit.hasNext()){
            Student s = (Student) lit.next();
            System.out.println(s.toString());
        }

        System.out.println("---------------3.2使用列表迭代器逆序------------------");
        while(lit.hasPrevious()){
            Student s = (Student) lit.previous();
            System.out.println(s.toString());
        }


        //4.判断
        System.out.println(arrayList.contains(new Student("梁朝伟",18)));
        System.out.println(arrayList.isEmpty());



        //5.查找
        System.out.println(arrayList.indexOf(new Student("郭富城",19)));

源码分析:

arrayList源码
DEFAULT_CAPACITY = 10;             //默认容量
          //注意:如果没有向集合中添加任何元素时,容量是0
elementData;                       //存放元素的数组
size;                              //实际元素个数
add()                              //添加元素

Vector:使用

//创建集合
 Vector vector = new Vector<>();

 //1.添加元素
 vector.add("草莓");
 vector.add("芒果");
 vector.add("西瓜");
 System.out.println("元素个数"+vector.size());
 System.out.println(vector.toString());

 //2.删除
 //vector.remove("草莓");
 //vector.remove(0);
 vector.clear();
 System.out.println("元素个数"+vector.size());
 System.out.println(vector.toString());

 //3.遍历
 //使用枚举器
 Enumeration en = vector.elements();
 while (en.hasMoreElements()){
     String o = (String)en.nextElement();
     System.out.println(o);
 }

 //4判断
 System.out.println(vector.contains("西瓜"));
 System.out.println(vector.isEmpty());
 //5.Svector其他方法
 //firstElement,lastElement,ElementAt();

Linkedlist使用

//创建集合
        LinkedList linkedList = new LinkedList();

        //1.添加元素
        Student s1 = new Student("刘德华",20);
        Student s2 = new Student("郭富城",22);
        Student s3 = new Student("梁朝伟",18);
        linkedList.add(s1);
        linkedList.add(s2);
        linkedList.add(s3);

        System.out.println("元素个数"+linkedList.size());
        System.out.println(linkedList.toString());

        //2.删除
//        linkedList.remove(new Student("刘德华",20));
//        System.out.println("元素个数"+linkedList.size());
//        linkedList.clear();

        //3.1for遍历
      //  System.out.println("-----------for-------------");
       // for (int i = 0;i < linkedList.size();i++){
      //      System.out.println(linkedList.get(i));
     //   }

        //3.2增强for
//        for (Object object : linkedList){
//            Student s = (Student) object;
//            System.out.println(s.toString());
//        }

        //3.3迭代器
//        Iterator it = linkedList.iterator();
//        while (it.hasNext()){
//            Student s = (Student)it.next();
//            System.out.println(s);
//        }

        //3.4 使用列表迭代器
        System.out.println("-----------使用列表迭代器------------");
        ListIterator lit = linkedList.listIterator();
        while (lit.hasNext()){
            Student s = (Student)lit.next();
            System.out.println(s.toString());
        }

        while (lit.hasPrevious()){
            Student s = (Student) lit.previous();
            System.out.println(s.toString());
        }

        //4.判断
        System.out.println(linkedList.contains(s1));
        System.out.println(linkedList.isEmpty());

        //5.获取
        System.out.println(linkedList.indexOf(s2));

ArrayList与LinkedList的区别

在这里插入图片描述

泛型
  • Java泛型是JDK1.5中引入的一个新特性,其本质是参数化类型,把类型作为参数传递
  • 常见的形式有泛型类、泛型接口、泛型方法
  • 语法
    • <T,…>T成为类型占位符,表示一种引用类型
  • 好处 :
    • (1)提高代码重要性
    • (2)防止类型转换异常,提高代码的安全性

泛型类的定义和使用

定义:

//泛型类
//语法 类名<T> T类型占位符,表示一种引用类型,如果编写多个使用逗号隔开

//1.创建变量
T t;

//2.泛型作为方法的参数
//不能在方法中new一个泛型对象  因为引用类型不确定
public void show(T t){
    System.out.println(t);
}

//3.泛型作为方法的返回值
public T getT(){
    return t;
}

使用:

//使用泛型类创建对象
//注意:1.泛型只能使用引用类型  2.不同泛型类型对象之间不能相互赋值
MyGeneric<String> myGeneric = new MyGeneric<String>();
myGeneric.t = "hello";
myGeneric.show("大家好");
String string = myGeneric.getT();


MyGeneric<Integer> myGeneric2 = new MyGeneric<Integer>();
myGeneric2.t = 100;
myGeneric2.show(200);
Integer integer = myGeneric2.getT();

泛型接口的定义和使用

定义:

public interface MyInterface<T> {

    //泛型接口
    //语法,接口名<T>  注意:不能泛型静态常量

    String name = "张三";
    T server(T t);
}

使用:

//实现接口时确定类型
public class MyIntegerfaceImpl implements MyInterface<String>{

    @Override
    public String server(String t) {
        System.out.println(t);
        return t;
    }
}

//实现接口时不确定类型
public class MyIntegerfaceImpl2<T> implements MyInterface<T>{

    @Override
    public T server(T t) {
        System.out.println(t);
        return t;
    }
}
//实现接口时确定类型
MyIntegerfaceImpl impl = new MyIntegerfaceImpl();
impl.server("xxxxxxxxxxxx");

//实现接口时不确定类型
MyIntegerfaceImpl2<Integer> impl2 = new MyIntegerfaceImpl2<Integer>();
impl2.server(1000);

泛型方法的定义和使用

定义:

public class MyGenericMethod {

    //泛型方法
    //语法  <T> 返回值类型

    //泛型方法
    public <T> void show(T t){
        System.out.println("泛型方法"+t);

    }

    public <T> T show2(T t){
        System.out.println("泛型方法"+t);
        return t;
    }
}

使用:

//泛型方法  类型不需要传递 根据给的值自动变化
MyGenericMethod myGenericMethod = new MyGenericMethod();
myGenericMethod.show("中国加油");
myGenericMethod.show(200);
myGenericMethod.show(3.14);
泛型集合

概念: 参数化类型、类型安全的集合,强制集合元素的类型必须一致。

特点:

  • 编译时即可检查,而非运行时抛出异常
  • 访问时,不必类型转换(拆箱)。
  • 不同泛型之间引用不能相互赋值,泛型不存在多态

泛型集合的使用:

public static void main(String[] args) {

        ArrayList arrayList = new ArrayList();
        arrayList.add("xxx");
        arrayList.add("yyy");
        arrayList.add(10);
        arrayList.add(20);

        //Integer不能转换String 类型转换异常
        for (Object object : arrayList){
            String str = (String) object;
            System.out.println(str);


            //不同泛型不能相互指挥
        ArrayList<String> arrayList2 = new ArrayList<String>();
        arrayList2.add("xxx");
        arrayList2.add("yyy");


        for (String string: arrayList2){
            String str2 = (String)string;
            System.out.println(str2);

        }

        ArrayList<Student> arrayList3 = new ArrayList<Student>();
            Student s1 = new Student("刘德华",20);
            Student s2 = new Student("郭富城",22);
            Student s3 = new Student("梁朝伟",18);
            arrayList3.add(s1);
            arrayList3.add(s2);
            arrayList3.add(s3);

            Iterator<Student> it = arrayList3.iterator();
            while (it.hasNext()){
                Student s= it.next();
                System.out.println(s.toString());
            }

    }
}
set集合

特点: 无序、无下标、元素不可重复。

方法: 全部继承自Collection中的方法

Set实现类:

HashSet[重点]

  • 基于HashCode计算元素存放位置,实现元素不重复
  • 当存入元素的哈希码相同时,会调用equals进行确认,如结果为true,则拒绝而后入

TreeSet:

  • 基于排列顺序实现元素不重复
  • 实现了SortedSet接口,对集合元素自动排序
  • 元素对象的类型必须实现Comparable接口,制定排序规则
  • 通过CompareTo方法确定是否为重复元素
//创建集合
        Set<String> set = new HashSet();
        //1.添加数据
        set.add("小米");
        set.add("苹果");
        set.add("华为");
System.out.println("数据个数:"+set.size());
    System.out.println(set.toString());

    //2.删除数据
//        set.remove("小米");
//        System.out.println(set.toString());   
//3.遍历【重点】
    //3.1使用增强for
    System.out.println("-------------增强for---------------");
    for (String string : set){
        System.out.println(string);
    }

    //3.2 使用迭代器
    System.out.println("-----------使用迭代器---------------");
    Iterator<String> it = set.iterator();
    while (it.hasNext()){
        System.out.println(it.next());
    }

    //4.判断
    System.out.println(set.contains("华为"));
    System.out.println(set.isEmpty());//创建集合
        Set<String> set = new HashSet();
        //1.添加数据
        set.add("小米");
        set.add("苹果");
        set.add("华为");

        System.out.println("数据个数:"+set.size());
        System.out.println(set.toString());

        //2.删除数据
//        set.remove("小米");
//        System.out.println(set.toString());

        //3.遍历【重点】
        //3.1使用增强for
        System.out.println("-------------增强for---------------");
        for (String string : set){
            System.out.println(string);
        }

        //3.2 使用迭代器
        System.out.println("-----------使用迭代器---------------");
        Iterator<String> it = set.iterator();
        while (it.hasNext()){
            System.out.println(it.next());
        }

        //4.判断
        System.out.println(set.contains("华为"));
        System.out.println(set.isEmpty());

HashSet 使用

//新建集合
        HashSet<String> hashSet = new HashSet<String>();

        //1.添加元素
        hashSet.add("刘德华");
        hashSet.add("梁朝伟");
        hashSet.add("林志玲");
        hashSet.add("周润发");
        hashSet.add("周润发");
        System.out.println("元素个数"+hashSet.size());
        System.out.println(hashSet.toString());

        //2.删除元素
        hashSet.remove("刘德华");
       // hashSet.clear();
        System.out.println("删除之后"+hashSet.size());

         //3遍历操作
        //3.1增强for
        System.out.println("------------3.1增强for--------------");
        for (String string : hashSet){
            System.out.println(string);
        }

        //3.2使用迭代器
        System.out.println("--------------3.2迭代器--------------");
        Iterator it = hashSet.iterator();
        while (it.hasNext()){
            System.out.println(it.next());
        }

        //4.判断
        System.out.println(hashSet.contains("刘德华"));
        System.out.println(hashSet.isEmpty());
//存储结构 : 哈希表(数组+链表+红黑树)
//存储过程
//(1)根据hashcode计算保存的位置,如果此位置为空,则直接保存,如果不为空执行第二部
//(2)再执行equals方法,如果equals方法为true,则认为是重复,否则,形成链表
public class Demo11 {
    public static void main(String[] args) {

        //创建集合
        HashSet<Person> persons = new HashSet<>();

        //1.添加数据
        Person p1 = new Person("刘德华",20);
        Person p2 = new Person("林志玲",22);
        Person p3 = new Person("梁朝伟",25);

        persons.add(p1);
        persons.add(p2);
        persons.add(p3);

        //重写hashcode后 hashcode位置相同 equlas判断为false 所以两个元素数据相同但是都添加进去了‘
        //重写equlas也是一样,但是两种方式都重写将不会再加入
        persons.add(new Person("梁朝伟",25));

        System.out.println("元素个数"+persons.size());
        System.out.println(persons.toString());

        //2.删除操作
//        persons.remove(p1);
//        persons.remove(new Person("刘德华",20));
//        System.out.println("删除之后"+persons.toString());

        //3.遍历【重点】
        //3.1增强for
        for (Person person: persons){
            System.out.println(person);
        }
        System.out.println("-----------------------");

        //3.2迭代器
        Iterator it = persons.iterator();
        while (it.hasNext()){
            System.out.println(it.next());
        }

        //4.判断
        System.out.println(persons.contains(p1));
        System.out.println(persons.isEmpty());


    }

重写hashCode和equals方法

//快捷方式alt+insert 或者鼠标右键Generate
    @Override
    public int hashCode() {
        int n1 = this.name.hashCode();
        int n2 = this.age;
        return n1+n2;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj){
            return true;
        }
        if (obj == null){
            return false;
        }
        if (obj instanceof Person){
            Person p = (Person) obj;
            if (this.name.equals(p.getName())&&this.age == p.getAge()){
                return true;
            }
        }
        return false;
    }

TreeSet使用

//创建集合
TreeSet<String> treeSet = new TreeSet<>();

//1.添加元素
treeSet.add("xyz");
treeSet.add("abc");
treeSet.add("hello");
treeSet.add("xyz");

System.out.println("元素个数"+treeSet.size());
System.out.println(treeSet.toString());

//2.删除
treeSet.remove("xyz");
System.out.println("元素个数"+treeSet.size());

//3.遍历
//3.1增强for
for (String string : treeSet){
    System.out.println(string);
}
System.out.println("---------------------");

//3.2迭代器
Iterator it = treeSet.iterator();
while (it.hasNext()){
    System.out.println(it.next());
}

//4.判断
System.out.println(treeSet.contains("abc"));
System.out.println(treeSet.isEmpty());
//存储结构:红黑树

        //创建集合
        TreeSet<Person> persons = new TreeSet<>();

        //1.添加元素

        Person p1 = new Person("刘德华",20);
        Person p2 = new Person("林志玲",22);
        Person p3 = new Person("梁朝伟",25);

        persons.add(p1);
        persons.add(p2);
        persons.add(p3);

        //报错Person cannot be cast to class java.lang.Comparable  没有指定比较对象
        //必须要实现Compareble接口,compareTo()方法返回值为0,认为是重复元素;
        System.out.println("元素个数"+persons.size());
        System.out.println(persons.toString());

        //2.删除操作
//        persons.remove(p1);
//        persons.remove(new Person("刘德华",20));
//        System.out.println("删除之后"+persons.toString());

        //3.遍历【重点】
        //3.1增强for
        for (Person person: persons){
            System.out.println(person);
        }
        System.out.println("-----------------------");

        //3.2迭代器
        Iterator it = persons.iterator();
        while (it.hasNext()){
            System.out.println(it.next());
        }

        //4.判断
        System.out.println(persons.contains(p1));
        System.out.println(persons.isEmpty());

Comparable接口实现(可比较的)

//Comparable是一个泛型接口
public class Person implements Comparable<Person>{

//先按姓名作比较再按年龄作比较
public int compareTo(Person o){
    int n1 = this.getName().compareTo(o.getName());
    int n2 = this.getAge()-o.getAge();
    return n1==0?n2:n1;
}

Comparator接口(TreeSet集合的使用)实现自己定制比较(比较器)

public static void main(String[] args) {

    //创建集合
    TreeSet<Person> persons = new TreeSet<>(new Comparator<Person>() {
        @Override
        public int compare(Person o1, Person o2) {
            int n1 = o1.getAge()-o2.getAge();
            int n2 = o1.getName().compareTo(o2.getName());
            return n1==0?n2:n1;
        }
    });

    Person p1 = new Person("xyz",20);
    Person p2 = new Person("hello",22);
    Person p3 = new Person("zhangsan",25);
    Person p4 = new Person("lisi",25);

    persons.add(p1);
    persons.add(p2);
    persons.add(p3);
    persons.add(p4);

    System.out.println(persons.toString());
}

案例 : 要求,使用TreeSet集合实现按字符串长度进行排序

//创建集合并制定规则
TreeSet<String> treeSet =  new TreeSet<>(new Comparator<String>() {
    @Override
    public int compare(String o1, String o2) {
        int n1 = o1.length()-o2.length();
        int n2 = o1.compareTo(o2);
        return n1==0?n2:n1;
    }
});

//添加数据
treeSet.add("helloworld");
treeSet.add("pingguo");
treeSet.add("lisi");
treeSet.add("zhangsan");
treeSet.add("beijing");
treeSet.add("cat");
treeSet.add("nanjing");
treeSet.add("xian");

System.out.println(treeSet.toString());
Map体系集合

Map父接口

特点: 1. 用于存储任意键值对(Key-Vaule)

​ 2. 键:无序、无下标、不允许重复(唯一)

​ 3. 值:无序、无下标、允许重复

方法:

V put(K key, V value)将对象存入到集合中,取关键值。key重复则覆盖原值
Object get(Object key)根据对应键获取对应的值
Set返回所有的key
Collection values()返回包含所有值的Collection集合
Set<Map.Entry<k,v>>键值匹配的Set集合

Map方法的使用

//创建Map集合
        Map<String,String> map = new HashMap<>();

        //1.添加元素
        map.put("cn","中国");
        map.put("cn","zhongguo");//同一个键  后创建的值会覆盖之前的值
        map.put("china","中国");
        map.put("uk","英国");
        map.put("usa","美国");

        System.out.println("元素个数"+map.size());
        System.out.println(map.toString());

        //2.删除
        map.remove("usa");

        System.out.println("删除之后"+map.size());
        System.out.println(map.toString());

        //3.遍历
        //3.1 使用keySet()遍历
        System.out.println("------------keySet()-------------");
        Set<String> keySet = map.keySet();  //获取键的Set集合
        for (String string : keySet){
            System.out.println(string);
        }

        //3.2使用entrySet()方法   Entry表示一个键值对
        System.out.println("---------------entrySet()--------------");
        //Set<Map.Entry<String,String>> entries = map.entrySet();
//        for (Map.Entry<String,String> entry : entries){
//            System.out.println(entry);
//        }
        //或者
        for (Map.Entry<String,String> entry : map.entrySet()){
            System.out.println(entry);
            //或者
            System.out.println(entry.getKey()+"----"+entry.getValue());

            //4.判断
            System.out.println(map.containsKey("cn"));
            System.out.println(map.containsValue("中国"));
        }

Map的实现类:

  • HashMap【重点】:
    JDK1.2版本,线程不安全,运行效率快;允许用null 作为key或是value
  • Hashtable 【过时,不常用】:
    JDK1.0版本,线程安全,运行效率慢;不允许null作为key或是value
  • Properties :
    Hashtable的子类,要求key和value都是String。通常用于配置文件的读取
  • TreeMap :
    实现了SortedMap接口(是Map的子接口),可以对key自动排序。

HashMap的使用:

//存储结构 : 哈希表(数组+链表+红黑树)

//创建集合
HashMap<Student, String> students = new HashMap<>();

//1.添加元素
Student s1 = new Student("孙悟空",100);
Student s2 = new Student("猪八戒",200);
Student s3 = new Student("沙和尚",300);
Student s4 = new Student("唐僧",1000);

students.put(s1,"北京");
students.put(s2,"上海");
students.put(s3,"深圳");
students.put(s4,"北京");
students.put(new Student("沙和尚",300),"深圳");//与上方沙和尚数据一样但是加进来了
//因为使用key作为判断  hashcode和equals可能判断未重复 需要重写这两个方法

System.out.println("元素个数"+students.size());
System.out.println(students.toString());

//2.删除
students.remove(s1);
System.out.println("删除后"+students.size());
System.out.println(students.toString());

//3.遍历
//3.1使用keySet()方法
for (Student key : students.keySet()){
    System.out.println(key);
}

//3.2使用entrySet()方法
for (Map.Entry<Student,String>  entry:students.entrySet()){
    System.out.println(entry.getKey()+"------"+entry.getValue());
}

//4.判断
System.out.println(students.containsKey(s2));
System.out.println(students.containsValue("北京"));
System.out.println(students.isEmpty());

TreeMap的使用:

//存储结构  红黑树
//创建集合
TreeMap<Student,String> treeMap = new TreeMap<>();

//1.添加元素
Student s1 = new Student("孙悟空",100);
Student s2 = new Student("猪八戒",200);
Student s3 = new Student("沙和尚",300);
Student s4 = new Student("唐僧",1000);

treeMap.put(s1,"北京");
treeMap.put(s2,"上海");
treeMap.put(s3,"深圳");
treeMap.put(s4,"北京");


System.out.println("元素个数"+treeMap.size());
System.out.println(treeMap.toString());

//2.删除
treeMap.remove(new Student("猪八戒",200));//可以删除  因为compareTo比较比较年龄
//年龄相同属性相同判定为同一对象

System.out.println("删除后元素个数"+treeMap.size());
System.out.println(treeMap.toString());

//3.遍历
//3.1 keySet()
System.out.println("------------keySet()------------");
for (Student key :treeMap.keySet()){
    System.out.println(key+"---"+treeMap.get(key));
}

//3.2 entrySet()
System.out.println("------------------entrySet()----------------");
for (Map.Entry<Student,String> entry:treeMap.entrySet()){
    System.out.println(entry.getKey()+"---"+entry.getValue());
}

//4.判断
System.out.println(treeMap.containsKey(s1));
System.out.println(treeMap.containsValue("北京"));
Colletions工具类

概念: 集合工具类,定义了除了存取以外的集合常用方法

方法:

public static void reverse(List<?> list)反转集合中元素的顺序
public static void shuffle(List<?> list)随即重置集合元素的顺序
public static void sort(List list)升序排序(元素类型必须实现Comparable接口)
  List<Integer> list = new ArrayList<>();
        list.add(20);
        list.add(5);
        list.add(12);
        list.add(30);
        list.add(6);

        //sort排序
        System.out.println("排序之前"+list.toString());
        Collections.sort(list);
        System.out.println("排序之后"+list.toString());

        //binarySearch   二分查找
        List<Integer> dest = new ArrayList<>();
        for (int k = 0; k < list.size();k++){
            dest.add(0);
        }
        Collections.copy(dest,list);  //两个集合大小不一样 IndexOutOfBoundsException
        System.out.println(dest.toString());


        // reverse  反转

        Collections.reverse(list);
        System.out.println("反转之后"+list);

        //shuffle  打乱
        Collections.shuffle(list);
        System.out.println("打乱之后"+list);

        //补充:list转成数组
        Integer[] arr = list.toArray(new Integer[0]);
        System.out.println(arr.length);
        System.out.println(Arrays.toString(arr));

        //数组转成集合
        String[] names = {"张三","李四","王五"};
        List<String> list2 = Arrays.asList(names);
//        list2.add("赵六");   报错
//        list2.remove(0);   报错
        System.out.println(list2);

        int[] nums = {100,200,300,400,500};
        List<int[]> list3 = Arrays.asList(nums);
        System.out.println(list3);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值