Collection:是单列集合的顶层接口
|—(可重复)List
|—(实现类)ArrayList ->底层数据结构是数组,查询快,增删慢线程不安全,效率高
|—(实现类)LinkedList ->底层数据结构是链表,查询慢,增删快线程不安全,效率高
|—(不能重复)Set
|—(实现类)HashSet ->迭代无序
|—(实现类)TreeSet ->迭代按照某种顺序
Map:是双列集合的顶层接口
|—(实现类)HashMap
|—(实现类)TreeMap
队列和栈
//队列
Queue<String> queue = new LinkedList<String>();
//栈
Deque<Integer> stack = new ArrayDeque<Integer>();
/*迭代器*/
Iterator it = array.iterator();
while(it.hasNext()) {
String s = it.next();
System.out.printfln(s);
}
/*增强for*/
for(String s : array){
System.out.printfln(s);
}
ArrayList
//添加
add(E e);
add(int index,E e);
addAll(Collection c);
//清除
clear();
//是否包含
contains(Object 0);
//获取
get(int index);
//判空
isEmpty();
//删除
remove(Object o);
//修改指定位置
set(int index, E element);
//转成数组
toArray(T[] a);
//返回大小
size();
LinkedList
//添加
addFirst(E e);
addLast(E e);
//是否包含
contains(Object 0);
//获取
get(int index);
getFirst();
getLast();
//返回第一次索引
indexOf(Object o);
//删除
remove(int index);
removeFirst();
removeLast();
HashSet
add(E e);
contains(Object o);
isEmpty();
remove(Object o);
size();
TreeSet
add(E e);
contains(Object o);
isEmpty();
remove(Object o);
size();
Queue队列
Queue<String> queue = new LinkedList<String>();
queue.offer("a");
queue.element();//检索但不删除
queue.poll()//检索并删除
queue.clear();//清除
Deque栈
Deque<Integer> stack = new ArrayDeque<Integer>();
stack.push(1);
stack.peek();
stack.pop();
queue.clear();
HashMap
get(Object key);
isEmpty();
keySet();//返回此地图中包含的键的Set视图
put(K key, V value);//添加元素
remove(Object key);
TreeMap
//略
demo01
ackage demo01;
import java.util.*;
/*
* List的子类特点
* ArrayList:
* 底层数据结构是数组,查询快,增删慢
* 线程不安全,效率高
* Vector:
* 底层数据结构是数组,查询快,增删慢
* 线程安全,效率低
* LinkedList:
* 底层数据结构是链表,查询慢,增删快
* 线程不安全,效率高
*
* ===========================
*
* 案例:
* 使用List的任何子类储存自定义对象并遍历
*
* ArrayList的使用
* 存储字符串并遍历
*
*/
public class ArrayListDemo1 {
public static void main(String[] args) {
//创建集合对象
ArrayList array = new ArrayList();
//添加元素
array.add("hello1");
array.add("hello2");
array.add("hello3");
//遍历(迭代器)
Iterator it = array.iterator();
while(it.hasNext()) {
String s = (String)it.next();
System.out.println(s);
}
System.out.println("-----------");
//遍历(for循环)
for(int i=0;i<array.size();i ) {
String s = (String)array.get(i);
System.out.println(s);
}
}
}
package demo01;
import java.util.*;
/*
* ArrayList存储自定义对象并遍历
*/
public class ArrayListDemo2 {
public static void main(String[] args) {
//创建集合对象
ArrayList array = new ArrayList();
//创建学生对象
Student s1 = new Student("AAA",3);
Student s2 = new Student("BBB",4);
Student s3 = new Student("CCC",5);
//添加元素
array.add(s1);
array.add(s2);
array.add(s3);
//遍历(迭代器)
Iterator it = array.iterator();
while(it.hasNext()) {
Student s = (Student)it.next();
System.out.println(s.getName() "--" s.getAge());
}
System.out.println("-------------");
//遍历(for)
for(int i=0;i<array.size();i ) {
Student s = (Student)array.get(i);
System.out.println(s.getName() "--" s.getAge());
}
}
}
package demo01;
public class Student {
private String name;
private int age;
public Student() {
super();
}
public Student(String name,int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
demo02
package demo02;
import java.util.*;
/*
* Vector的特有功能
* 1.添加功能
* public void addElement(Object obj)
* 2.获取功能
* public Object elementAt(int index)
* public Enumeration element()
*/
public class VectorDemo1 {
public static void main(String[] args) {
//创建集合对象
Vector v = new Vector();
//添加功能
v.addElement("hello");
v.addElement("AAA");
v.addElement("Java");
//遍历
for(int i=0;i<v.size();i ) {
String s = (String)v.get(i);
System.out.println(s);
}
System.out.println("-------------");
Enumeration en = v.elements();
while(en.hasMoreElements()) {
String s = (String)en.nextElement();
System.out.println(s);
}
}
}
demo03
package demo03;
import java.util.*;
/*
* LinkedList的特有功能
* 1.添加功能
* addFirst(Object e)
* addLast(Object e)
* 2.获取功能
* getFirst()
* getLast()
* 3.删除功能
* removeFirst()
* removeLast()
*/
public class LinkedListDemo {
public static void main(String[] args) {
//创建集合对象
LinkedList link = new LinkedList();
//添加元素
link.add("hello");
link.add("AAA");
link.add("Java");
link.addFirst("head");
link.addLast("end");
//输出对象
System.out.println(link);
System.out.println(link.getFirst());
System.out.println(link.removeLast());
}
}
demo04
package demo04;
import java.util.ArrayList;
import java.util.Iterator;
/*
* ArrayList去除集合字符串的重复值(字符串内容相同)
* 分析
* 1.创建集合对象
* 2.添加多个字符串
* 3.创建新集合
* 4.遍历旧集合,获取每一个元素
* 5.拿着元素去新集合找
*/
public class ArrayListDemo1 {
public static void main(String[] args) {
ArrayList array = new ArrayList();
array.add("aaa");
array.add("aba");
array.add("baa");
array.add("aaa");
array.add("abbaa");
ArrayList newArray = new ArrayList();
Iterator it = array.iterator();
while(it.hasNext()) {
String s = (String)it.next();
if(!newArray.contains(s)) {
newArray.add(s);
}
}
for(int i=0;i<newArray.size();i ) {
String s = (String)newArray.get(i);
System.out.println(s);
}
}
}
demo01
package demo02;
import java.util.HashSet;
import java.util.Set;
public class HashSetDemo2 {
public static void main(String[] args) {
Set<Student> hs = new HashSet<>();
Student s1 = new Student("AA",8);
Student s2 = new Student("BB",8);
Student s3 = new Student("CC",8);
Student s4 = new Student("AA",8);
hs.add(s1);
hs.add(s2);
hs.add(s3);
hs.add(s4);
for(Student s : hs) {
System.out.println(s.getName() "--" s.getAge());
}
}
}
demo02
package demo03;
import java.util.LinkedHashSet;
//迭代顺序有序(哈希表和链表)
public class LinkdeHashSetDemo {
public static void main(String[] args) {
LinkedHashSet<String> hs = new LinkedHashSet<>();
hs.add("AAA");
hs.add("BBB");
hs.add("CCC");
hs.add("DDD");
hs.add("BBB");
hs.add("CCC");
hs.remove("AAA");
for(String s : hs) {
System.out.println(s);
}
}
}
demo03
package demo04;
import java.util.TreeSet;
/*
* TreeSet 能对元素进行排序
* 按照某种规则
* A:自然排序
* B:比较器
*/
public class TreeSetDemo {
public static void main(String[] args) {
TreeSet<Integer> ts = new TreeSet<Integer>();
ts.add(12);
ts.add(15);
ts.add(14);
ts.add(12);
ts.add(12);
ts.remove(12);
//自然顺序
for(Integer i : ts) {
System.out.println(i);
}
}
}
demo01
package demo01;
import java.util.HashMap;
import java.util.Map;
public class HashMapDemo {
public static void main(String[] args) {
Map<String,String> map = new HashMap<String,String>();
map.put("AAA", "aaa");
map.put("AAA", "bbb");
map.put("CCC", "ccc");
map.put("EEE", "eee");
System.out.println(map);
System.out.println(map.remove("AAA"));
//是否包含
System.out.println(map.containsKey("BBB"));
//判空
System.out.println(map.isEmpty());
System.out.println(map.size());
}
}
package demo01;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
/*
* Map集合的遍历
* Map--夫妻对
* 思路:
* A:获取所有结婚的集合
* B:遍历结婚证的集合,得到每一个结婚证
* C:根据结婚证获取丈夫和妻子
*
* 转换:
* A:获取所有键值对对象的集合
* B:遍历键值对对象的集合,得到每一个对象
* C:获取键和值
*
* 键值对对象如何表示
* entrySet();返回键值对对象集合
* Set> set = map.entrySet();
*/
public class MapDemo2 {
public static void main(String[] args) {
Map<String,String> map = new HashMap<String,String>();
map.put("AAA", "aaa");
map.put("AAA", "bbb");
map.put("CCC", "ccc");
map.put("EEE", "eee");
Set<Map.Entry<String,String>> set = map.entrySet();
for(Map.Entry<String,String> me : set) {
String key = me.getKey();
String value = me.getValue();
System.out.println(key "--" value);
}
}
}
demo02
package demo02;
import java.util.LinkedHashMap;
import java.util.Set;
/*
* LinkedHashMap具有可预知的迭代顺序
*/
public class LinkedHashMapDemo {
public static void main(String[] args) {
LinkedHashMap<String,String> hm = new LinkedHashMap<String,String>();
hm.put("111", "AAA");
hm.put("111", "AAB");
hm.put("222", "BBB");
hm.put("333", "CCC");
Set<String> set = hm.keySet();
for(String key : set) {
String value = hm.get(key);
System.out.println(key "--" value);
}
}
}
package demo03;
import java.util.Comparator;
import java.util.Set;
import java.util.TreeMap;
public class TreeMapDemo2 {
public static void main(String[] args) {
TreeMap<Student,String> tm = new TreeMap<Student,String>(
new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
int num = o1.getAge()-o2.getAge();//主要条件
int num2 = num == 0 ? o1.getName().compareTo(o2.getName()) : num;
return num2;
}
});
Student s1 = new Student("AA",1);
Student s2 = new Student("BB",2);
Student s3 = new Student("CC",3);
Student s4 = new Student("AA",1);
tm.put(s1, "!");
tm.put(s2, "!!");
tm.put(s3, "!!!");
tm.put(s4, "!!!!");
Set<Student> set = tm.keySet();
for(Student key : set) {
String value = tm.get(key);
System.out.println(key.getName() " " key.getAge() " " value);
}
}
}
demo03
package demo04;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeMap;
/*
* Hashtable 同步的 !!!
*
* "aasdsadasfgfgg" 统计字符串中每一个字母出现的次数
* 要求返回a(5)b(4)c(6)...
*
* 分析:
* 1.定义一个字符串(可改键盘录入)
* 2.定义一个TreeMap集合
* key:Character
* vlaue:Integer
* 3.把字符串转为字符数组
* 4.遍历字符数组,得到每一个字符
* 5.拿字符作为键到集合中找
* null:不存在,加入
* 存在:值加一
* 6.定义字符串缓冲区
* 7.遍历集合,按要求拼接
*
*/
public class TreeMapDemo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入字符串");
String line = sc.next();
TreeMap<Character,Integer> tm = new TreeMap<Character,Integer>();
char[] chs = line.toCharArray();
for(char ch : chs) {
Integer i = tm.get(ch);
if(i==null) {
tm.put(ch, 1);
}else {
i ;
tm.put(ch, i);
}
}
//定义字符串缓冲区变量
StringBuilder sb = new StringBuilder();
Set<Character> set = tm.keySet();
for(Character key : set) {
Integer value = tm.get(key);
sb.append(key).append("(").append(value).append(")");
}
String res = sb.toString();
System.out.println(res);
}
}
package demo01;
import java.util.*;
/*
* Collections是针对集合进行操作的工具类
* 都是静态方法
*
* 面试题:
* Collection:是单列集合的顶层接口,有子接口List和Set
* Collections:是针对集合操作的工具类,有对集合进行排序和二分查找的方法
*
* 要知道的方法:
* public static void sort(List list):自然排序
* public static int binarySearch(List> list,T key):二分查找
* public static void reverse(List> list):反转
* public static void shuffle(List> list):随机置换
*/
public class CollectionsDemo {
public static void main(String[] args) {
List<Integer> list = new ArrayList<Integer>();
list.add(30);
list.add(3);
list.add(3233);
list.add(2);
list.add(123);
System.out.println(list);
//public static void sort(List list):自然排序
Collections.sort(list);
System.out.println(list);
Integer x = Collections.binarySearch(list, 3);
System.out.println(x);//返回索引,找不到最大索引加一取负数
//最大值
System.out.println(Collections.max(list));
//反转
Collections.reverse(list);
System.out.println(list);
//随机置换
Collections.shuffle(list);
System.out.println(list);
}
}
demo02
package demo02;
import java.util.*;
import demo03.Student;
public class CollectionsDemo {
public static void main(String[] args) {
List<Student> list = new ArrayList<>();
Student s1 = new Student("AA",1);
Student s2 = new Student("BB",2);
Student s3 = new Student("CC",3);
Student s4 = new Student("DD",4);
list.add(s1);
list.add(s2);
list.add(s3);
list.add(s4);
//Collections.sort(list);
//需要实现接口
for(Student s : list) {
System.out.println(s.getName() "--" s.getAge());
}
}
}