java 列表与集合总结

列表与集合  (一切输出都用for each!丢弃迭代器)

列表List

1 顺序表 Arraylist  适用于静态查找
2   链式双向表 Linkedlist 适用于增删该查
3 (容器) Vector  适用于多线程 使得数据同步  

集合Set

1 HashSet  无序 可null ---- 
2 TreeSet  可排序 不可null 通过compareTo和Comparator排序
3 LinkedHashSet     可排序 可null 按照插入顺序排序
都有以下方法
  1 add增remove删contains查size大小retainAll交集Clear清除
  2 SortedSet s=Collections.synchronizedSortedSet(new SET方法); 加入使得集合可支持同步(多线程)
    3 当类作为集合类型 要进行比较时,
    ①对于HashSet  LinkedHashSet   系统会先判断hashCode()是否相同,若相同再判断equals()函数,符合两条才确定相同
     解决方法:
      1) 需要重写hashCode()函数 否则按照父类object的会比较哈希值
      2 )重写equals()函数
      3 )附加重写toString方法
    ②对于TreeSet  通过compareTo和Comparator排序
     解决方法:
        1)继承comparable接口
                        2 )重写compareTo函数

工具类

Arrays 处理数组

排序:  sort
查找  binarySearch
批量复制   copyOf
批量赋值   fill
等价性比较  equals

Collections 处理collection及其子类

排序:  sort
查找  binarySearch
批量赋值   fill
找最大最小值   max,min
反序  reverse
自定义对象排序时
1 要实现Comparable 接口 重写compareTo方法
2 无源码时 调用比较器Comparator 重写compare()方法
 
 
 
 

以上是对其部分总结 下方为具体实现

=======================================================================================

一.list接口()
  1.有序的
  2.允许多个null元素

  3.对列表每个元素的插入位置进行精确控制

  具体实现类ArrayList,Vector,LinkedList

使用规则:

1.安全性问题           //用vector
2.是否频繁插入,删除操作   //用linkedlist
3.是否存储后遍历        //用arraylist

======================================================

   ①  Vector
  * 1.实现原理 采用动态对象数组实现 默认构造方法创建大小为10的对象值
  * 2.扩充算法:当增量为0扩充为两倍
  * 3.不适合进行删除和插入操作
  * 4.为防止扩充次数过多 建议给定初始容量
  * 5.线程安全 适合在多线程访问使用,单线程效率低

    public static void vector() {
        Vector<String> v=new Vector<String>();
        v.add("a");
        v.add("b");
        v.add("c");
        v.add("f");
        //遍历集合
        for(int i=0;i<list.size();i++) {
            System.out.println(list.get(i));
        }    
    }

    ②  ArrayList()
  * 1.实现原理 采用动态对象数组实现
  * 2.第一次添加元素拓展容量为10 之后扩充算法:原来大小*1.5
  * 3.不适合进行删除和插入操作
  * 4.为防止扩充次数过多 建议给定初始容量
  * 5.适用于单线程 效率高

public static void arraylist() {
        //     * 前面不定义<>类型使用集合存储多个不同类型的元素
        // * 定义类型后<类型>应在一个集合中存同意一类型
        List<String> list=new ArrayList();
        list.add("a");
        list.add("b");
        list.add("c");
        list.add("f");
//        list.add(20);
        
        //遍历集合
        for(int i=0;i<list.size();i++) {
            System.out.println(list.get(i));
        }    
    }

     ③ LinkedList
  * 1.实现原理:采用双向链表结构实现
  * 2.适合插入删除操作,性能高
  * 在实际开发中 选择list的具体实现

================================================================================================================================================================================

  二.set接口
 * 1.无序的(不保证顺序)
 * 2.不允许重复元素
 * HashSet,TreeSet,LinkedHashSet


  ** 使用选择
 * HashSet,       不排序,不用保证顺序
 * TreeSet,         要排序
 * LinkedHashSet   不要排序,要录入顺序

