Java基础11-List;Set;Map

作业解析:

  1. remove(int index); //删除指定位置的元素

    List list = new ArrayList();
    list.add("s1");
    list.add("s2");
    list.add("s3");

    OutputList(list);// s1,s2,s3

    // 删除指定位置的元素
    list.remove(0);
    OutputList(list);// s2,s3

    list.add("s1");
    Student s1 = new Student("s-1", 1, 'f');
    Student2 s2 = new Student2("s-2", 2, 'm');
    list.add(s1);
    list.add(s2);
    OutputList(list);//s1,s2,s3,s-1,s-2

  2. remove(Object o); //删除指定对象,考虑删除对象的规则是什么?

    // 删除指定对象,考虑删除规则是什么?equals方法?

    // 重写Equals方法,按对象名称删除
    System.out.println("重写Equals方法,按对象名称删除");
    list.remove(s1);
    OutputList(list);//s1,s2,s3,s-2

    // 重写Equals方法,按匿名对象删除
    list.add(s1);
    System.out.println("重写Equals方法,按匿名对象删除");
    list.remove(new Student("s-1", 1, 'f'));
    OutputList(list);//s1,s2,s3,s-2

    // 没有重写Equals方法,按对象名称删除
    System.out.println("没有重写Equals方法,按对象名称删除");
    list.remove(s2);
    OutputList(list);//s1,s2,s3

    // 没有重写Equals方法,按匿名对象删除
    System.out.println("没有重写Equals方法,按匿名对象删除");
    list.add(s2);
    list.remove(new Student("s-2", 2, 'm'));
    OutputList(list);//s1,s2,s3,s-2

    list.add(s1);

  3. removeAll(Collection col);//删除指定集合中的所有元素。

    //删除指定集合中的所有元素

    List list2 = new ArrayList();
    list2.add("s1");
    list2.add(new Student("s-1", 1, 'f'));

    //3.1 指定集合中的元素都在大集合中
    System.out.println("指定集合中的元素都在大集合中");
    list.removeAll(list2);
    OutputList(list);//s2,s3,s-2

    //3.2 指定集合中,存在某一元素不在大集合中
    System.out.println("指定集合中,存在某一元素不在大集合中");
    list.addAll(list2);
    list2.add(new Student("s-3",3,'f'));
    list.removeAll(list2);
    OutputList(list);//s1,s2,s3,s-2,s-1,错误,删除含有指定集合中的那些元素,剩下s2,s3,s-2

    //3.3 没有重写Equals方法的元素在指定集合中
    System.out.println("没有重写Equals方法的元素在指定集合中");
    list.addAll(list2);
    list2.add(new Student2("s-2",2,'m'));
    list.removeAll(list2);
    OutputList(list);//s2,s3,s-2,不会删掉匿名对象,对于重写Equals方法的匿名对象,会删除

  4. contains(Object o); //是否包含

    list.addAll(list2);
    System.out.println(list.contains(s2));//true
    System.out.println(list.contains(s1));//true
    System.out.println(list.contains(null));//false
    System.out.println(list.contains(new String("s1")));//true
    System.out.println(list.contains(new Student("s-1",1,'f')));//true
    System.out.println(list.contains(new Student2("s-2",2,'m')));//false,没有重写equals方法的匿名对象

  5. contains(Collection col); //是否包含集合。

    System.out.println("------list------");
    OutputList(list);
    System.out.println("------list2------");
    OutputList(list2);
    System.out.println(list.contains(list2));//true,结果是false,why?
    System.out.println(list.contains("s2"));//true

    List list3 = new ArrayList();
    list3.add("s1");
    list3.add(new Student("s-1", 1, 'f'));
    System.out.println(list.contains(list3));//true,结果是false,why?
    list3.remove(new Student("s-1", 1, 'f'));
    System.out.println(list.contains(list3));//true, 结果依然是false, 没有contains(Collection col)方法,只有contains(Object o)
    System.out.println(list.contains("s1"));//true
    System.out.println("--list3---");
    OutputList(list3);

    System.out.println(list.containsAll(list2));//true,有containsAll(Collection col)方法
    list.add(new Student2("s-2",2,'m'));
    list2.add(new Student2("s-2",2,'m'));
    System.out.println(list.contains(list2));//false
    System.out.println(list.containsAll(list2));//false

