集合课后复习一

一.HashSet

关键字:集合、容器、无序、无索引

1. 方法

1.1 add()

添加该集合中没有的元素,如果该元素已存在,则覆盖已有的元素。

 HashSet<String> set=new HashSet();
        set.add("123");

1.2 clear()

清空该集合。

 set.clear();

1.3 contains()

如果集合中不包含该元素则返回true。

 System.out.println(set.contains("123"));

1.4 isEmpty()

该集合中不包含任何元素则返回true。

    System.out.println(set.isEmpty());

1.5 iterator()

返回对该集合中元素进行迭代的迭代器。

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

      while (it.hasNext()){
          System.out.println(it.next());
      }

1.6 remove()

如果该集合中含有此元素,将其移除。

 set.remove("123");

1.7 size()

返回该容器中元素的数量。

System.out.println(set.size());

2.父类方法

collection()。

2.1 remove()

移除该集合中所有也包含于指定集合中的元素。

nameset.removeAll(set);

二.TreeSet

有序

底层结构是TreeMap(树形结构),因为树要求存储的数据必须有序,因此该类集合是有序的。

1. Java二叉树(中序、递归)

public class mytree {
    private Node root;
    public void add(int x){
        Node p=new Node();
        p.date=x;

        if(root==null){
            root=p;
        }else{
            root.addNode(p);
        }
    }
    public void sort(){
        if(root==null){
            return;
        }else{
            root.zhongxun();
        }
    }
}
public class Node {
    public int date;
    public Node leftNode ;
    public Node rightNode;

    //添加节点的方法
    public void addNode(Node t){
        //添加节点的时候比较节点的date 小于就添加左边节点
        if(t.date < this.date){
            if(leftNode == null){
                leftNode=t;
            }else{
                leftNode.addNode(t);
            }

        }else{
            if(rightNode==null){
                rightNode=t;
            }else{
                rightNode.addNode(t);
            }
        }
    }

    //树形结构要求存入的数据必须有序 中序
    public void zhongxun(){
        if(leftNode!=null)
            leftNode.zhongxun();

        System.out.print(date);

        if (rightNode!=null)
            rightNode.zhongxun();
    }
}
public class test {

    public static void main(String[] args) {
        mytree m=new mytree();
        m.add(5);
        m.add(4);
        m.add(6);
        m.add(3);
        m.add(2);
        m.add(1);

        m.sort();
    }




}

 三. List

         有序序列,一般称为列表,可以存放重复元素

3.1 Vector

动态数组

方法

add(),remove(),set/get()。

如果为Integer类型,remove中默认为索引。

public class test {

    public static void main(String[] args) {

        Vector<Integer> a=new Vector<>();

        a.add(1);
        a.add(2);
        a.add(3);

        System.out.println(a);

        System.out.println(a.get(0));
        a.set(0,8);

        a.remove(1);
        a.remove(new Integer(1));

        System.out.println(a);


    }


}

3.2 ArrayList

  1. 底层结构 动态数组
  2. 线程不安全
  3. 默认初始容量为10
  4. 1.5倍扩容   >>运算,1/2
  5. 更擅长随机访问 查询速度比较快 (LinkedList)
    public void ensureCapacity(int minCapacity) {
        int minExpand = (elementData != DEFAULTCAPACITY_EMPTY_ELEMENTDATA)
            // any size if not default element table
            ? 0
            // larger than default for default empty table. It's already
            // supposed to be at default size.
            : DEFAULT_CAPACITY;

        if (minCapacity > minExpand) {
            ensureExplicitCapacity(minCapacity);
        }
    }
    private void grow(int minCapacity) {
        // overflow-conscious code
        // 旧容量 = 数组的成功度
        // 新容量 = 旧容量的1.5倍
        int oldCapacity = elementData.length;
        int newCapacity = oldCapacity + (oldCapacity >> 1);
        if (newCapacity - minCapacity < 0)
            newCapacity = minCapacity;
        if (newCapacity - MAX_ARRAY_SIZE > 0)
            newCapacity = hugeCapacity(minCapacity);
        // minCapacity is usually close to size, so this is a win:
        elementData = Arrays.copyOf(elementData, newCapacity);
    }

3.2 自然排序

3.2.1 Comparable

        接口,需重写compareTo方法

此接口强行对实现它的每个类的对象进行整体排序。此排序被称为该类的自然排序,类的 compareTo 方法被称为它的自然比较方法

是对引用类型的比较,基本类型,无法调用。

注意:自己定义的类,要重写toString()方法,不然打印会得到一串地址。**************

public class Child  implements Comparable{
       private String a;
       private int b;


    @Override
    public String toString() {
        return "Child{" +
                "a='" + a + '\'' +
                ", b=" + b +
                '}';
    }

    public Child(String a, int b){
        this.a=a;
        this.b=b;
    }

    public String getA() {
        return a;
    }

    public void setA(String a) {
        this.a = a;
    }

    @Override
    public int compareTo(Object o) {
        if (!(o instanceof Child))
            return -1;

            Child p=(Child) o;
           return this.getA().compareTo(p.getA());


    }
}



public class test {
    public static void main(String[] args) {
      List<Child> list=new LinkedList<Child>();



      list.add(new Child("3",3));
        list.add(new Child("5",5));
        list.add(new Child("1",1));
        System.out.println(list);
        Collections.sort(list);
        System.out.println(list);






    }

}

3.2.2 Comparator

        比较器

匿名内部类,写在测试类中,而是不定义的类中。

public class test {
    public static void main(String[] args) {
      List<Child> list=new LinkedList<Child>();



      list.add(new Child("3",3));
        list.add(new Child("5",5));
        list.add(new Child("1",1));
        System.out.println(list);
        
        Collections.sort(list, new Comparator<Child>() {
            @Override
            public int compare(Child o1, Child o2) {
                    return o1.getB()-o2.getB();
            }
        });
        System.out.println(list);


    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值