Java集合

1、Collection接口

  Collection接口是集合中单值保存的最大父接口(每次向集合中保存一个对象),其方法如下:

No

方法

类型

描述

1

Public Boolean add(E e)

普通

向集合里保存数据

2

addAll(Collection<? Extends E> c)

普通

追加一个集合

3

Public void clear()

普通

清空集合

4

Public Boolean contains(Object o)

普通

判断是否包含有指定的内容,需要equals支持

5

Public Boolean isEmpty()

普通

判断集合是否为空

6

Public Boolean remove(Object o)

普通

删除对象,需要equals支持

7

Public int size()

普通

获取集合中元素个数

8

Public Object[] toArray()

普通

将集合变为数组保存

9

Public Iterator <E > iterator()

普通

实例化Iterator接口

在实际使用一般不会直接使用Collection接口,而是使用子接口:List(允许重复)、Set(不允许重复)

2、List接口

  List是Collection中最常用的接口,其数据的保存顺序就是数据的添加顺序,并且可以保存重复的元素,其在Collection的基础上还有一些常用的方法:

No

方法

类型

描述

1

Public E get(int index)

普通

获得索引编号的内容

2

Public E set(int index, E element)

普通

修改指定索引号编号的内容

3

Public ListIterator<E> listIterator()

普通

实例化ListIterator

若使用List接口,一般是使用其子接口ArrayList子类

2.1 ArrayList

在集合中保存数据:

import java.util.ArrayList;
import java.util.List;

public class ListDemo {
    public static void main(String[] args) throws Exception{
        List<String> all = new ArrayList<String>(); //设置泛型,保证集合中所有的数据类型都一样
        System.out.println("长度:" + all.size() + ",是否为空:" + all.isEmpty());
        all.add("Hello");
        all.add("Hello");
        all.add("World");
        System.out.println("长度:" + all.size() + "是否为空:" + all.isEmpty());
        System.out.println(all); //输出所有元素,以列表的形式表现
        //size方法可以获取集合元素个数
        for (int x= 0;x<all.size();x++){   //get方法可以根据索引取得数据
            String str = all.get(x);  //取得数据
            System.out.println(str);
        }
    }
}

  在集合中保存对象

import java.util.ArrayList;
import java.util.List;

class Book{
    private String title;
    private Double price;

    public Book(String title, Double price) {
        this.title = title;
        this.price = price;
    }
    public boolean equals(Object object){
        if (this == object)
            return true;
        if (object == null)
            return false;
        if (!(object instanceof Book))
            return false;
        Book book = (Book) object;
        if (this.title.equals(book.title) && this.price == book.price){
            return true;
        }
        return false;
    }
    public String toString(){
        return "书名:" + this.title + ",价格:" + this.price + "
";
    }
}
public class ListDemo {
    public static void main(String[] args) throws Exception{
        List<Book> all = new ArrayList<Book>();
        all.add(new Book("Java",80.0));
        all.add(new Book("python",99.9));
        all.add(new Book("C++",60.5));
        System.out.println(all);
        all.remove(new Book("Java",80.0));
        System.out.println(all);
        }
    }
}  

2.2 Vector

  Vector与ArrayList不同之处在于Vecrot在线程中使用时数据更加安全,采用同步处理,其使用方法与ArrayList类似

 

3、Set接口

  Set接口是Collection接口下的另一个接口,与List不同的是Set接口只是简单继承了Collection接口,没有进行扩充,也没有get方法,并且不能保存重复元素,其共有HashSet和TreeSet两个子类

 

3.1 HashSet

import java.util.HashSet;
import java.util.Set;

public class HashSetDemo {
    public static void main(String[] args) {
        Set<String> all = new HashSet<String>();
        all.add("Good");
        all.add("Hello");
        all.add("world");
        all.add("Hello");
        System.out.println(all);
    }
}

输出结果:

[world, Hello, Good]

 可以看出HashSet中是不能保存重复元素,且保存的元素是无序存储,与输入顺序没有关联 

 3.2 TreeSet

import java.util.Set;
import java.util.TreeSet;

public class TreeSetDemo {
    public static void main(String[] args) throws Exception{
        Set<String> all = new TreeSet<String>();
        all.add("X");
        all.add("B");
        all.add("A");
        all.add("B");
        System.out.println(all);
    }
}

输出结果:

[A, B, X]

  可以看出TreSet和HashSet一样是不能保存重复元素,但是TreeSet中存储的元素是有序的,与输入顺序相关的

 

 4 Map接口

  Map接口与collection接口不同的是Map接口保存的是一对数据(key = value),类似于python的字典,其拥有以下方法

No

方法

类型

描述

1

Public V put(K key,V value)

普通

向集合中保存数据

2

Public V get (Object key)

普通

根据key查找对应的value

3

Public Set<Map,Entry<K,V>>entrySet()

普通

将Map转化为Set集合

4

Public Set<K> keyset()

普通

取出全部的key

   Map接口中共有以下两个子类:HashMap、Hashtable

 

4.1、HashMap

import java.util.HashMap;
import java.util.Map;

public class HashMapDemo {
    public static void main(String[] args) {
        Map<String,Integer> map = new HashMap<String, Integer>();
        map.put("",1);
        map.put("",2);
        map.put("three",3);
        map.put("three",4);
        map.put("four",3);
        System.out.println(map); //返回{一=1, four=3, 二=2, three=4}
        System.out.println(map.get("three")); //查询key=three的value,返回4
        System.out.println(map.get(1));  //若key不存在则返回null
        System.out.println(map.keySet());// 返回[一, four, 二, three]
    }
}

 HashMap中的数据是无序存放的,与输入顺序无关,若存入的key重复,后输入的key会覆盖之前的key

 

 4.2、Hashtable

import java.util.Hashtable;
import java.util.Map;

public class HashtableDemo {
    public static void main(String[] args) {
        Map<String,Integer> hashtable = new Hashtable<String, Integer>();
        hashtable.put("",1);
        hashtable.put("",2);
        hashtable.put("three",3);
        hashtable.put("three",4);
        hashtable.put("four",3);
        System.out.println(hashtable);// 输出{three=4, four=3, 一=1, 二=2}
        System.out.println(hashtable.get("three")); //输出4
        System.out.println(hashtable.get(1));  //返回null
        System.out.println(hashtable.keySet()); //输出[three, four, 一, 二]
    }
}

HashMap与Hashtable的区别:

No

区别点

HashMap

Hashtable

1

推出时间

JDK1.2推出,属于新的类

JDK1.0推出,属于旧的类

2

性能

异步处理

同步处理

3

数据安全

非线程安全

线程安全

4

设置null

允许key或value内容为null

不允许null

 

 4.3 Map的遍历方法

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class HashMapDemo {
    public static void main(String[] args) {
        Map<String,Integer> map = new HashMap<String, Integer>();
        map.put("",1);
        map.put("",2);
        map.put("three",3);
        map.put("three",4);
        map.put("four",3);
        
        System.out.println("遍历一:");
        for (Map.Entry<String, Integer> entry : map.entrySet()) {
            System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
        }
        System.out.println("遍历二:");
        Iterator<Map.Entry<String, Integer>> iterator = map.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, Integer> entry = iterator.next();
            System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
        }
    }
}

输出结果:

遍历一:
key= 一 and value= 1
key= four and value= 3
key= 二 and value= 2
key= three and value= 4
遍历二:
key= 一 and value= 1
key= four and value= 3
key= 二 and value= 2
key= three and value= 4

 

原文地址:https://www.cnblogs.com/attentle/p/9759523.html