List

  1. 可重复
    只跟equals方法有关
    如果是集合中的对象,但是equals方法返回了false,contains()返回false

  2. 有序

  3. ArrayList
    优缺点:查询快, 写入慢

  4. LinkedList
    链表:手拉手

  5. Vector 线程安全的

    //添加重复元素
    List list = new ArrayList();
    Student s = new Student("s1",1);
    list.add(s);
    list.add(s);
    list.add(s);
    for(int i=0;i<list.size();i++) {
    System.out.println(list.get(i));
    }//打印出三个相同的地址

    //提取第一个元素,进行修改
    list.get(0).setName("jerry");

    System.out.println(list.get(2).getName());//jerry

    //提取元素,进行修改
    list.add(new Student("s2",2));
    list.add(new Student("s2",2));
    list.add(new Student("s2",2));
    list.add(new Student("s2",2));
    list.get(4).setName("tom");
    System.out.println(list.get(5).getName());//s2,内容一样,但地址不一样

==和equals区别

  1. ==

    判断对象是否是同一个,地址是否相同

  2. equals

    判断对象的内容是否相同,默认调用的是Object的equals()方法,而该方法判断的仍然是地址是否相同

  3. 如果没有重写equals方法,和==的效果一样

Set

HashSet
  1. 先通过hashcode

  2. 然后通过equals方法
    若将equals写死为false,则同一个对象也是不同的,但只能重复添加一次

    Set set = new HashSet();

    System.out.println("----------1.相同物理地址,hashcode相同,无法添加-------------");
    Person p = new Person("p1",1);
    //Set集合不能重复添加元素
    set.add(p);
    set.add(p);
    set.add(p);
    Iterator it = set.iterator();
    while(it.hasNext()) {
    System.out.println(it.next());
    }

    System.out.println("----------1.相同物理地址,hashcode不同,可以添加-------------");
    p = new Person("p1",1);
    set = new HashSet();
    set.add(p);
    set.add(p);
    set.add(p);
    it = set.iterator();
    while(it.hasNext()) {
    System.out.println(it.next());
    }

    //匿名对象,可以重复添加,元素是否相同存在一个判断准则---hashcode
    System.out.println("----------2.不同物理地址,hashcode相同 equals=false==>可以添加-------------");
    set=new HashSet();
    Person p2 = new Person("p3",3);
    Person p3 = new Person("p3",3);
    set.add(p2);
    set.add(p3);
    it = set.iterator();
    while(it.hasNext()) {
    System.out.println(it.next());
    }

    System.out.println("----------3.不同物理地址,hashcode相同 equals=true==>无法添加-------------");
    set=new HashSet();
    p2 = new Person("p3",3);
    p3 = new Person("p3",3);
    set.add(p2);
    set.add(p3);
    it = set.iterator();
    while(it.hasNext()) {
    System.out.println(it.next());
    }

    System.out.println("---------4.不同物理地址,hashcode不同 equals=false==>可以添加-------------");
    set=new HashSet();
    p2 = new Person("p3",3);
    p3 = new Person("p3",3);
    set.add(p2);
    set.add(p3);
    it = set.iterator();
    while(it.hasNext()) {
    System.out.println(it.next());
    }

    System.out.println("---------5.不同物理地址,hashcode不同 equals=true==>可以添加-------------");
    set=new HashSet();
    p2 = new Person("p3",3);
    p3 = new Person("p3",3);
    set.add(p2);
    set.add(p3);
    it = set.iterator();
    while(it.hasNext()) {
    System.out.println(it.next());
    }

