集合框架
集合概念:对象容器,实现了对对象常用的操作类似数组功能
和数组的区别:
(1)数组长度固定。集合长度不固定
(2)数组可以存储基本类型和引用类型,集合只能存储引用类型【装箱改变】
Collection体系集合
collection的使用
package Collection;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class Demo01 {
public static void main(String[] args) {
Collection collection=new ArrayList();
collection.add("苹果");
collection.add("西瓜");
collection.add("香蕉");
System.out.println("元素个数:"+collection.size());
System.out.println(collection);
System.out.println("===================================");
System.out.println("使用增强for遍历");
for (Object o : collection) {
System.out.println(o);
}
System.out.println("使用迭代器遍历");
Iterator it=collection.iterator();
while (it.hasNext()){
String next = (String)it.next();
System.out.println(next);
}
System.out.println(collection.contains("西瓜"));
System.out.println(collection.isEmpty());
}
}
package Collection;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class Demo02 {
public static void main(String[] args) {
Collection collection=new ArrayList();
Student s1=new Student("张三",19);
Student s2=new Student("张四",19);
Student s3=new Student("张五",19);
Student s4=new Student("张六",19);
collection.add(s1);
collection.add(s2);
collection.add(s3);
collection.add(s4);
System.out.println("元素个数:"+collection.size());
System.out.println(collection);
for (Object o : collection) {
System.out.println(o);
}
System.out.println("++++++++++++++++++++++++++++++++++");
Iterator iterator=collection.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
}
}
package Collection;
public class Student {
String name;
int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public Student() {
}
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;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
List子接口
特点:有序,有下标,元素可以重复
LinkedList
package Collection;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
public class Demo03 {
public static void main(String[] args) {
List list=new ArrayList<>();
list.add("苹果");
list.add("西瓜");
list.add(0,"香蕉");
System.out.println("元素个数:"+list.size());
System.out.println(list);
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
Iterator iterator = list.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
ListIterator listIterator = list.listIterator();
System.out.println("使用列表迭代器正向");
while (listIterator.hasNext()){
System.out.println(listIterator.nextIndex()+":"+listIterator.next());
}
System.out.println("逆向");
while (listIterator.hasPrevious()){
System.out.println(listIterator.previousIndex()+":"+listIterator.previous());
}
System.out.println(list.indexOf("苹果"));
}
}
package Collection;
import java.util.ArrayList;
import java.util.List;
public class Demo04 {
public static void main(String[] args) {
List list=new ArrayList();
list.add(10);
list.add(20);
list.add(30);
list.add(40);
list.add(50);
System.out.println("元素个数:"+list.size());
System.out.println(list);
list.remove(new Integer(20));
System.out.println("元素个数:"+list.size());
List list1 = list.subList(1, 3);
System.out.println(list1);
}
}
ArrayList数组结构实现,查询快,增删慢
源码分析:
默认容量DEFAULT_CAPACITY = 10
注意:如果没有向集合中添加元素容量为0
EMPTY_ELEMENTDATA 存放元素数组
size 实际元素个数
add 添加元素
public boolean add(E e) {
modCount++;
add(e, elementData, size);
return true;
}
Vector:
数组结构实现,查询快,增删慢
运行效率慢,线程安全
package Collection;
import java.util.Enumeration;
import java.util.Vector;
public class Demo06 {
public static void main(String[] args) {
Vector vector=new Vector();
vector.add("草莓");
vector.add("芒果");
vector.add("西瓜");
System.out.println("元素的个数:"+vector.size());
Enumeration elements = vector.elements();
while (elements.hasMoreElements()){
Object o = elements.nextElement();
System.out.println(o);
}
}
}
LinkedList
链表结构,增删快,查询慢
package Collection.LinkedList;
import Collection.Student;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.ListIterator;
public class Demo01 {
public static void main(String[] args) {
LinkedList<Object> objects = new LinkedList<>();
Student s1=new Student("刘德华",20);
Student s2=new Student("郭富城",20);
Student s3=new Student("梁朝伟",20);
objects.add(s1);
objects.add(s2);
objects.add(s3);
System.out.println("元素个数:"+objects.size());
System.out.println(objects.toString());
System.out.println("---------for--------------");
for (int i = 0; i <objects.size() ; i++) {
System.out.println(objects.get(i));
}
System.out.println("-------增强for--------------");
for (Object object : objects) {
System.out.println(object);
}
System.out.println("-------使用迭代器--------------");
Iterator<Object> iterator = objects.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
System.out.println("-------使用逆向迭代器--------------");
ListIterator<Object> objectListIterator = objects.listIterator();
while(objectListIterator.hasNext()){
System.out.println(objectListIterator.next());
}
}
}
ArrayList与LinkedList的区别
ArrayList:必须开辟连续空间,查询快,增删慢
LinkedList:无需开辟连续空间,查询慢,增删快
泛型
java泛型是JDK1.5中引入的一个性特性,其本质是参数化类型,把类型作为参数传递
常见形式有泛型类,泛型接口,泛型方法
语法:
<T……>T称为类型占位符,表示一种引用类型
好处:
1.提高代码的重用性
2.防止类型转化异常,提高代码安全性
泛型类
package 泛型;
public class Demo01<T> {
T t;
public void show(T t){
System.out.println(t);
}
public T getT(){
return t;
}
}
package 泛型;
public class TestDemo01 {
public static void main(String[] args) {
Demo01<String> demo01=new Demo01<>();
demo01.t="hello";
demo01.show("大家好");
System.out.println(demo01.getT());
Demo01<Integer> demo011=new Demo01<Integer>();
demo011.t=6;
demo011.show(9);
System.out.println(demo011.getT());
}
}
泛型接口
package 泛型;
public interface Demo02<T> {
String name="张三";
T server(T t);
}
package 泛型;
public class TestDemo02 implements Demo02<String>{
@Override
public String server(String s) {
System.out.println(s);
return s;
}
}
package 泛型;
public class TestDemo02_1<T> implements Demo02<T>{
@Override
public T server(T t) {
System.out.println(t);
return null;
}
}
package 泛型;
public class TestDemo01 {
public static void main(String[] args) {
Demo01<String> demo01=new Demo01<>();
demo01.t="hello";
demo01.show("大家好");
System.out.println(demo01.getT());
Demo01<Integer> demo011=new Demo01<Integer>();
demo011.t=6;
demo011.show(9);
System.out.println(demo011.getT());
TestDemo02 testDemo02 = new TestDemo02();
testDemo02.server("你好");
TestDemo02_1 testDemo02_1=new TestDemo02_1<Integer>();
testDemo02_1.server(1);
}
}
泛型方法
package 泛型;
public class Demo03 {
public <T> T show(T t){
System.out.println("泛型方法"+t);
return t;
}
}
package 泛型;
public class TestDemo03 {
public static void main(String[] args) {
Demo03 demo03 = new Demo03();
demo03.show("你好");
demo03.show(3.14);
demo03.show(8);
}
}
set接口
无序,无下标,元素不可重复
package set;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
public class Demo01 {
public static void main(String[] args) {
Set<String> set=new HashSet<>();
set.add("小米");
set.add("华为");
set.add("苹果");
System.out.println("数据个数:"+set.size());
System.out.println(set.toString());
System.out.println("------增强for--------");
for (String s : set) {
System.out.println(s);
}
System.out.println("--------迭代器--------");
Iterator<String> iterator = set.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
}
}
hash
package set;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
public class Demo02 {
public static void main(String[] args) {
HashSet<String> hashMap=new HashSet<String>();
hashMap.add("刘德华");
hashMap.add("周润发");
hashMap.add("梁朝伟");
hashMap.add("周星驰");
System.out.println("个数"+hashMap.size());
System.out.println(hashMap.toString());
System.out.println("------增强for----------");
for (String s : hashMap) {
System.out.println(s);
}
System.out.println("--------迭代器----------");
Iterator<String> iterator = hashMap.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
System.out.println(hashMap.contains("郭富城"));
}
}
package set;
public class person {
String name;
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;
}
@Override
public String toString() {
return "person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
@Override
public int hashCode() {
int n1=this.name.hashCode();
int n2=this.age;
return n1+n2;
}
@Override
public boolean equals(Object obj) {
if(this==obj){
return true;
}
if (obj==null){
return false;
}
if (obj instanceof person){
person p=(person) obj;
if (this.name.equals(p.getName())&&this.age==p.getAge()){
return true;
}
}
return false;
}
}
package set;
import java.util.HashSet;
public class Demo03 {
public static void main(String[] args) {
HashSet<person> hashSet=new HashSet<>();
person p1=new person("李德华",18);
person p2=new person("王德华",18);
person p3=new person("张德华",18);
hashSet.add(p1);
hashSet.add(p2);
hashSet.add(p3);
System.out.println("个数"+hashSet.size());
System.out.println(hashSet.toString());
}
}
TreeSet
基于排序顺序实现元素不重复
实现了SortedSet接口,对集合元素自动排序
元素对象的类型必须实现Comparable接口,指定排序规则
红黑树
package set;
import java.util.TreeSet;
public class Demo04 {
public static void main(String[] args) {
TreeSet<String> treeMap=new TreeSet<String>();
treeMap.add("xyz");
treeMap.add("abc");
treeMap.add("hello");
System.out.println("个数:"+treeMap.size());
System.out.println(treeMap.toString());
}
}
package set;
public class person implements Comparable<person>{
String name;
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;
}
@Override
public String toString() {
return "person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
@Override
public int hashCode() {
int n1=this.name.hashCode();
int n2=this.age;
return n1+n2;
}
@Override
public boolean equals(Object obj) {
if(this==obj){
return true;
}
if (obj==null){
return false;
}
if (obj instanceof person){
person p=(person) obj;
if (this.name.equals(p.getName())&&this.age==p.getAge()){
return true;
}
}
return false;
}
@Override
public int compareTo(person o) {
int n1=this.getName().compareTo(o.getName());
int n2=this.getAge()-o.getAge();
return n1==0?n2:n1;
}
}
package set;
import java.util.TreeSet;
public class Demo05 {
public static void main(String[] args) {
TreeSet<person> people=new TreeSet<>();
person p1=new person("李德华",18);
person p2=new person("王德华",18);
person p3=new person("张德华",18);
people.add(p1);
people.add(p2);
people.add(p3);
System.out.println(people.size());
System.out.println(people.toString());
}
}
package set;
import java.util.Comparator;
import java.util.TreeSet;
public class Demo06 {
public static void main(String[] args) {
TreeSet<person> treeSet=new TreeSet<>(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;
}
});
person p1=new person("李德华",18);
person p2=new person("王德华",18);
person p3=new person("张德华",18);
treeSet.add(p1);
treeSet.add(p2);
treeSet.add(p3);
System.out.println(treeSet.size());
}
}
Map集合
package Map;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class Demo01 {
public static void main(String[] args) {
Map<String,String> map=new HashMap<>();
map.put("cn","中国");
map.put("uk","英国");
map.put("usa","美国");
System.out.println("个数:"+map.size());
System.out.println(map.toString());
System.out.println("--------使用Keyset-----------");
Set<String> strings = map.keySet();
for (String string : strings) {
System.out.println(string+"-----------"+map.get(string));
}
System.out.println("------使用entryset------");
Set<Map.Entry<String, String>> entries = map.entrySet();
for (Map.Entry<String, String> entry : entries) {
System.out.println(entry.getKey()+"----"+entry.getValue());
}
}
}
|