一、集合框架(关于ArrayList,LinkedList,HashSet,LinkedHashSet,TreeSet)

一、ArrayList

  解决了数组的局限性,最常见的容器类,ArrayList容器的容量capacity会随着对象的增加,自动增长。不会出现数组边界的问题。

package collection;
 
import java.util.ArrayList;
 
import charactor.Hero;
 
public class TestCollection {
    @SuppressWarnings("rawtypes")
    public static void main(String[] args) {
        //容器类ArrayList,用于存放对象
        ArrayList heros = new ArrayList();
        heros.add( new Hero("盖伦"));
        System.out.println(heros.size());
         
        //容器的容量"capacity"会随着对象的增加,自动增长
        //只需要不断往容器里增加英雄即可,不用担心会出现数组的边界问题。
        heros.add( new Hero("提莫"));
        System.out.println(heros.size());
         
    }
     
}

二、ArrayList常用方法

首先创建一个重写了toString的Hero类

package charactor;
  
public class Hero {
    public String name;
    public float hp;
  
    public int damage;
  
    public Hero() {
  
    }
  
    // 增加一个初始化name的构造方法
    public Hero(String name) {
  
        this.name = name;
    }
  
    // 重写toString方法
    public String toString() {
        return name;
    }
  
}

1、add:增加{1、直接add对象 hero.add(new Hero("demo")); 2、指定位置增加对象hero.add(3,"demo")}

package collection;
 
import java.util.ArrayList;
 
import charactor.Hero;
 
public class TestCollection {
    public static void main(String[] args) {
        ArrayList heros = new ArrayList();
 
        // 把5个对象加入到ArrayList中
        for (int i = 0; i < 5; i++) {
            heros.add(new Hero("hero " + i));
        }
        System.out.println(heros);
 
        // 在指定位置增加对象
        Hero specialHero = new Hero("special hero");
        heros.add(3, specialHero);
 
        System.out.println(heros.toString());
 
    }
 
}

2、contains():判断是否存在

3、get():获取指定位置的对象

4、indexOf():获取对象所处的位置

5、remove():删除

6、set():替换

7、size():获取大小

8、toArray():转换为数组

三、ArrayList和List

ArrayList实现了接口List,常见的写法会把引用声明为接口List类型

package collection;
  
import java.util.ArrayList;
import java.util.List;
 
import charactor.Hero;
  
public class TestCollection {
 
    public static void main(String[] args) {
        //ArrayList实现了接口List
         
        //常见的写法会把引用声明为接口List类型
        //注意:是java.util.List,而不是java.awt.List
        //接口引用指向子类对象(多态)
         
        List heros = new ArrayList();
        heros.add( new Hero("盖伦"));
        System.out.println(heros.size());
         
    }
      
}

四、ArrayList遍历的方法

1、使用for循环,通过获取ArrayList的size()来一一遍历

5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package collection;
 
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
 
import charactor.Hero;
 
public class TestCollection {
 
    public static void main(String[] args) {
        List<Hero> heros = new ArrayList<Hero>();
 
        // 放5个Hero进入容器
        for (int i = 0; i < 5; i++) {
            heros.add(new Hero("hero name " + i));
        }
 
        // 第一种遍历 for循环
        System.out.println("--------for 循环-------");
        for (int i = 0; i < heros.size(); i++) {
            Hero h = heros.get(i);
            System.out.println(h);
        }
 
    }
 
}

2、使用迭代器Iterator进行遍历,迭代器每次都是从一个空的位置开始,通过hasNext()来判断,当返回false时表示后面没有数据了结束遍历,获取通过next()方法获取。

package collection;
 
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
 
import charactor.Hero;
  
public class TestCollection {
 
    public static void main(String[] args) {
        List<Hero> heros = new ArrayList<Hero>();
         
        //放5个Hero进入容器
        for (int i = 0; i < 5; i++) {
            heros.add(new Hero("hero name " +i));
        }
         
        //第二种遍历,使用迭代器
        System.out.println("--------使用while的iterator-------");
        Iterator<Hero> it= heros.iterator();
        //从最开始的位置判断"下一个"位置是否有数据
        //如果有就通过next取出来,并且把指针向下移动
        //直达"下一个"位置没有数据
        while(it.hasNext()){
            Hero h = it.next();
            System.out.println(h);
        }
        //迭代器的for写法
        System.out.println("--------使用for的iterator-------");
        for (Iterator<Hero> iterator = heros.iterator(); iterator.hasNext();) {
            Hero hero = (Hero) iterator.next();
            System.out.println(hero);
        }
         
    }
      
}

3、常用的for循环遍历.

不能进行ArrayList初始化,也不知道到底多少个元素在ArrayList,就是所有遍历出来

五、ArrayList和LinkedList的区别

ArrayList:顺序结构,插入,删除数据很慢(O(N)),查找快(O(1)),就类似于数组,跟链表的关系一样。

LinkedList:链表结构,插入,删除数据快(O(1)),查找慢(O(N))。

六、ArrayList和HashSet区别

最大区别在于:1、是否有顺序:

ArrayList:有顺序

HashSet:无顺序(集合的无序性),它的具体顺序既不是按照hashcode的顺序,也不是按照插入顺序,根据在JVM的不用版本中,看到的顺序也是不同的,HashSet的顺序本身不稳定的。

2、是否重复

List中的数据可以重复

Set中的数据不能重复

重复判断的标准是:首先看hashcode是否相同,不同,则肯定是不同数据

如果相同,在比较equals,如果equals相同,则是相同数据,否则是不同数据。