TreeSet
  1. 使用比较方法判断对象是否重复。

  2. 比较方法实现有两种

  • 自定义Comparator比较器,和TreeSet关联。
  • 让javaBean实现Comparable接口,实现CompareTo()方法。
  1. TreeSet可以容纳null元素。

  2. TreeSet可以使用降序排列。
    通过descendingIterator()方法得到降序迭代器实现。

  3. TreeSet默认升序排列。

    Comparator comp = new Comparator(){
    public int compare(Person o1,Person o2){
    if(o1null){
    if(o2
    null){
    return 0;
    }
    else
    return -1;
    }
    else{
    if(o2==null){
    return 1;
    }
    else
    return o1.getAge()-o2.getAge();
    }
    }
    };
    //通过Comparator构造TreeSet
    TreeSet ts = new TreeSet(comp);
    ts.add(null);
    ts.add(null);
    Person p1 = new Person("p1",1);
    ts.add(new Person("p1",1));
    ts.add(new Person("p3",3));
    ts.add(new Person("p2",2));
    ts.add(new Person("p4",1));
    System.out.println(ts.size()); // 4, 通过comparator的compare方法判断

    Iterator it = ts.iterator();
    while(it.hasNext()){
    Person p1 = (Person)it.next();
    if(p1!=null){
    System.out.println(p1.getName());
    }
    else{
    System.out.println("No body");
    }
    }// 默认升序,No body, p1,p2,p3
    }

    //通过实现Comparable接口,构造TreeSet
    TreeSet tss = new TreeSet();
    tss.add(new Dog("black",20,"京巴"));
    tss.add(new Dog("black",19,"京巴"));
    tss.add(new Dog("white",20,"京巴"));
    tss.add(new Dog("yellow",20,"藏獒"));
    tss.add(new Dog("black",20,"藏獒"));
    System.out.println(tss.size());
    //it = tss.iterator();
    it = tss.descendingIterator();//获得降序的迭代器
    while(it.hasNext()) {
    Dog d = (Dog)it.next();
    System.out.println(d);
    }// “藏獒”yellow, 藏獒”black, “京巴”white, “京巴”black 20, 京巴”black 19

    //实现Comparable接口的Dog类
    class Dog implements Comparable{
    private String category = "";
    private String color = "";
    private int weight;

    public int compareTo(Object o){
    if(othis){
    return 0;
    }
    if(o
    null){
    return 1;
    }
    if(o.getClass()==Dog.class){
    Dog dog = (Dog)o;
    int catResult = this.getCategory().compareTo(dog.getCategory());
    //类别不同
    if(catResult!=0){
    return catResult;
    }
    //类别相同,判断颜色
    else{
    int colResult = this.getColor().compareTo(dog.getColor());
    if(colResult!=0){
    return colResult;
    }
    //颜色相同,判断体重
    else{
    return this.getWeight()-dog.getWeight();
    }
    }
    }
    else{
    System.exit(-1);
    }
    return -1;
    }

    public void toString(){
    System.out.println(this.getCategory()+":"+this.getColor()+":"+this.getWeight());
    }
    }

Map

HashMap

