您的位置:首页 > 其它

集合第八发练习之容器版V2.0

2016-03-26 12:52 274 查看


一、Clooection

package cn.hncu.MySet3;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

import cn.hncu.collectionDemo.collectionDemo;

public class MyCollection {
private Collection col=new ArrayList();
public boolean add(Object obj){
col.add(obj);
return true;
}
public boolean update(Object oldObj,Object newObj){
Object objs[]=col.toArray();
col.clear();
for(int i=0;i<objs.length;i++){
if(objs[i].equals(oldObj)){
objs[i]=newObj;
}
col.add(objs[i]);
}
return true;
}
public boolean delete(Object obj){
return col.remove(obj);
}
public Collection getAll(){
return col;
}
public Collection getByConditon(String str){
Collection result=new ArrayList();
Iterator it=col.iterator();
while(it.hasNext()){
if(it.next().toString().indexOf(str)>=0){//模糊查询
result.add(it);
}
}
return result;
}
}


二、Map

package cn.hncu.MySet3;

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

public class MyMap {
private Map map=new HashMap();
public  boolean add(Object key,Object value) {
map.put(key, value);
return true;
}
public Object delete(Object key){
return map.remove(key);
}
public Map select(String key){
Map result=new HashMap();
Set set=map.entrySet();
Iterator it=set.iterator();
while(it.hasNext()){
Entry entry=(Entry) it.next();
if(entry.getKey().toString().indexOf(key)>=0){
result.put(entry.getKey(), entry.getValue());
}
}
return result;
}
public Map getAll(){
return map;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: