java _ _容器,
java _ _容器,
java 中容器的基本介绍
Collection
├List│├LinkedList
│├ArrayList
│└Vector
│ └Stack
└Set
Map
├Hashtable
├HashMap
└WeakHashMap
Collection 的方法有:
boolean isEmpty(); 如果此 collection 不包含元素,则返回true。
boolean contains(Object o); 如果此 collection 包含指定的元素,则返回true。
Iterator<E> iterator(); 返回在此 collection 的元素上进行迭代的迭代器。
Object[] toArray(); 返回包含此 collection 中所有元素的数组。
boolean add(E e); 添加指定类型的元素到容器
boolean remove(Object o); 从此 collection 中移除指定元素的单个实例,如果存在的话(可选操作)。 注意底层比较用的equals
boolean containsAll(Collection<?> c); 如果此 collection 包含指定 collection 中的所有元素,则返回true。
boolean addAll(Collection<? extends E> c);将指定 collection 中的所有元素都添加到此 collection 中(可选操作)。
boolean removeAll(Collection<?> c);移除此 collection 中那些也包含在指定 collection 中的所有元素(可选操作)。
void clear();移除此 collection 中的所有元素(可选操作)。
int hashCode();返回此 collection 的哈希码值。
Map: 的方法有:
V get(Object key); 返回指定键所映射的值;如果此映射不包含该键的映射关系,则返回
null
。
V put(K key, V value); 将指定的值与此映射中的指定键关联
Set<K> keySet(); 返回此映射中包含的键的
Set
视图。
V remove(Object key); 如果存在一个键的映射关系,则将其从此映射中移除(可选操作)。
Collection<V> values();返回此映射中包含的值的
Collection
视图。
由于List 和Set 是 Collection 的子接口,故也包含这些函数
List:
* 实现 List 接口的子类 区别::
* ArrayList(查询快,插入,修改,删除慢,实现是数组) 线程不安全,效率高
* LinkedList(查询慢,插入,修改,删除快,实现是链表) 线程不安全,效率高
* Verctor (线程安全),效率低
Set:
* set 无序 不重复
底层实现原理:
ArrayList
数组;
LinkedList 链表(单向链表或者双向链表)
HashMap 数组+链表 (利用hashCode 优化)
HashSet 底层采用的 HashMap
另外,附上 迭代器 Iterator 的基本使用方法
Set<String> set=new HashSet<String>();
set.add("zhangsna");
set.add("kim");
set.add("ben");
Iterator it=set.iterator();
while(it.hasNext()){
String value=(String) it.next();
System.out.println(value);
}
此外:jdk1.7 支持的for 增强,也可以用来便历
for(Object o:set){
System.out.println(o);
}
以及:便历Map结构
Map<String,String> map=new HashMap<String,String>();
map.put("aa", "李四");
map.put("bb", "张三");
map.put("cc", "王五");
Set set=map.keySet();
for(Object obj:set){
if(obj.equals("aa")){
System.out.println(map.get("aa"));
}
}
以下是参考代码:
//-------------------------------------------------------------模拟ArrayList
package com.lucas.testCollection;
public class SxtArrayList {
private Object[] elementData;
private int size;
public SxtArrayList(){
this(10);
}
public SxtArrayList(int length){
if(length < 0){
try {
throw new Exception();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
elementData=new Object[length];
}
public boolean add(Object o){
ensureCapacityInternal();
this.elementData[size++]=o;
return true;
}
public Object get(int index){
rangeCheck(index);
return elementData[index];
}
private void rangeCheck(int index){
if(index < 0 || index >= size){
throw new IndexOutOfBoundsException("out of the bound :"+index);
}
}
public int size(){
return this.size;
}
public boolean remove(Object o){
for(int i = 0; i < size;i ++){
int newLength=size-i-1;
if(elementData[i].equals(o)){
System.arraycopy(elementData, i+1, elementData, i, newLength);
elementData[--size]=null;
return true;
}
}
return false;
}
private void ensureCapacityInternal(){
if(size == elementData.length){
Object[] newArray=new Object[size*2 + 1];
System.arraycopy(elementData, 0, newArray, 0, size);
elementData=newArray;
}
}
public Object remove(int index){
rangeCheck(index);
int newLength=size-index-1;
Object o=elementData[index];
System.arraycopy(elementData, index+1, elementData, index, newLength);
elementData[--size]=null;
return o;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
SxtArrayList arrayList=new SxtArrayList(3);
arrayList.add("1111");
arrayList.add("2222");
arrayList.add("333");
arrayList.remove("2222");
// System.out.println(arrayList.size());
for(int i=0;i<arrayList.size();i++){
System.out.println(arrayList.get(i));
}
// System.out.println(arrayList.size());
}
}
//----------------------------------------------------------------模拟Linkedlist
package com.lucas.testCollection;
public class Node {
private Node previous;
private Object obj;
private Node next;
public Node getPrevious() {
return previous;
}
public void setPrevious(Node previous) {
this.previous = previous;
}
public Object getObj() {
return obj;
}
public void setObj(Object obj) {
this.obj = obj;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
}
package com.lucas.testCollection;
public class SxtLinkedList {
private Node first;
private Node last;
private int size;
public void add(Object o){
Node newNode= new Node();
newNode.setObj(o);
if(first ==null){
first=newNode;
last=first;
}else{
last.setNext(newNode);
newNode.setPrevious(last);
last=newNode;
}
size++;
}
public int size(){
return this.size;
}
public Object remove(int index){
Node node=find(index);
Object ob=node.getObj();
if(index < 0 || index >= size){
try{
throw new Exception();
}catch(Exception e){
e.getStackTrace();
}
}
if(index == 0){
first=first.getNext();
first.setPrevious(null);
}else if(index == size -1){
last= last.getPrevious();
last.setNext(null);
}else{
Node temp=find(index);
temp.getPrevious().setNext(temp.getNext());
temp.getNext().setPrevious(temp.getPrevious());
}
size--;
return ob;
}
public Node find(int index){
Node node=new Node();
node=first;
for(int i = 0 ; i< index; i ++){
node=first.getNext();
}
return node;
}
public static void main(String[] args) {
SxtLinkedList slk=new SxtLinkedList();
slk.add("zhangsna");
slk.add("dddd");
slk.add("ffff");
slk.remove(1);
System.out.println(slk.size());
}
}
//----------------------------------------------------------------------模拟hashMap
package com.lucas.testCollection;
import java.util.LinkedList;
public class SxtMap {
private LinkedList[] listArray=new LinkedList[99];
private int size;
public void put(Object key,Object value){
MyMap m=new MyMap(key,value);
System.out.println(key.hashCode());
int a=key.hashCode() % listArray.length;
a=Math.abs(a);
System.out.println(a);
if(listArray[a] == null){
LinkedList tempList=new LinkedList();
listArray[a]=tempList;
tempList.add(m);
}else{
listArray[a].add(m);
}
size++;
}
public Object get(Object key){
int a=key.hashCode() % listArray.length;
a=Math.abs(a);
if(listArray[a] != null){
for(int i = 0; i < listArray[a].size(); i ++){
MyMap myMap=(MyMap)listArray[a].get(i);
if(myMap.getKey().equals(key)){
return myMap.getValue();
}
}
}
return null;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
SxtMap sm=new SxtMap();
sm.put("zhangsan", "dddd");
System.out.println(sm.get("zhangsan"));
}
}
class MyMap{
private Object key;
private Object value;
public Object getKey() {
return key;
}
public void setKey(Object key) {
this.key = key;
}
public Object getValue() {
return value;
}
public void setValue(Object value) {
this.value = value;
}
public MyMap(){
}
public MyMap(Object key,Object value){
this.key = key;
this.value = value;
}
}
相关文章
- 暂无相关文章
用户点评