七、HashSet,LinkedHashSet,TreeSet比较

HashSet:无顺序

LinkedHashSet:按照插入顺序

TreeSet:从小到大顺序

package collection;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.TreeSet;

public class TestCollection{
    public static void main(String[] args){
        HashSet<Integer> hashSet=new HashSet<Integer>();
        //HashSet中的数据不是按照插入顺序存放
        hashSet.add(22);
        hashSet.add(33);
        hashSet.add(55);
        System.out.println(hashSet);

        LinkedHashSet<Integer> linkedHashSet=new LinkedHashSet<Integer>();
        //LinkedHashSet中的数据按照插入顺序存放
        linkedHashSet.add(22);
        linkedHashSet.add(56);
        linkedHashSet.add(28);
        System.out.println(linkedHashSet);//22,56,28

        TreeSet<Integer> treeSet=new TreeSet<Integer>();
        //TreeSet中的数据是进行了排序的从小到大
        treeSet.add(22);
        treeSet.add(56);
        treeSet.add(28);
        System.out.println(treeSet);//22,28,56
    }
}

原文地址:https://www.cnblogs.com/drq1/p/8482852.html

时间: 2024-08-03 04:32:31

一、集合框架(关于ArrayList,LinkedList,HashSet,LinkedHashSet,TreeSet)的相关文章

安卓 ArrayList,LinkedList,HashSet,Vector,TreeSet的区别和使用

java的集合就那么几种 总体为:List,Set,Map (都是接口由其子类去实现具体的方法) ArrayList,LinkedList,Vector都属于List List:元素是有顺序的,元素可以重复因为每个元素有自己的角标(索引)  |-- ArrayList:底层的数据结构是数组结构,特点是:查询很快,增 删 稍微慢点,线程不同步 |-- LinkedList:底层使用的是链表数据结构,特点是:增 删很快,查询慢. |--Vector:底层是数组数据结构,线程同步,被ArrayList

Java集合详解7:HashSet,TreeSet与LinkedHashSet

Java集合详解7:HashSet,TreeSet与LinkedHashSet 今天我们来探索一下HashSet,TreeSet与LinkedHashSet的基本原理与源码实现,由于这三个set都是基于之前文章的三个map进行实现的,所以推荐大家先看一下前面有关map的文章,结合使用味道更佳. 具体代码在我的GitHub中可以找到 https://github.com/h2pl/MyTech 文章首发于我的个人博客: https://h2pl.github.io/2018/05/12/colle

Java集合框架:ArrayList

ArrayList定义 package java.util; public class ArrayList<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, java.io.Serializable{ private static final int DEFAULT_CAPACITY = 10; private static final Object[] EMPTY_ELEME

【转】Java如何克隆集合——深度拷贝ArrayList和HashSet

原文网址:http://blog.csdn.net/cool_sti/article/details/21658521 原英文链接:http://javarevisited.blogspot.hk/2014/03/how-to-clone-collection-in-java-deep-copy-vs-shallow.html 程序员通常会误用集合类(如List.Set.ArrayList.HashSet)所提供的拷贝构造函数或其它方法来完成集合的拷贝.值得记住的一点是,Java中集合提供的拷贝

深入集合框架之ArrayList源码剖析

ArrayList概述 ArrayList底层由数组实现,非线程安全,但是数组可以动态增加,也可以叫动态数组,提供了一系列的好处,我们来深入看看: 成员变量与构造函数 /** * 存储ArrayList内的数组 */ private transient Object[] elementData; /** * The size of the ArrayList (the number of elements it contains). * ArrayList中包含了多少元素 */ private

[javaSE] 集合框架(ArrayList,LinkedList,Vector)

ArrayList特点:底层使用数组数据结构,查询速度快(使用脚标查),插入删除慢(索引要改变) LinkedList特点:底层使用链表数据结构,查询慢(需要一个一个去问),插入删除快 Vector特点:底层是数组数据结构,线程同步,被ArrayList替代了 ArrayList import java.util.ArrayList; public class ArrayListDemo { public static void main(String[] args) { ArrayList l

java成神之——集合框架之ArrayList,Lists,Sets

集合 集合种类 ArrayList 声明 增删改查元素 遍历几种方式 空集合 子集合 不可变集合 LinkedList Lists 排序 类型转换 取交集 移动元素 删除交集元素 Sets 集合特点 常用方法 根据set创建list 集合 集合种类 List<String> list = new ArrayList<>(data); Set<String> set1 = new HashSet<>(data); // 值不重复 SortedSet<St

集合框架(去除ArrayList集合中的重复字符串元素案例2)

package cn.itcast_04; import java.util.ArrayList; import java.util.Iterator; /* * 需求:ArrayList去除集合中字符串的重复值(字符串的内容相同) * 要求:不能创建新的集合,就在以前的集合上做. */ public class ArrayListDemo2 { public static void main(String[] args) { // 创建集合对象 ArrayList array = new Ar

集合框架(去除ArrayList集合中的重复自定义对象元素案例)

学生类 package cn.itcast_04; public class Student { private String name; private int age; public Student() { super(); } public Student(String name, int age) { super(); this.name = name; this.age = age; } public String getName() { return name; } public v

深入理解java集合框架之---------Arraylist集合 -----构造函数

ArrayList有三个构造方法 ArrayList有三个常量 1.private transient Object[] elementData (数组); 2.private int size (元素个数) 1.ArrayList(int initialCapacity); 构造一个初始容量的集合 /** * 序列化 */ private static final long serialVersionUID = -6277824875242725854L; /** * 定义一个数组 */ pr