key(键) - value(值): kv对

  1. 映射

  2. Key - Value对:称为Entry, Key不能重复,无序

  3. map.size()指的是条目个数

  4. Map接口是单独的接口,没有继承任何接口,与之前的jdk版本不同

  5. put:放入元素,将key和value进行关联,如果key已经存在,则value被覆盖

  6. get(key): 提取元素

  7. 迭代

    entrySet

    keySet

  8. HashMap.hash()方法意义:将新hash值跟更多的特征值相关

  9. Node<K,V>

    class Node<K,V>{
    int hash; //新hash
    final K key;
    V value;

    Node<K,V> next;
    }

  10. 元素重复判断标准

    if (p.hash == hash &&
    ((k = p.key) == key || (key != null && key.equals(k))))

  • new hashCode不同,对象不同

  • new hash相同,对象是同一对象,则对象相同

  • new hash相同,对象不是同一对象,再判断equals方法,则对象相同性判定和equals一致

  1. 增删改查操作

    Map<String, Dog> map = new HashMap<String, Dog>();
    Dog dog = new Dog("",12,"");
    map.put("dog-1000-1981", dog);
    map.put("dog-1000-1982", dog);
    map.put("dog-1000-1983", dog);
    map.put("dog-1000-1984", dog);
    map.put("dog-1000-1985",  new Dog("",12,""));
    map.put("dog-1000-1985",  new Dog("",13,""));//将原本存在的元素修改了
    System.out.println(map.size());//5

    System.out.println(map.get("dog-1000-1986"));//null

    //迭代Map中的条目
    //1. 利用KeySet
    Set keySet = map.keySet();
    for(String key: keySet){
    //Dog dog = map.getValue(key);
    Dog dog = map.get(key);
    System.out.println(key+":"+dog);
    }

    //2. 利用EntrySet
    //EntrySet<Map<String,Dog>> entries = map.entrySet();
    Set<Entry<String,Dog>> entries = map.entrySety();
    for(Entry<String,Dog> entry: entries){
    String key = entry.getKey();
    Dog dog = entry.getValue();
    System.out.println(key+":"+dog);
    }

    //删除条目
    Dog dog = map.remove("dog-1000-1985");//null
    System.out.println("删除了"+dog);

HashTable

线程安全的,练习增删改查

    Hashtable<String, Dog> ht = new Hashtable<String, Dog>();
    ht.put("dog1981", new Dog("",10,""));
    ht.put("dog1982", new Dog("",10,""));
    ht.put("dog1983", new Dog("",10,""));
    ht.put("dog1982", new Dog("",12,""));//改掉了原本存在的元素
    System.out.println(ht.size());//3

    //利用keySet迭代元素
    Set<String> keySet = ht.keySet();
    for(String key: keySet) {
        Dog dog = ht.get(key);
        System.out.println(key+":"+dog);
    }

    System.out.println("=================");
    //利用EntrySet迭代
    Set<Entry<String,Dog>> entrySet = ht.entrySet(); 
    for(Entry<String,Dog> entry: entrySet) {
        String key = entry.getKey();
        Dog dog = entry.getValue();
        System.out.println(key+":"+dog);
    }

    //删除元素
    Dog dog = ht.remove("dog1988");
    System.out.println("删除了"+dog);//null

    dog = ht.remove("dog1982");
    System.out.println("删除了"+dog);

    //put即可以修改条目
    ht.put("dog1982",new Dog("black",15,"京巴"));
    System.out.println(ht.get("dog1982"));//get(key)即可以根据键值查找元素

作业

  1. 取出整数的16进制表示形式 \u00ff

  2. 取出整数的8进制表示形式

  3. 使用韩语字符集实现字符串编解码

  4. 字符串倒序排列

  5. 练习自动拆箱和装箱

  6. 实现equals方法,比较四方面要素,Student

  7. 创建集合,存放10个person对象,5个Car对象,3个Bee对象,迭代集合,输出每个对象的name属性

  8. 使用Map集合实现嵌套: 操场上六个年级,每个年级有10班,每个班有60人(String)
    tom-x-y-n

  9. 定义罪犯Criminal类,height(身高)/weight(体重)/blood(血型)/home(籍贯)属性。
    重写hashcode和equals,使用四个属性的组合进行实现。
    创建HashSet集合,里面存放20个Criminal对象,其中O型血2人,A型血3人,B型血4人,AB型血1人,其余血型不详。
    注意:hashcode()方法实现时,要求身高、体重、和血型三个属性合成一个数字,实现两两比较的高效算法。

  10. 创建HashMap, Person为key, Dog 为value。
    存放100元素,遍历map集合,两种方式:EntrySet + KeySet

posted @ 2019-04-15 10:46  Shinesu  阅读(215)  评论(0)    收藏  举报