JAVA ArraySet<E> SET形式的有序LIST
Set形式的数组,数组内容重复
package com.sicdt.library.core.utils; import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; import java.util.Map; import java.util.Set; /** * * <br>类 名: ArraySet * <br>描 述: 自定义Set * <br>作 者: shizhenwei * <br>创 建: 2017年5月15日 * <br>版 本: v0.0.2 * <br> * <br>历 史: (版本) 作者 时间 注释 */ public class ArraySet<E> implements Set<E> { private ArrayList<E> list; public ArraySet() { list = new ArrayList<>(); } public ArraySet(int initialCapacity){ list = new ArrayList<>(initialCapacity); } public ArraySet(Collection<? extends E> c) { list = new ArrayList<>(c); } @Override public int size() { return list.size(); } @Override public boolean isEmpty() { return list.isEmpty(); } @Override public boolean contains(Object o) { return list.contains(o); } @Override public Iterator<E> iterator() { return list.iterator(); } @Override public Object[] toArray() { return list.toArray(); } @Override public <T> T[] toArray(T[] a) { return list.toArray(a); } @Override public boolean add(E e) { if(list.contains(e)){ return false; } return list.add(e); } @Override public boolean remove(Object o) { return list.remove(o); } @Override public boolean containsAll(Collection<?> c) { return list.containsAll(c); } @Override public boolean addAll(Collection<? extends E> c) { if(c == null || c.isEmpty()) return false; for(E e: c){ add(e); } return true; } @Override public boolean retainAll(Collection<?> c) { return list.retainAll(c); } @Override public boolean removeAll(Collection<?> c) { return list.removeAll(c); } @Override public void clear() { list.clear(); } @Override public String toString() { return list == null ? "[]" : list.toString(); } public static void main(String[] args) { Set<String> set = new ArraySet<>(); set.add("aa"); set.add("cc"); set.add("bb"); set.add("cc"); System.out.println(set); Map<String, String> map = new ArrayMap<>(); map.put("a", "aa"); map.put("b", "bb"); map.put("c", "cc"); map.put("a", "dd"); System.out.println(map); System.out.println(map.keySet()); Iterator<String> it = map.keySet().iterator(); while(it.hasNext()){ String key = it.next(); String value = map.get(key); System.out.println(key + "," + value); } } }