===================================================

  ①HashSet
  * 1.实现原理,基于哈希表(HashMap)实现
  * 2.不允许重复(重复会覆盖),可以有一个null元素
  * 3.不保证顺序恒久不变
  * 4.添加元素时把元素作为HashMap的key存储,value使用一个固定对象
  * 5.两对象是否相同
   (1)先哈希值比较
   (2)若1正确 再用equal函数判断 才可确定
  * 6.若自定义对象认为属性值都相同时为同一对象, 则需重写对象所在类的HashCode和equal方法
  *
  * 小结
  * ①.哈希表:数组+链表
  * ②.把对象存到哈希表中,先计算对象的hashcode值 再对数组的长度求余数 得到结果不同存于数组,相同存于链表

private static void hashset() {
        Set<String> set=new HashSet<String>();
        set.add("ok");
        set.add("no");
        set.add("ok");
        String[] str=set.toArray(new String[] {});
        for(String s:str)
            System.out.println(s);
    }
public static void main(String[] args) {
        hashset();
    }
    

  ②treeset

 *  1.有序的,基于TreeMap(二叉树数据结构),对象需要比较大小通过对象比较器来实现
  *  2.对象比较器可以用来去除重复元素,
  *  3.自定义的数据类,没有实现比较器的接口,将无法添加到Treeset集合中

    private static void treeset() {
        Set<hero>tree=new TreeSet<hero>(new herocomparator());
        hero h1=new hero("卡特",1,20);
        hero h2=new hero("龙女",2,22);
        hero h3=new hero("男刀",3,32);
        hero h4=new hero("ez",4,19);
        tree.add(h1);
        tree.add(h2);
        tree.add(h3);
        tree.add(h4);
        for(hero h:tree)
            System.out.println(h);        
    }

  ③LinkedHashSet<> 

  *1.哈希表和链接列表实现
  * 2.维护着一个运行于所有条目的双重链接列表。此链接列表定义了迭代顺序,即按照将元素插入到set中的顺序进行迭代

    private static void linkedhashset() {
        LinkedHashSet<hero> link=new LinkedHashSet<hero>();
        hero h1=new hero("卡特",1,20);
        hero h2=new hero("龙女",2,22);
        hero h3=new hero("男刀",3,32);
        hero h4=new hero("ez",4,19);
        link.add(h1);
        link.add(h2);
        link.add(h3);
        link.add(h4);
        for(hero h:link)
            System.out.println(h);        
    }

============补充==========

  上述的set接口所用的hero类

/**
 * 
 */

/**
 * @author CF
 * @hero 类做setdemo示例
 * 2019年3月11日
 * 下午1:53:23
 */
public class hero {
    private String name;
    private int id;
    private int age;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public hero() {
        super();
        // TODO Auto-generated constructor stub
    }
    public hero(String name, int id, int age) {
        super();
        this.name = name;
        this.id = id;
        this.age = age;
    }
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + age;
        result = prime * result + id;
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        return result;
    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        hero other = (hero) obj;
        if (age != other.age)
            return false;
        if (id != other.id)
            return false;
        if (name == null) {
            if (other.name != null)
                return false;
        } else if (!name.equals(other.name))
            return false;
        return true;
    }
    @Override
    public String toString() {
        return "hero [name=" + name + ", id=" + id + ", age=" + age + "]";
    }
    
}

  上述treeset()方法所用的herocomparator比较器接口

import java.util.Comparator;
/**
 * @author CF
 * @做啥?
 * 2019年3月11日
 * 下午2:06:20
 */
public class herocomparator implements Comparator<hero>{
    /* 
     * 覆盖了父类的compare函数
     */
    @Override
    public int compare(hero o1, hero o2) {
        
        return o1.getAge()-o2.getAge();
    }
}

  Iterator类 用于集合的输出(迭代)

public class Iteratordemo {

    private static void iterator1(Collection<hero> c) {
        for(hero h:c) {
            System.out.println(h);
        }
    }
    private static void iterator2(Collection<hero> c) {
        Iterator<hero> it=c.iterator();
        while(it.hasNext()) {
            System.out.println(it.next());
        }
        
    }
    public static void main(String[] args) {
        Set<hero>tree=new TreeSet<hero>(new herocomparator());
        hero h1=new hero("卡特",1,20);
        hero h2=new hero("龙女",2,22);
        hero h3=new hero("男刀",3,32);
        hero h4=new hero("ez",4,19);
        tree.add(h1);
        tree.add(h2);
        tree.add(h3);
        tree.add(h4);
        iterator2(tree);
    }
}
原文地址:https://www.cnblogs.com/cc123nice/p/10506269.html