集合(2)

HashSet

package Gather;

import java.util.HashSet;
import java.util.Iterator;

public class HashSetDemo {
    public static void main(String[] args) {
        /*
        *   Set 接口 不能存储重复元素
        *   HashSet
        *       元素是无序的(既不是添加顺序,也不是元素自然排序)
        *       向Hashset中添加元素时,是如何判断元素是否重复的
        *
        *       添加元素是,可以调用equals();判断,但是效率很低,一个字符一个字符进行比对
        *       底层使用的是hashCode();和equals();方法
        *       nvjcxzlkxckvh 用内容计算一个hash值(整数),用hash值比较速度快
        *       但是hash值是不安全的,有可能内容不同计算的hash值相同
        *       当hash值相同时,调用equals()方法判断内容给是否相等
        *       这样子既提高了效率也提高了安全性
        *
        * */

        /*HashSet<String> hashSet=new HashSet<>();
        hashSet.add("a");
        hashSet.add("x");
        hashSet.add("x");
        hashSet.add("d");
        hashSet.add("i");
        hashSet.add("n");
        hashSet.add("l");
        System.out.println(hashSet);*/

        /*HashSet<Character> set=new HashSet<>();
        for (char i = 'z'; i >= 'a'; i--) {
            set.add(i);
        }
        System.out.println(set);//并不按照顺序输入*/

        HashSet<String> set=new HashSet<>();
        set.add("a");
        set.add("x");
        set.add("X");
        set.add("x");
        set.add("d");
        set.add("i");
        set.add("n");
        set.add("l");
        System.out.println(set);

        /*
        *   添加时,判断会调用类中的hashCode(),计算hash值
        * */

        Student stu1=new Student(101,"张三");
        Student stu2=new Student(102,"李四");
        Student stu3=new Student(103,"王五");
        Student stu4=new Student(101,"张三");

        HashSet<Student> stuList=new HashSet<>();
        stuList.add(stu1);
        stuList.add(stu2);
        stuList.add(stu3);
        stuList.add(stu4);
        System.out.println(stuList.toString());

        for(Student s:stuList){
            System.out.println(s);
        }

        System.out.println("*********************************************************************************");

        Iterator<Student> stu= stuList.iterator();
        while(stu.hasNext()){
            Student e=stu.next();
            System.out.println(e);
        }
    }
}

TreeSet

package Gather;

import java.util.Iterator;
import java.util.TreeSet;

public class TreeSetDemo {
    /*
    *   Set 不能存储重复元素
    *       TreeSet
    *           底层是树结构
    *           添加进来的元素是可以排序的(有序的不是添加的顺序,是元素的自然排序)
    *       树形结构是个啥马达?
    *           root-根
    * */

    public static void main(String[] args) {
        TreeSet<Integer> treeSet=new TreeSet<>();
        treeSet.add(3);
        //clear  size  contains  isEmpty  remove  pollLast
        //

        TreeSet<Student> StuTree=new TreeSet<>();
        Student stu1=new Student(101,"张三");
        Student stu2=new Student(102,"李四");
        Student stu3=new Student(103,"王五");
        Student stu4=new Student(101,"张三");
        StuTree.add(stu1);
        StuTree.add(stu2);
        StuTree.add(stu3);
        StuTree.add(stu4);
        System.out.println(StuTree);

        for(Student s:StuTree){
            System.out.println(s);
        }

        System.out.println("*************************************************************");

        Iterator<Student> stu=StuTree.iterator();
        while(stu.hasNext()){
            Student e=stu.next();
            System.out.println(e);
        }
    }
}

关于Collection的补充:

package Gather;

import java.util.ArrayList;
import java.util.Collections;

public class Collection {
    public static void main(String[] args) {
        ArrayList arrayList=new ArrayList();
        arrayList.add("a");
        arrayList.add("a");
        arrayList.add("b");
        arrayList.add("a");
        System.out.println(arrayList);
        arrayList.remove("b");
        System.out.println(arrayList);

        //Collections添加功能
        Collections.addAll(arrayList,"kjdsahgjdlakhgljkadshglkjsdhjkgdsahgasldkgholadshlkdsahj");
        /*
            ...语法,当自己也不知道要添加多少个变量的时候可以用,他就代表很多个的意思
         */
        System.out.println(arrayList);

        //查找元素,并返回下标
        System.out.println(Collections.binarySearch(arrayList,"kjdsahgjdlakhgljkadshglkjsdhjkgdsahgasldkgholadshlkdsahj"));

        Collections.addAll(arrayList,"b","f","g","c");
        //排序
        Collections.sort(arrayList);
        System.out.println(arrayList);
        //交换元素
        Collections.swap(arrayList,4,1);
        System.out.println(arrayList);
        //拷贝数组
        ArrayList<String> arrayList1=new ArrayList<>();
        Collections.addAll(arrayList1,"e","q");
        Collections.copy(arrayList,arrayList1);
        System.out.println(arrayList);

        System.out.println(Collections.max(arrayList));
        System.out.println(Collections.min(arrayList1));
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值