java复习笔记5

  java的常用类集

    接口的继承关系:

      Collection继承了Interable

      List,Set,Queue, SortedSet, Vector继承了Collection

 

      SortedMap继承了Map

     最早的java是使用Collection接口的,  随着java的发展, 使用了ListSet等构造函数

    List接口:可以存放重复的内容;

    Set接口:不能存放重复的内容, 内容通过hashCode和equals这两个方法进行区分的。

    Queue:队列接口;

    SortedSet接口:集合支持排序;

  ArrayList

  verctor接口和List是一样的,是历史原因遗留的类;

运行下面代码

复制代码
import java.util.*;
public class ArrayListDemo {
    public static void main(String args[]) {
        List<String> l = new ArrayList<String>();
        List<String> l1 = new ArrayList<String>();
        //add
        l.add("hell0 ");
        l.add("world ");
        l1.add("haha");
        //addAll
        l.addAll(l1);
        System.out.println(l);
        //remove
        l.remove(0);
        System.out.println(l);
        //isEmpty
        System.out.println(l.isEmpty());
        //indexOf
        System.out.println(l.indexOf("haha"));
        //contains
        System.out.println(l.contains("haha"));
        Iterator it = l.iterator();
        while(it.hasNext()){
            System.out.println(it.next());
        }
    }
}
复制代码

 

  Vector

运行下面代码

复制代码
import java.util.*;
public class VectorDemo {
    public static void main(String args[]) {
        Vector<String> l = new Vector<String>();
        Vector<String> l1 = new Vector<String>();
        //add
        l.addElement("hell0 ");
        l.addElement("world ");
        l1.addElement("haha");
        //addAll
        l.addAll(l1);
        System.out.println(l);
    }
}
复制代码

 

  LinkedList

  LinkedList继承并扩充了queue和List的方法, 可以在数组头部或者尾部添加元素, 如果经常对数组进行操作的话,可以用这个方法, linkedListpoll就是js中的pop方法, addLast就是js中的push方法

运行下面代码

复制代码
import java.util.*;
public class LinkedDemo {
    public static void main(String args[]) {
        LinkedList<String> link = new LinkedList<String>();
        link.add("1");
        link.add("2");
        link.addFirst("x");
        link.addLast("Y");
        System.out.println(link);
        link.removeLast();
        link.removeFirst();
        System.out.println(link);

        //element
        System.out.println(link.element());
        //peek
        System.out.println(link.peek());
        //pool
        System.out.println(link.poll());
        System.out.println(link);
    }
}
复制代码

  TreeSet

  TreeSet中的元素必须实现Comparable接口才ok , 而且TreeSet的compareTo如果返回的int是0, 那么元素会被删除,去重, 如果返回-1会排到前面, 返回1 的话相反, 会排到后面;

运行下面代码

复制代码
import java.util.*;
class Person implements Comparable<Person>{
    public String name ;
    public int age ;
    public Person (String name, int age){
        this.name = name;
        this.age = age;
    }
    public String toString() {
        return this.age+this.name;
    }
    public int compareTo(Person p) {
        if(this.age<p.age) {
            return -1;
        }else if(this.age>p.age){
            return 1;
        }else{
            return 0;
        }
    }
}
public class TreeSetDemo{
    public static void main(String args[]) {
        Set<Person> set = new TreeSet<Person>();
        set.add(new Person("a",10));
        set.add(new Person("b",20));
        set.add(new Person("c",30));
        set.add(new Person("c",30));
        set.add(new Person("c",30));
        set.add(new Person("c",30));
        set.add(new Person("c",30));
        System.out.println(set);
    }
}
复制代码

   

  HashSet

  利用HashSet实现的去重排序, (Java中的hashCode方法就是根据一定的规则将与对象相关的信息(比如对象的存储地址,对象的字段等)映射成一个数值,这个数值称作为散列值);

  "a".hashCode === "a".hashCode; 而且hashCode必须为97, 只有一个字符的时候hashCode就是ascll码, 其他的就不确定了, 要去看jvm是怎么实现的;

  HashSet实现了equals相等和hasCode相等, 那么HashSet内容相等的会被删除掉:

