集合
是对象的容器,实现了对对像的常用操作,类似于数组的功能。
数组长度固定,集合长度部固定。
数组可以存储基本类型和引用类型,集合只能存储引用类型。
Collection体系集合
collection是体系的根接口,代表一组对象被称为集合。
collection下面有两个接口分别是List接口和Set接口。
List接口有序,有下标,元素可以重复。
Set接口无序,无下标,元素不能重复。
List接口下有ArrayList,LinkedList和Vector三个实现类。
Set接口有HashSet这个实现类和SortedSet接口,以及SortedSet接口下的TreeSet实现类。
collection中的方法:
add();添加一个对象。
addAll();将一个集合中的所有对象添加到此集合中
clear();清空集合中所有对象。
contains(o);检查是否包含o对象。
equals();比较此集合是否与指定对象相等。
isEmpty();判断此集合是否为空。
remove(o);移除o对象
size();获取元素个数。
toArray();将集合转换成数组。
public class Demo01 {
public static void main(String[] args) {
//创建集合
Collection collection=new ArrayList();
//(1)添加元素
collection.add("苹果");
collection.add("香蕉");
collection.add("橙子");
System.out.println(collection.size());
System.out.println(collection);
System.out.println("--------------------------------------");
//(2)删除元素
collection.remove("香蕉");
System.out.println(collection.size());
System.out.println(collection);
System.out.println("--------------------------------------");
//(3)遍历元素
//3.1增强for循环
for (Object object:
collection) {
System.out.println(object);
}
System.out.println("--------------------------------------");
//3.2用迭代器遍历
Iterator it=collection.iterator();
while (it.hasNext()){
String s=(String)it.next();
System.out.println(s);
}
System.out.println(collection.contains("西瓜"));
}
}
List子接口
方法:
add(index,o): 在指定位置添加一个对象o。
addAll(index,c);在指定位置将集合c中的元素添加到此集合厚葬。
get();返回指定位置元素。
subList(a,b);返回a到b之间的集合元素。
public class ListDemo01 {
public static void main(String[] args) {
//创建集合对象
List list=new ArrayList();
//添加元素
list.add("猪");
list.add("马");
list.add("牛");
list.add("羊");
list.add(0,"狼");
System.out.println(list.size());
System.out.println(list);
System.out.println("--------------------------------------");
//删除元素
list.remove("牛");
System.out.println(list.size());
System.out.println(list);
System.out.println("--------------------------------------");
//遍历元素
//增强for循环
for (Object o:
list) {
System.out.println(o);
}
System.out.println("--------------------------------------");
//迭代器
Iterator it=list.iterator();
while (it.hasNext()){
String s=(String)it.next();
System.out.println(s);
}
//列表迭代器 可以进行前后遍历和增删改等操作
ListIterator lit=list.listIterator();
//前遍历
System.out.println("--------------------------------------");
while (lit.hasNext()){
;
System.out.println(lit.nextIndex()+""+lit.next());
}
//后遍历
System.out.println("--------------------------------------");
while (lit.hasPrevious()){
;
System.out.println(lit.previousIndex()+":"+lit.previous());
}
//判断
System.out.println(list.contains("羊"));
System.out.println(list.isEmpty());
//获取位置
System.out.println("--------------------------------------");
System.out.println(list.indexOf("狼"));
}
}
ArrayList
Arraylist 是数组结构 ,查询快,增删慢。
默认容量是10
LinkedList
linkedList是双向链表结构,查询慢,增删快。
泛型
本质是参数化类型,把类型作为参数传递。
常见形势有泛型类,泛型接口,泛型方法。
泛型只能是引用类型,不同泛型对象不能相互赋值
不能用泛型创建静态常量
Set接口
全部方法继承自Collection中的方法。
Set<String> set = new HashSet<String>();
//添加数据
set.add("css");
set.add("cs");
set.add("cf");
set.add("lyf");
set.add("lyf");
System.out.println(set.size());
System.out.println(set.toString());
//删除数据
set.remove("css");
System.out.println(set.toString());
//set.clear();
//遍历
//foreach
for (String s:
set) {
System.out.println(s);
}
//迭代器
Iterator<String> iterator= set.iterator();
while(iterator.hasNext()){
System.out.println(iterator.next());
}
HashSet集合
存储结构为hash表
基于HashCode计算元素存放位置,若存入元素hash码相同,会调用equals进行确认,若为True,则拒绝后者存入
TreeSet
基于排列顺序实现元素部重复
实现了SortedSet接口,对集合元素自动排序。
元素对象类型必须实现Comparable接口,指定排序。
通过ComparableTo方法来确定元素是否重复。
当ComparableTo方法返回值为0,则认为是重复元素。
package Set;
import java.util.Iterator;
import java.util.TreeSet;
/*
* TreeSet
* 要求实现Comparable接口
* 存储结构:红黑树
* */
public class Demo02 {
public static void main(String[] args) {
//创建集合
TreeSet<String> treeSet=new TreeSet<String>();
//添加元素
treeSet.add("cs");
treeSet.add("cf");
treeSet.add("lyf");
treeSet.add("cs");
System.out.println(treeSet.size());
System.out.println(treeSet.toString());
//删除元素
treeSet.remove("cf");
//treeSet.clear();
//遍历元素
//foreach
for (String s:
treeSet) {
System.out.println(s);
}
//迭代器
Iterator it=treeSet.iterator();
while (it.hasNext()){
System.out.println(it.next());
}
System.out.println("--------------------------------------------------------");
TreeSet<Person> treeSet2=new TreeSet<>();
Person p1=new Person("cs",24);
Person p2=new Person("cf",45);
Person p3=new Person("lyf",45);
Person p4=new Person("css",16);
treeSet2.add(p1);
treeSet2.add(p2);
treeSet2.add(p3);
treeSet2.add(p4);
System.out.println(treeSet2.size());
System.out.println(treeSet2.toString());
System.out.println("--------------------------------------------------------");
//删除元素
treeSet2.remove(p4);
System.out.println(treeSet2.size());
System.out.println(treeSet2.toString());
System.out.println("--------------------------------------------------------");
//遍历元素
//foreach
for (Person p:
treeSet2) {
System.out.println(p);
}
//迭代器
Iterator it2=treeSet2.iterator();
while(it2.hasNext()){
System.out.println(it2.next());
}
//判断
treeSet2.contains(p2);
}
}
Person类
package Set;
import java.util.Objects;
public class Person implements Comparable<Person>{
private String name;
private int age;
public Person(String name, int age) {
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;
}
public Person() {
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Person person = (Person) o;
return age == person.age && Objects.equals(name, person.name);
}
@Override
public int hashCode() {
return Objects.hash(name, age);
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
@Override
public int compareTo(Person o) {
int n1=this.getName().compareTo(o.getName());
int n2=this.getAge()-o.getAge();
return (n1==0)?n2:n1;
}
}
有一个Comparator接口可用于在创建TreeSet集合时声明比较规则。
package Set;
import java.util.Comparator;
import java.util.TreeSet;
/*
* Comparator:实现定制比较(比较器)
* Comparable(可比较的)
* */
public class Demo03 {
public static void main(String[] args) {
//利用匿名内部类声明比较规则,即可不连接Comparable接口
TreeSet<Person> personTreeSet=new TreeSet<Person>(new Comparator<Person>() {
@Override
public int compare(Person o1, Person o2) {
int n1=o1.getAge()-o2.getAge();
int n2=o1.getName().compareTo(o2.getName());
return n1==0?n2:n1;
}
});
}
}
Map接口
用于存储任意键值对。
键 无序 无下标 无重复
值 无序 无下标 可重复
方法
put()将对象存入集合中,关联键值。键重复会覆盖原值。
get()跟局键获取对应值。
KeySet()获取所有键。
values()返回包含所有值得Collection集合。
EntrySet()获取所有值。
package Map;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class Demo01 {
public static void main(String[] args) {
//创建Map集合
Map<String,String> map=new HashMap<>();
//添加元素
map.put("cn","中国");
map.put("usa","美国");
map.put("uk","英国");
map.put("cn","zhongguo");
System.out.println(map.size());
System.out.println(map.toString());
//删除
map.remove("usa");
System.out.println(map.size());
//遍历
//用keySet获取键得Set集合,再用Map中的get()方法和foreach循环遍历
Set<String> keySet= map.keySet();
for (String s:
keySet) {
System.out.println(s+map.get(s));
}
//使用EntrySet()方法遍历,
Set<Map.Entry<String,String>> entrySet= map.entrySet();
for (Map.Entry<String,String> entry:
entrySet) {
System.out.println(entry.getKey()+entry.getValue());
}
}
}
HashMap
刚创建时,table为null,为节省空间,当添加一个元素后,他变了容量调为16.
当元素个数大于阈值(16*0.75=12)时,会扩容,扩容为原来的两倍。
jdk1.8当链表长度大于8,且元素总数大于64,会调整为红黑树,提高效率。
当链表长度小于6,调整为链表。
jdk1.8以前,链表头插入,之后尾插入。
TreeMap
元素对象类型必须实现Comparable接口,指定排序。
通过ComparableTo方法来确定元素是否重复。
当ComparableTo方法返回值为0,则认为是重复元素。
浙公网安备 33010602011771号