java容器

1.Array

数组

int[] a = new int[5];
 
a[0] = 1;
a[1] = 2;
a[2] = 4;
a[3] = 8;
a[4] = 16;

内存中地址

 

 

 

2.arrayAList

数组列表

List<String> names = new ArrayList<>(); 
 
names.add("lokesh");    //[lokesh]
names.add("alex");      //[lokesh, alex]
 
names.set(1, "brian");  //[lokesh, brian]
 
names.remove(1);        //[lokesh]

3.LinkedList

链表列表

import java.util.LinkedList;
import java.util.ListIterator;
 
public class LinkedListExample 
{
    public static void main(String[] args) 
    {
        //Create linked list
        LinkedList<String> linkedList = new LinkedList<>();
         
        //Add elements
        linkedList.add("A");
        linkedList.add("B");
        linkedList.add("C");
        linkedList.add("D");
         
        System.out.println(linkedList);
         
        //Add elements at specified position
        linkedList.add(4, "A");
        linkedList.add(5, "A");
         
        System.out.println(linkedList);
         
        //Remove element
        linkedList.remove("A");     //removes A
        linkedList.remove(0);       //removes B
         
        System.out.println(linkedList);
         
        //Iterate
        ListIterator<String> itrator = linkedList.listIterator();
 
        while (itrator.hasNext()) {
            System.out.println(itrator.next());
        }
    }
}

4.HashMap

哈希Map

import java.util.HashMap;
 
public class HashMapExample 
{
    public static void main(String[] args) throws CloneNotSupportedException 
    {
        HashMap<Integer, String> map = new HashMap<>();
         
        map.put(1,  "A");
        map.put(2,  "B");
        map.put(3,  "C");
         
        System.out.println(map);
    }
}

// output  {1=A, 2=B, 3=C}

 

5.Hashtable

跟hashMap类似, 但是Hashtable是同步的

import java.util.Hashtable;
import java.util.Iterator;
 
public class HashtableExample 
{
    public static void main(String[] args) 
    {
        //1. Create Hashtable
        Hashtable<Integer, String> hashtable = new Hashtable<>();
         
        //2. Add mappings to hashtable 
        hashtable.put(1,  "A");
        hashtable.put(2,  "B" );
        hashtable.put(3,  "C");
         
        System.out.println(hashtable);
         
        //3. Get a mapping by key
        String value = hashtable.get(1);        //A
        System.out.println(value);
         
        //4. Remove a mapping
        hashtable.remove(3);            //3 is deleted
         
        //5. Iterate over mappings
        Iterator<Integer> itr = hashtable.keySet().iterator();
         
        while(itr.hasNext()) 
        {
            Integer key = itr.next();
            String mappedValue = hashtable.get(key);
             
            System.out.println("Key: " + key + ", Value: " + mappedValue);
        }
    }
}


// Output
{3=C, 2=B, 1=A}
A
Key: 2, Value: B
Key: 1, Value: A

 

 

6.LinkedHashMap

这个是有序的

LinkedHashMap<Integer, String> pairs = new LinkedHashMap<>();
         
pairs.put(1,  "A");
pairs.put(2,  "B");
pairs.put(3,  "C");
pairs.put(4,  "D");
 
pairs.forEach((key, value) -> {
    System.out.println("Key:"+ key + ", Value:" + value);
});

// output
Key:1, Value:A
Key:2, Value:B
Key:3, Value:C
Key:4, Value:D

 

7.TreeMap

还是map,不过排序是基于红黑树

import java.util.Iterator;
import java.util.TreeMap;
 
public class LinkedHashMapExample 
{
    public static void main(String[] args) 
    {
        //Natual ordering by deafult
        TreeMap<Integer, String> pairs = new TreeMap<>();
         
        pairs.put(2,  "B");
        pairs.put(1,  "A");
        pairs.put(3,  "C");
         
        String value = pairs.get(3);    //get method
         
        System.out.println(value);
         
        value = pairs.getOrDefault(5, "oops");  //getOrDefault method
         
        System.out.println(value);
         
        //Iteration example
        Iterator<Integer> iterator =  pairs.keySet().iterator();
         
        while(iterator.hasNext()) {
            Integer key = iterator.next();
            System.out.println("Key: " + key + ", Value: " + pairs.get(key));
        }
         
        //Remove example
        pairs.remove(3);
        System.out.println(pairs);
         
        System.out.println(pairs.containsKey(1));    //containsKey method   
         
        System.out.println(pairs.containsValue("B"));    //containsValue method   
 
        System.out.println(pairs.ceilingKey(2));
    }
}

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值