Set(集)
List(列表)
Map(映射)
List特点:元素有放入顺序,元素可重复
Map特点:元素按键值对存储,无放入顺序
Set特点:元素无放入顺序,元素不可重复(注意:元素虽然无放入顺序,但是元素在set中的位置是有该元素的HashCode决定的,其位置其实是固定的)
List接口有三个实现类:LinkedList,ArrayList,Vector
LinkedList:底层基于链表实现,链表内存是散乱的,每一个元素存储本身内存地址的同时还存储下一个元素的地址。链表增删快,查找慢
ArrayList和Vector的区别:ArrayList是非线程安全的,效率高;Vector是基于线程安全的,效率低
Set接口有两个实现类:HashSet(底层由HashMap实现),LinkedHashSet
SortedSet接口有一个实现类:TreeSet(底层由平衡二叉树实现)
Query接口有一个实现类:LinkList
Map接口有三个实现类:HashMap,HashTable,LinkeHashMap
HashMap非线程安全,高效,支持null;HashTable线程安全,低效,不支持null
SortedMap有一个实现类:TreeMap
其实最主要的是,list是用来处理序列的,而set是用来处理集的。Map是知道的,存储的是键值对
set 一般无序不重复.map kv 结构 list 有序
Set:
package cn.hncu.search.set;
public class MySet {
private Object[] objs = new Object[0];
public boolean add(Object obj) {
// 卫条件---相同的对象只能放一份,因此有重复的对象则放不进去
if (contains(obj)) {
return false;
}
// 经过上面的卫条件,说明当前对象在集合中不存在,可以加
// 1先创建一个新的数组,长度为原来的加1
Object tmpObjs[] = new Object[objs.length + 1];
// 2把原来数组当中的元素拷到新的当中
System.arraycopy(objs, 0, tmpObjs, 0, objs.length);
// 3把将要加入集合的对象obj放在新数组中的最后一个位置
tmpObjs[objs.length] = obj;
// 4把新数组赋给objs
objs = tmpObjs;
return true;
}
public Object[] getAll() {
return objs;
}
public boolean contains(Object obj) {
for (Object tm : objs) {
if (tm.equals(obj)) {
return true;
}
}
return false;
}
public int size() {
return objs.length;
}
}
import org.junit.Test;
public class TestMySet {
@Test //测试添加基本数据类型
public void test1(){
MySet set = new MySet();
set.add(1);
set.add("hello");
set.add("+");
set.add(1);//测试重复
Object objs[] = set.getAll();
for(Object obj:objs){
System.out.println(obj);
}
}
@Test //测试添加自定义类型
public void test2(){
MySet set = new MySet();
Person p = new Person("Jack",20);
Person p2 = new Person("Jack",20);//测试重复
Person p3 = new Person("Rose",22);
set.add(p);
set.add(p2);
set.add(p3);
set.add( new A(1,23.5));
set.add( new A(2,53.5));
set.add( new A(100,123.5));
set.add( new A(100,123.5));//测试重复
set.add( new A(8,98));
Object objs[] = set.getAll();
for(Object obj:objs){
System.out.println(obj);
}
}
}
class Person{
private String name;
private int age;
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Person other = (Person) obj;
if (age != other.age)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
@Override
public String toString() {
return name + "," + age;
}
}
class A{
private int x;
private double y;
public A(int x, double y) {
super();
this.x = x;
this.y = y;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + x;
long temp;
temp = Double.doubleToLongBits(y);
result = prime * result + (int) (temp ^ (temp >>> 32));
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
A other = (A) obj;
if (x != other.x)
return false;
if (Double.doubleToLongBits(y) != Double.doubleToLongBits(other.y))
return false;
return true;
}
@Override
public String toString() {
return "[" + x + "," + y + "]";
}
}
List:
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
//演示集合的普通操作
public class ListDemo {
//添加到List中的元素的顺序与它的HashCode无关,是按照添加的先后顺序存放
public static void main(String[] args) {
List list = new ArrayList();
list.add(11);
list.add("abc");
list.add(45.6);
list.add(11);//List允许添加重复的元素
list.add(new Person("Jack",22));
list.add(new Person("Jack",22));//List允许添加重复的元素
list.add(new Person("Tom",21));
//查(遍历)--法1
Iterator it = list.iterator();
while(it.hasNext()){
Object obj = it.next();
System.out.println(obj);
}
System.out.println("-----------");
//查(遍历)--法2 ----利用与位置相关的方法来实现
for(int i=0; i<list.size(); i++){
System.out.println( list.get(i) );
}
}
}
Map:
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
/**
* 开发的一个Map容器
*@author<a href="mailto:729627398@qq.com">廖枝平</a>
*@version 1.0 2017-1-6 上午8:55:04
*@fileName MyMap.java
*/
public class MyMap {
private Map map = new HashMap();
/**
* @param key
* 新增加元素的键
* @param value
* 新增加元素的值
* @return
* 返回值是原来容器中该key所对应的value值,如果原来不存在则返回null
*/
public Object put(Object key,Object value){
return map.put(key, value);
}
//删
public Object remove(Object key){
return map.remove(key);
}
//改
public boolean update(Object key,Object value){
if(map.get(key)==null){
return false;
}else{
map.put(key, value);
return true;
}
}
//查(单、全、条件)
public Object get(Object key){
return map.get(key);
}
public Map getAll(){
return map;
}
public Map getByCondition(String strValue){
Map m = new HashMap();
Set<Entry> entries = map.entrySet();
for(Entry en:entries){
Object value = en.getValue();
if(value.toString().contains(strValue)){
Object key=en.getKey();
m.put(key, value);
}
}
return m;
}
}