运行下面代码

复制代码
import java.util.*;
class Person{
    public String name ;
    public int age ;
    public Person (String name, int age){
        this.name = name;
        this.age = age;
    }
    public String toString() {
        return this.name+this.age;
    }
    public boolean equals(Object obj) {
        if(this == obj) {
            return true;
        }
        Person p  = (Person)obj;
        if(this.age == p.age && this.name == p.name) {
            return true;
        }
        return false;
        
    }
    public int hashCode() {
        //System.out.println(this.name.hashCode());
        return this.name.hashCode()*this.age;
        //Random ran = new Random();
        //return ran.nextInt(100);
    }
}
public class HashSetDmeo{
    public static void main(String args[]) {
        Set<Person> set = new HashSet<Person>();
        set.add(new Person("a",10));
        set.add(new Person("b",20));
        set.add(new Person("c",30));
        set.add(new Person("c",30));
        set.add(new Person("c",30));
        set.add(new Person("c",30));
        set.add(new Person("c",30));
        System.out.println(set);
    }
}
复制代码

 

  SortedSet

   SortedSet是TreeSet的抽象实现, 要使用SortedSet的话要通过实例化TreeSet的方式实现:

运行下面代码

SortedSet<String> ss = new TreeSet<String>();

 

  测试代码

运行下面代码

复制代码
import java.util.*;
public class SortSetDemo1 {
    public static void main(String args[]) {
        SortedSet<String> ss = new TreeSet<String>();
        ss.add("a");
        ss.add("d");
        ss.add("c");
        ss.add("b");
        System.out.println(ss.first());
        System.out.println(ss.last());
        System.out.println(ss.headSet("a"));
        System.out.println(ss.tailSet("b"));
        System.out.println(ss.subSet("a","b"));
    }
}
复制代码

 

   Iterator遍历数组

运行下面代码

复制代码
import java.util.*;
public class IteratorDemo {
    public static void main( String args[] ) {
        List<String> l = new ArrayList<String>();
        l.add("1");
        l.add("2");
        l.add("3");
        l.add("4");
        Iterator<String> it = l.iterator();
        while(it.hasNext()) {
            System.out.println(it.next());
            it.remove();
        }
        System.out.println(l);
    }
}
复制代码

 

  

  ListIterator

  ListIterator如果要进行由后向前输出, 必须先由前向后输出:

运行下面代码

复制代码
import java.util.*;
public class ListIteratorDemo {
    public static void main( String args[] ) {
        List<String> l = new ArrayList<String>();
        l.add("1");
        l.add("2");
        l.add("3");
        l.add("4");
        ListIterator<String> it = l.listIterator();
        while(it.hasNext()) {
            System.out.println(it.next());
        }
        while(it.hasPrevious()) {
            System.out.println(it.previous());
        }
    }
}
复制代码

 

  

  Enumeration迭代

  Enumeration的迭代, 主要是迭代Vector,这个类也是历史遗留下来的,用的比较少;

运行下面代码

复制代码
import java.util.*;
public class EnumerationDemo{
    public static void main(String args[]) {
        Vector<String> v = new Vector<String>();
        v.add("a");
        v.add("b");
        v.add("c");
        v.add("d");
        Enumeration<String> e = v.elements();
        while(e.hasMoreElements()) {
            System.out.println(e.nextElement());
        }
    }
}
复制代码

 

本文作者:方方和圆圆

本文链接:https://www.cnblogs.com/diligenceday/p/5230458.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   方方和圆圆  阅读(213)  评论(0编辑  收藏  举报
历史上的今天:
2014-03-03 最近新学的小东西和单词

再过一百年, 我会在哪里?

💬
评论
📌
收藏
💗
关注
👍
推荐
🚀
回顶
收起
点击右上角即可分享
微信分享提示