Collection是单列集合的顶层接口,Map是双列结合的顶层的接口,都挺重要的,掌握,不过有点多,需多练。附带一张图片
面向对象对事物的体现都是以对象的形式,为了方便对多个对象的操作,就对对象进行存储。
package collection;
/*
* 集合中顶层接口Collection中的抽象的方法
*
* 演示接口中,没有All的方法
*
* Collection 接口 = 自己的实现类对象();
* 实现类对象调用方法,运行的方法都是实现类重写
*/
import java.util.*;
public class CollectionDemo {
public static void main(String[] args) {
method();
}
/*
* 返回集合中存储元素的个数
* 集合的长度
* int size()
* 三种长度表示形式: 数组的length,字符串length() 集合的size()
*/
public static void method_5()
{
Collection coll=new ArrayList();
coll.add("abc");
coll.add("abc2");
coll.add("abc3");
coll.add("abc4");
System.out.println(coll);//[abc, abc2, abc3, abc4]
//调用集合大方法size(),获取长度
int size=coll.size();
System.out.println(size);//4
}
/*
* 判断集合中是不是有元素(集合是否为空)
* boolean isEmpty()
* 如果集合中么有元素,返回true
* */
public static void method_4()
{
Collection coll=new ArrayList();
boolean b=coll.isEmpty();
System.out.println(b);//true
}
/*判断集合中是否包含指定元素
* boolean contains(Object obj)
* 如果指定的对象存在集合中返回true
* */
public static void method_3()
{
Collection coll=new ArrayList();
coll.add("abc1");
coll.add("abc2");
coll.add("abc3");
coll.add("abc4");
System.out.println(coll);
//集合调用方法contains,判断元素在不在集合中
boolean b=coll.contains("abc2");
System.out.println(b);//true
}
/*清空集合中的所有元素
* void clear()
* */
public static void method_2()
{
//创建Collection子类对象ArrayList
Collection coll=new ArrayList();
//向集合中添加元素
coll.add("abc");
coll.add("abd");
coll.add("abe");
coll.add("abf");
//调用集合方法clean()清空集合
coll.clear();
System.out.println(coll);//[]表示已清空
}
/*
* 移除集合中的元素
* boolean remove(Object obj)
* 传递一个对象,集合会移除已有对象,移除成功返回true
* */
public static void method_1()
{
Collection coll=new ArrayList();
coll.add("abc");
coll.add("acc");
coll.add("adc");
coll.add("aec");
System.out.println(coll);//[abc, acc, adc, aec]
//调用集合中的方法remove(),移除指定的元素
boolean b=coll.remove("adc");
System.out.println(b);//true
System.out.println(coll);//[abc, acc, aec]
}
/*
* 将元素添加到集合
* 将对象存储到集合
* boolean add(Object obj)什么对象都可以存储
* */
public static void method()
{
Collection coll=new ArrayList();
coll.add("abc1");
coll.add(123);
coll.add(false);
//提示:输出集合,看到了集合中存储对象
//但是,只能称为观看,不能为获取
System.out.println(coll);//[abc1, 123, false]
}
}
---------------------------------------------------------------------------------------
package collection;
/*
* Collection接口中带有All结尾方法
*/
import java.util.*;
public class CollectionDemo1 {
public static void main(String[] args) {
method();
}
/*
* 获取两个集合的交集
* boolean retainAll(Collection c)
* 返回值boolean类型:ture和false
* 看原始集合,调用者,里面的元素是不是变化啦,如果变化了就是true
* */
public static void method_3()
{
Collection co1=new ArrayList();
Collection co2=new ArrayList();
co1.add(1231);
co1.add(1232);
co1.add(1233);
co2.add(1231);
co2.add(1231);
co2.add(1233);
co2.add(4);
co2.add(67);
//集合co11调用方法retainAll()传递co2
//将两个集合co1和co2中的相同元素,保存到co1中
boolean b=co1.retainAll(co2);
System.out.println(b);//true
System.out.println(co1);//[1231, 1233]
}
/*
* 移除两个集合相同的元素
* boolean removeall(Collection c)
* */
public static void method_2()
{
Collection col1=new ArrayList();
Collection col2=new ArrayList();
col1.add(1231);
col1.add(1232);
col1.add(1233);
col2.add(1232);
col2.add(1230);
col2.add(1231);
//集合col1调用方法removeAll传递col2
//移除的是col1,移除的是col1和col2中相同的元素
boolean b=col1.removeAll(col2);
System.out.println(b);//true
System.out.println(col1);//[1233]
System.out.println(col2);//[1232, 1230, 1231]
}
/*
* 判断一个集合中的元素,是否完全包含另一个集合中的元素
* boolean containsAll(Collection c)
*/
public static void method_1()
{
Collection col1=new ArrayList();
Collection col2=new ArrayList();
col1.add(1231);
col1.add(1232);
col1.add(1233);
col2.add(1231);
col2.add(1233);
//集合clo1调用方法containsAll,传递col2
//判断col2中的所有元素,是不是包含在col1中
boolean b=col1.containsAll(col2);
System.out.println(b);//true
}
/*
* 将一个集合添加到另一个集合中
* boolean addAll(Collection c)
* 方法,传递的是所有Collection类型的对象
* 传递Collection接口的实现类对象
* 向集合中添加一批对象
*/
public static void method()
{
Collection col1=new ArrayList();
Collection col2=new ArrayList();
col1.add("abc1");
col1.add("abc2");
col2.add("abc3");
col2.add(123);
col2.add(456);
col2.add(789);
//使用集合col1调用方法addAll传递col2
//将col2中的元素,添加到col1中
boolean b=col1.addAll(col2);
System.out.println(b);//true
System.out.println(col1);//[abc1, abc2, abc3, 123, 456, 789]
//小细节 add(Object obj)
//col1.add(col2);
//System.out.println(col1);[abc1, abc2, abc3, 123, 456, 789]
}
}
--------------------------------------------------------------------------------------
package collection;
/*
* 将集合转成数组
* 遍历数组->间接遍历集合
*
* 数据类型,集合中什么类型都存
* 转成后的数组呢,存储是什么类型, Object
*
* Collection接口定义方法
* Object[] toArray()
*/
import java.util.*;
public class CollectionDemo2 {
public static void main(String[] args) {
Collection coll=new ArrayList();
coll.add("abc");
coll.add("你");
coll.add(false);
coll.add(new Date());
//将集合转成数组,集合调用方法toArray()
Object[] obj=coll.toArray();
//循环遍历数组
for(int x=0;x<obj.length;x++)
System.out.println(obj[x]);
/*
* abc
你
false
//当前系统时间
Mon Oct 26 14:31:34 CST 2015
* */
}
}
-------------------------------------------
package list;
import java.util.ArrayList;
import java.util.Iterator;
/*2015-10-25 13:28:34
* 集合嵌套存储
* 1个集合中,存储多个集合
* ArrayList中,存储了多个ArrayList
* 外层的ArrayList,和内ArrayList
* 大个集合,小个集合
* 小集合存储字符串
*/
public class ListTest3 {
public static void main(String[] args) {
//创建ArrayList集合,存储的是其他ArrayList集合
//小集合,存储的是字符串对象
ArrayList<ArrayList<String>> al=new ArrayList<ArrayList<String>>();
//创建小的ArrayList集合
ArrayList<String> a1=new ArrayList<String>();
ArrayList<String> a2=new ArrayList<String>();
//两个小集合,存储字符串
a1.add("君威");
a1.add("宝马");
a2.add("卡宴");
a2.add("路虎");
//小集合,存储到大集合中
al.add(a1);
al.add(a2);
//迭代器,迭代大集合,迭代出小集合
Iterator<ArrayList<String>> it=al.iterator();
while(it.hasNext())
{
//迭代器it.next()获取出来的是什么,是ArrayList集合,集合存储的是字符串
//使用集合对象接收next方法返回值
ArrayList<String> a=it.next();
//迭代小集合
Iterator<String> ita=a.iterator();
while(ita.hasNext())
{
//迭代器ita.next方法,获取出来的就是小集合中存储的字符串
System.out.println(ita.next());
/* 君威
宝马
卡宴
路虎*/
}
}
System.out.println("========for循环形式=================");
for(ArrayList<String> a:al)
{
for(String s : a)
{
System.out.println(s);
}
}
}
}
------------------------------------------------------------------------------
package linkedlist;
/*
* LinkedList集合自身特性
* 底层数据结构,链表结果 采用对象之间的地址记录
* 查询慢,增删快
* 线程不安全,运行速度快
*
* 特殊的功能,操作集合的开头和结尾
*/
import java.util.LinkedList;
public class LinkedListDemo {
public static void main(String[] args) {
method();
}
/*
* LinkedList在JDK1.6的后,增加一组新的方法
* 替换原有的方法
* boolean offerFirst(E e)
在此列表的开头插入指定的元素。
* offerFirst -> addFirst
* boolean offerLast(E e)
在此列表末尾插入指定的元素。
* offerLast -> addLast
* E peekFirst()
获取但不移除此列表的第一个元素;如果此列表为空,则返回 null。
* peekFirst -> getFirst
* boolean offerLast(E e)
在此列表末尾插入指定的元素。
* peekLast -> getLast
* E pollFirst()
获取并移除此列表的第一个元素;如果此列表为空,则返回 null。
* pollFirst -> removeFirst
* E pollLast()
获取并移除此列表的最后一个元素;如果此列表为空,则返回 null。
* pollLast -> removeLast
*
* 获取功能和移除功能,在链表没有元素的时候,返回null不会出现异常
*/
public static void method_3()
{
LinkedList link=new LinkedList();
link.add(1);
link.add(2);
link.add(3);
link.add(4);
System.out.println(link);//[1, 2, 3, 4]
System.out.println(link.getFirst());//1获取到开头第一个元素1
Object o = link.peekFirst();
System.out.println(o);//1
System.out.println(link);
System.out.println(o);
o = link.pollFirst();
System.out.println(o);
System.out.println(link);
}
/*
* LinkedList特有功能
* E removeFirst()
移除并返回此列表的第一个元素。
* Object removeFirst() 获取并移除链表的开头元素
* E removeLast()
移除并返回此列表的最后一个元素。
* Object removeLast() 获取并移除链表的结尾元素
*/
public static void method_2()
{
LinkedList link=new LinkedList();
link.addFirst(1);
link.addFirst(2);
link.addFirst(3);
link.addFirst(4);
System.out.println(link);//[4, 3, 2, 1]
System.out.println("----------");
Object obj=link.removeFirst();
System.out.println(obj);//移除的元素
System.out.println(link);//[3, 2, 1]
obj=link.removeLast();
System.out.println(obj);//1
System.out.println(link);//[3, 2]
}
/*
* LinkedList特有功能
* E getFirst()
返回此列表的第一个元素。
* Object getFirst()获取链表开头元素
* E getLast()
返回此列表的最后一个元素。
* Object getLast() 获取链表的结尾元素
* 如果链表中,没有元素了,还要获取,抛出NoSuchElementException
*/
public static void method_1()
{
LinkedList link=new LinkedList();
link.addFirst(1);
link.addFirst(2);
link.addFirst(3);
link.addFirst(4);//现金后出
System.out.println(link);
Object obj=link.getFirst();
System.out.println(obj);//4获取集合中第一个元素
System.out.println(link);//[4, 3, 2, 1]
obj=link.getLast();
System.out.println(obj);//1获取集合中最后一个元素
System.out.println(link);//[4, 3, 2, 1]
}
/*
* LinkedList特有功能
* void addFirst(E e)
将指定元素插入此列表的开头。
* addFirst(Object o)将元素添加到链表的开头
* void addLast(E e)
将指定元素添加到此列表的结尾。
* addLast(Object o) 将元素添加到链表的结尾
*/
public static void method()
{
LinkedList link=new LinkedList();
link.add(1);
link.add(2);
link.add(3);
link.add(4);
link.addFirst("ABC");
System.out.println(link);//[ABC, 1, 2, 3, 4]
link.addLast("xx");
System.out.println(link);//[ABC, 1, 2, 3, 4, xx]
}
}