增:add
删:remove
改:set
查:get
List接口
/**
* collection:ArrayList,LinkedList,vector
* ArrayList:集合,一个一个的有序的,可重复的容器
* 方法:add(),addAll(),clear(),isEmpty()
*/
@Test
public void test(){
ArrayList<Object> arrayList = new ArrayList<>();
System.out.println("输出" + arrayList);//输出[]
//boolean add(E e)
//将指定的元素追加到此列表的末尾。
arrayList.add(123);
arrayList.add(34);
arrayList.add("qwe");
//int size()
//返回此列表中的元素数。
System.out.println("输出" + arrayList.size());//输出3
System.out.println("输出" + arrayList);//输出[123, 34, qwe]
System.out.println("********************");
ArrayList<Object> arrayList1 = new ArrayList<>();
arrayList1.add(566);
//boolean addAll(Collection<? extends E> c)
//将指定集合中的所有元素按指定集合的Iterator返回的顺序附加到此列表的末尾。
arrayList.addAll(arrayList1);
System.out.println("输出" + arrayList.size());//输出4
System.out.println("输出" + arrayList1);//输出[566]
System.out.println("输出" + arrayList);//输出[123, 34, qwe, 566]
System.out.println("*****************");
//boolean isEmpty()
//如果此列表不包含任何元素,则返回 true 。
System.out.println(arrayList.isEmpty());//false
//void clear()
//从此列表中删除所有元素。
arrayList.clear();
System.out.println("输出" + arrayList);//输出[]
System.out.println(arrayList.isEmpty());//true
}
public class Test911 {
/**
* collection接口,toArray(),hashCode(),asList()
*/
@Test
public void test(){
Collection collection = new ArrayList<>();
collection.add("qwe");
collection.add(123);
collection.add("收到");
for (int i = 0;i < collection.toArray().length;i++) {
//Object[] toArray()
//以适当的顺序(从第一个元素到最后一个元素)返回包含此列表中所有元素的数组。
System.out.print(collection.toArray()[i] +"\t");//qwe 123 收到
}
System.out.println();
//int hashCode()
//返回此集合的哈希码值。
System.out.println(collection.hashCode());//108857917
//static <T> List<T> asList(T... a)
//返回由指定数组支持的固定大小的列表。
List<String> strings = Arrays.asList(new String[]{"123","asd"});
System.out.println(strings);//[123, asd]
}
}
arrayList.add(123);
arrayList.add(456);
arrayList.add("ad");
System.out.print(arrayList +"\t");//[123, 456, ad]
System.out.println();
//int indexOf(Object o)
//返回此列表中第一次出现的指定元素的索引,如果此列表不包含该元素,则返回-1。
System.out.println(arrayList.indexOf(456));//1
System.out.println(arrayList.indexOf(123));//0
//void add(int index, E element)
//将指定元素插入此列表中的指定位置。
arrayList.add(0, "qwe");
System.out.print(arrayList);//[qwe, 123, 456, ad]
//remove(int position)
//从此滚动列表中删除指定位置的项目。
arrayList.remove(0);
System.out.println(arrayList);//[123, 456, ad]
//boolean remove(Object o)
//从该列表中删除指定元素的第一个匹配项(如果存在)(可选操作)。
arrayList.remove(new Integer(123));
System.out.println(arrayList);//[456, ad]
Set接口
/**
* set:无序,不可重复的
* HashSet:不安全的,主要实现类,可以存null,数组+链表
* LinkedHashSet:作为HashSet的子类出现,遍历时可以按照添加的顺序遍历
* TreeSet:可以按照一定的属性排序
*
*/
@Test
public void test4(){
//方法与ArrayList差不多
Set set = new HashSet<>();
set.add(123);
set.add("sdf");
set.add(333);
//无序不等于随机,输出结果虽然无序,但是不会再改变
//数据存的时候随机占一个数组位置,占完以后就已经定了
System.out.println(set);//[sdf, 123, 333]
set.add(123);
//不可重复,123的哈希值已经选择了一个位置,就不会再出现第二个123了,但地址值不一样,可以存
//如果一个哈希值有其他元素,则比较两个哈希值,
// 1:哈希值不同,则替换添加 2:哈希值相同,equlas,相同则不添加,不相同则替换添加
//添加不是替换,用链表的方式连到同一个地址,可以连很多
System.out.println(set);//[sdf, 123, 333]
}