集合框架
集合的概念
概念:对象的容器,实现对对象常用的操作,类似数组功能
和数组的区别:
- 数组长度固定,集合长度不固定
- 数组可以存储基本类型和引用类型,集合只能存储引用类型
Collection接口
Collection父接口
特点:代表一组任意类型的对象,无序.无下标.不能重复
方法:
- boolean add(Object obj) //添加一个对象
- boolean addAll(Collection c) //将一个集合中的所有对象添加到此集合中
- void clear() //清空此集合中的所有对象
- boolean contains(Object o) //检查此集合中是否包含o对象
- boolean equals(Object o) //比较此集合是否与指定对象相等
- boolean isEmpty() //判断此集合是否为空
- boolean remove(Object o) //在此集合中移除o对象
- int size() //返回此集合中的元素个数
- Object[] toArray() //将此集合转换为数组
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
/**
* Collection接口的使用
* 1.添加元素
* 2.删除元素
* 3.遍历元素
* 4.判断
*/
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);
//删除元素
collection.remove("西瓜");
//collection.clear();
System.out.println(collection);
//遍历元素
//1.使用增强for
for (Object o : collection) {
System.out.println(o);
}
//2.使用迭代器Iterator(迭代器是专门来遍历集合的一种方式)
//hasNext();有没有下一个元素
//next();获取下一个元素
//remove();删除当前元素
Iterator it = collection.iterator();
while (it.hasNext()){
String s = (String)it.next();
System.out.println(s);
//迭代过程不能使用collection的删除方法
//collection.remove();
it.remove();
}
System.out.println("元素个数:"+collection.size());
//判断
System.out.println(collection.contains("西瓜"));
}
}
public class Student {
String name;
int age;
public Student(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 "Student [name=" + name + ",age=" + age +"]";
}
}
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
/**
* Collection的使用:保存学生信息
*/
public class Demo02 {
public static void main(String[] args) {
//创建Collection对象
Collection collection = new ArrayList();
Student s1 = new Student("gy1",21);
Student s2 = new Student("gy2",22);
Student s3 = new Student("gy3",23);
//添加数据
collection.add(s1);
collection.add(s2);
collection.add(s3);
System.out.println("元素个数:"+collection.size());
System.out.println(collection.toString());
//删除数据
collection.remove(s1);
//collection.clear();
System.out.println("删除之后元素个数:"+collection.size());
//遍历
//1.使用增强for
for (Object o : collection) {
Student s= (Student)o;
System.out.println(s.toString());
}
//2.使用迭代器Iterator(迭代器是专门来遍历集合的一种方式)
Iterator it = collection.iterator();
while (it.hasNext()){
Student s = (Student) it.next();
System.out.println(s.toString());
}
//判断
System.out.println(collection.contains(s1));
}
}
List接口与实现类
List子接口
特点:有序.有下标.元素可以重复
方法:
- void ad(int index,Object o) //在index位置插入对象o
- boolean addAll(int index,Collection c) //将一个集合中的元素添加到此集合中的index位置
- Object get(int index) //返回集合中指定位置的元素
- List subList(int fromIndex,int toIndex) //返回fromIndex和toIndex之间的集合元素
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
/**
* List接口的使用
*/
public class Demo01 {
public static void main(String[] args) {
//创建集合对象
List list = new ArrayList<>();
//添加元素
list.add("苹果");
list.add("西瓜");
list.add("橘子");
System.out.println("元素个数:"+list.size());
System.out.println(list.toString());
//删除元素
list.remove("苹果");
//list.remove(1); //下标
System.out.println("删除之后元素个数:"+list.size());
System.out.println(list.toString());
//遍历
//1.使用for遍历
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
//2.使用增强for
for (Object object : list){
System.out.println(object);
}
//3.使用迭代器
Iterator iterator = list.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
//4.使用列表迭代器
//和Iterator的区别:可以向前或向后遍历,添加.删除.修改元素
ListIterator listIterator = list.listIterator();
while (listIterator.hasNext()){
System.out.println(listIterator.nextIndex()+":"+listIterator.next());
}
//从后向前遍历
while (listIterator.hasPrevious()){
System.out.println(listIterator.previousIndex()+":"+listIterator.previous());
}
//判断
System.out.println(list.contains("西瓜"));
//获取位置
System.out.println(list.indexOf("橘子"));
}
}
import java.util.ArrayList;
import java.util.List;
/**
* List的使用
*/
public class Demo02 {
public static void main(String[] args) {
//创建集合
List list = new ArrayList();
//添加数字元素(自动装箱)
list.add(1);
list.add(2);
list.add(3);
System.out.println("元素个数:"+list.size());
System.out.println(list.toString());
//删除元素
list.remove(new Integer(1));
System.out.println("删除之后元素个数:"+list.size());
System.out.println(list.toString());
//补充方法
//subList:返回子集合(含头不含尾)
List subList = list.subList(0,1);
System.out.println(subList.toString());
}
}
List实现类
- ArrayList(重点):
- 数组结构实现,查询快,增删慢
- JDK1.2版本,运行效率快,线程不安全
- Vector:
- 数组结构实现,查询快,增删慢
- JDK1.0版本,运行效率慢,线程安全
- LinkedList:
- 链表结构实现,增删快,查询慢
public class Student {
String name;
int age;
public Student(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 "Student [name=" + name + ",age=" + age +"]";
}
@Override
public boolean equals(Object obj) {
//判断是不是同一个对象
if (this == obj){
return true;
}
//判断是否为空
if (obj == null){
return false;
}
//判断是否为Student类型
if (obj instanceof Student){
Student s = (Student)obj;
//比较属性
if (this.name.equals(s.getName())&&this.age==s.getAge()){
return true;
}
}
//不满足条件返回
return false;
}
}
import java.util.ArrayList;
import java.util.Iterator;
import java.util.ListIterator;
/**
* ArrayList的使用
* 存储结构:数组,查找遍历速度快,增删慢
*/
public class Demo01 {
public static void main(String[] args) {
//创建集合
ArrayList arrayList = new ArrayList<>();
//添加元素
Student s1 = new Student("gy111", 21);
Student s2 = new Student("gy222", 22);
Student s3 = new Student("gy333", 23);
arrayList.add(s1);
arrayList.add(s2);
arrayList.add(s3);
System.out.println("元素个数:"+arrayList.size());
System.out.println(arrayList.toString());
//删除元素
//arrayList.remove(s1);
arrayList.remove(new Student("gy111", 21));
System.out.println("删除之后元素个数:"+arrayList.size());
System.out.println(arrayList.toString());
//遍历
//1.使用迭代器
Iterator iterator = arrayList.iterator();
while (iterator.hasNext()){
Student s = (Student)iterator.next();
System.out.println(s.toString());
}
//2.使用列表迭代器
ListIterator listIterator = arrayList.listIterator();
while (listIterator.hasNext()){
Student s = (Student)listIterator.next();
System.out.println(s.toString());
}
while (listIterator.hasPrevious()){
Student s = (Student)listIterator.previous();
System.out.println(s.toString());
}
//判断
System.out.println(arrayList.contains(new Student("gy222", 22)));
System.out.println(arrayList.isEmpty());
}
}
import java.util.Enumeration;
import java.util.Vector;
/**
* Vector的使用
*/
public class Demo01 {
public static void main(String[] args) {
//创建集合
Vector vector = new Vector();
//添加元素
vector.add("草莓");
vector.add("荔枝");
vector.add("葡萄");
System.out.println(vector.toString());
//删除元素
vector.remove("荔枝");
System.out.println(vector.toString());
//遍历
//使用枚举器
Enumeration elements = vector.elements();
while (elements.hasMoreElements()){
String s = (String)elements.nextElement();
System.out.println(s);
}
//判断
System.out.println(vector.contains("草莓"));
System.out.println(vector.isEmpty());
//其他方法
//firstElement.lastElement.elementAt();
}
}
import com.Collection.demo04.Student;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.ListIterator;
/**
* LinkedList的使用
* 存储结构:双向链表
*/
public class Demo01 {
public static void main(String[] args) {
//创建集合
LinkedList linkedList = new LinkedList();
//添加元素
Student s1 = new Student("gy111", 21);
Student s2 = new Student("gy222", 22);
Student s3 = new Student("gy333", 23);
linkedList.add(s1);
linkedList.add(s2);
linkedList.add(s3);
System.out.println("元素个数:"+linkedList.size());
System.out.println(linkedList.toString());
//删除元素
linkedList.remove(s1);
//linkedList.remove(new Student("gy222", 22));
//linkedList.clear();
System.out.println("删除之后元素个数:"+linkedList.size());
//遍历
//1.for遍历
for (int i = 0; i < linkedList.size(); i++) {
System.out.println(linkedList.get(i));
}
//2.增强for
for (Object object : linkedList){
Student s = (Student)object;
System.out.println(s.toString());
}
//3.使用迭代器
Iterator iterator = linkedList.iterator();
while (iterator.hasNext()){
Student s = (Student)iterator.next();
System.out.println(s.toString());
}
//4.使用列表迭代器
ListIterator listIterator = linkedList.listIterator();
while (listIterator.hasNext()) {
Student s = (Student) listIterator.next();
System.out.println(s.toString());
}
//判断
System.out.println(linkedList.contains(s1));
System.out.println(linkedList.isEmpty());
//获取
System.out.println(linkedList.indexOf(s2));
}
}
泛型和工具类
本质是参数化类型,把类型作为参数传递
语法:
<T...>T称为类型占位符,表示一种引用类型
好处:
- 提高代码的重用性
- 防止类型转换异常,提高代码的安全性
泛型类
/**
* 泛型类
* 语法:类名<T>
*/
public class Generic<T> {
//使用泛型T
//1.创建变量
T t;
//2.泛型作为方法的参数
public void show(T t){
System.out.println(t);
}
//3.泛型作为方法的返回值
public T getT(){
return t;
}
}
public class TestGeneric {
public static void main(String[] args) {
//使用泛型类创建对象
//注意:
//1.泛型只能使用引用类型
//2.不同泛型类型对象之间不能相互赋值
Generic<String> generic = new Generic<String>();
generic.t = "hello";
generic.show("大家好");
String string = generic.getT();
Generic<Integer> generic1 = new Generic<>();
generic1.t = 100;
generic1.show(200);
Integer integer = generic1.getT();
}
}
泛型接口
/**
* 泛型接口
* 语法:接口名<T>
*/
public interface MyInterface<T> {
String name = "gy";
T server(T t);
}
public class MyInterfaceImpl implements MyInterface<String>{
@Override
public String server(String t) {
System.out.println(t);
return t;
}
}
public class TestGeneric {
public static void main(String[] args) {
//使用泛型类创建对象
//注意:
//1.泛型只能使用引用类型
//2.不同泛型类型对象之间不能相互赋值
Generic<String> generic = new Generic<String>();
generic.t = "hello";
generic.show("大家好");
String string = generic.getT();
Generic<Integer> generic1 = new Generic<>();
generic1.t = 100;
generic1.show(200);
Integer integer = generic1.getT();
MyInterfaceImpl myInterfaceImpl = new MyInterfaceImpl();
myInterfaceImpl.server("gy123");
}
}
/**
* 泛型接口
* 语法:接口名<T>
*/
public interface MyInterface<T> {
String name = "gy";
T server(T t);
}
public class MyInterfaceImpl2<T> implements MyInterface<T>{
@Override
public T server(T t) {
System.out.println(t);
return t;
}
}
public class TestGeneric {
public static void main(String[] args) {
MyInterfaceImpl2<Integer> myInterfaceImpl1 = new MyInterfaceImpl2<>();
myInterfaceImpl1.server(100);
}
}
泛型方法
/**
* 泛型方法
* 语法:<T> 返回值类型
*/
public class GenericMethod {
//泛型方法
public <T> T show(T t){
System.out.println("泛型方法");
return t;
}
}
public class TestGeneric {
public static void main(String[] args) {
GenericMethod genericMethod = new GenericMethod();
genericMethod.show("加油");
genericMethod.show(200);
}
}
泛型集合
概念:参数化类型.类型安全的集合,强制集合元素的类型必须一致
特点:
- 编译时即可检查,而非运行时抛出异常
- 访问时,不必类型转换(拆箱)
- 不同泛型之间引用不能相互赋值,泛型不存在多态
import com.Collection.demo04.Student;
import java.util.ArrayList;
import java.util.Iterator;
public class GenericCollection {
public static void main(String[] args) {
ArrayList<String> arrayList = new ArrayList<String>();
arrayList.add("ggg");
arrayList.add("yyy");
for (String string : arrayList) {
System.out.println(string);
}
ArrayList<Student> arrayList2 = new ArrayList<Student>();
//添加元素
Student s1 = new Student("gy111", 21);
Student s2 = new Student("gy222", 22);
Student s3 = new Student("gy333", 23);
arrayList2.add(s1);
arrayList2.add(s2);
arrayList2.add(s3);
Iterator<Student> iterator = arrayList2.iterator();
while (iterator.hasNext()){
Student s = iterator.next();
System.out.println(s.toString());
}
}
}
Set接口与实现类
Set子接口
特点:无序.无下标.元素不可重复
方法:全部继承Collection中的方法
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
/**
* Set接口的使用
*/
public class Demo01 {
public static void main(String[] args) {
//创建集合
Set<String> set = new HashSet<>();
//添加数据
set.add("火龙果");
set.add("哈密瓜");
set.add("猕猴桃");
set.add("猕猴桃");
System.out.println("元素个数:"+set.size());
System.out.println(set.toString());
//删除数据
set.remove("哈密瓜");
System.out.println("删除之后元素个数:"+set.size());
System.out.println(set.toString());
//遍历
//1.使用增强for
for (String string : set){
System.out.println(string);
}
//2.使用迭代器
Iterator<String> iterator = set.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
//判断
System.out.println(set.contains("猕猴桃"));
System.out.println(set.isEmpty());
}
}
Set实现类
- HashSet(重点):
- 基于HashCode实现元素不重复
- 当存入元素的哈希码相同时,会调用equals进行确认,如结果为true,则拒绝后者存入
import java.util.HashSet;
import java.util.Iterator;
/**
* HashSet集合的使用
* 存储结构:哈希表(数组+链表+红黑树)
*/
public class Demo02 {
public static void main(String[] args) {
//创建集合
HashSet<String> hashSet = new HashSet<>();
//添加元素
hashSet.add("火龙果");
hashSet.add("哈密瓜");
hashSet.add("猕猴桃");
System.out.println("元素个数:"+hashSet.size());
System.out.println(hashSet.toString());
//删除元素
hashSet.remove("火龙果");
System.out.println("删除之后元素个数:"+hashSet.size());
System.out.println(hashSet.toString());
//遍历
//1.增强for
for (String string : hashSet){
System.out.println(string);
}
//2.使用迭代器
Iterator<String> iterator = hashSet.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
//判断
System.out.println(hashSet.contains("猕猴桃"));
System.out.println(hashSet.isEmpty());
}
}
import java.util.HashSet;
import java.util.Iterator;
/**
* HashSet的使用
* 存储结构:哈希表(数组+链表+红黑树)
* 存储过程:
* 1.根据hashcode计算保存的位置,如果此位置为空,则直接保存,如果不为空,则执行第二步
* 2.再执行equals方法,如果equals方法为true,则认为是重复,否则,形成链表
*/
public class Demo03 {
public static void main(String[] args) {
//创建集合
HashSet<Person> person = new HashSet<>();
//添加数据
Person p1 = new Person("刘德华", 21);
Person p2 = new Person("郭富城", 22);
Person p3 = new Person("梁朝伟", 23);
person.add(p1);
person.add(p2);
person.add(p3);
person.add(new Person("梁朝伟", 23));
System.out.println("元素个数:"+person.size());
System.out.println(person.toString());
//删除数据
person.remove(p1);
//person.remove(new Person("刘德华", 21));
System.out.println("删除之后元素个数:"+person.size());
System.out.println(person.toString());
//遍历
//1.增强for
for (Person person1 : person){
System.out.println(person1.toString());
}
//2.使用迭代器
Iterator<Person> iterator = person.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
//判断
System.out.println(person.contains(p2));
System.out.println(person.isEmpty());
}
}
public class Person {
private String name;
private int age;
public Person() {
}
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 boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Person)) return false;
Person person = (Person) o;
if (age != person.age) return false;
return name != null ? name.equals(person.name) : person.name == null;
}
@Override
//1.31是一个质数,减少散列冲突
//2.31提高执行效率
public int hashCode() {
int result = name != null ? name.hashCode() : 0;
result = 31 * result + age;
return result;
}
// @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 false;
// }
// 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;
// }
}
-
TreeSet
-
基于排列顺序实现元素不重复
-
实现了SortedSet接口,对集合元素自动排序
-
元素对象的类型必须实现Comparable接口,指定排列规则
-
通过CompareTo方法确定是否为重复元素
-
import java.util.Iterator;
import java.util.TreeSet;
/**
* TreeSet的使用
* 存储结构:红黑树
*/
public class Demo04 {
public static void main(String[] args) {
//创建集合
TreeSet<String> treeSet = new TreeSet<>();
//添加元素
treeSet.add("火龙果");
treeSet.add("哈密瓜");
treeSet.add("猕猴桃");
System.out.println("元素个数:"+treeSet.size());
System.out.println(treeSet.toString());
//删除元素
treeSet.remove("火龙果");
System.out.println("删除之后元素个数:"+treeSet.size());
System.out.println(treeSet.toString());
//遍历
//1.增强for
for (String string : treeSet){
System.out.println(string);
}
//2.使用迭代器
Iterator<String> iterator = treeSet.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
//判断
System.out.println(treeSet.contains("猕猴桃"));
System.out.println(treeSet.isEmpty());
}
}
import java.util.Iterator;
import java.util.TreeSet;
/**
* 使用treeSet保存数据
* 存储结构:红黑树
* 要求:元素必须要实现Comparable接口,comparaTo()方法返回值为0,认为是重复元素
*/
public class Demo05 {
public static void main(String[] args) {
//创建集合
TreeSet<Person> person = new TreeSet<>();
//添加数据
Person p1 = new Person("张三", 21);
Person p2 = new Person("李四", 22);
Person p3 = new Person("王五", 23);
person.add(p1);
person.add(p2);
person.add(p3);
System.out.println("元素个数:"+person.size());
System.out.println(person.toString());
//删除数据
person.remove(p1);
System.out.println("删除之后元素个数:"+person.size());
System.out.println(person.toString());
//遍历
//1.增强for
for (Person person1 : person){
System.out.println(person1.toString());
}
//2.使用迭代器
Iterator<Person> iterator = person.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
//判断
System.out.println(person.contains(p2));
System.out.println(person.isEmpty());
}
}
public class Person implements Comparable<Person>{
private String name;
private int age;
public Person() {
}
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 boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Person)) return false;
Person person = (Person) o;
if (age != person.age) return false;
return name != null ? name.equals(person.name) : person.name == null;
}
@Override
public int hashCode() {
//1.31是一个质数,减少散列冲突
//2.31提高执行效率
int result = name != null ? name.hashCode() : 0;
result = 31 * result + age;
return result;
}
@Override
public int compareTo(Person o) {
int n1 = this.getName().compareTo(o.getName());
int n2 = this.age-o.getAge();
return n1 == 0 ? n2 : n1;
}
// @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 false;
// }
// 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;
// }
}
Comparator接口
import java.util.Comparator;
import java.util.TreeSet;
/**
* treeSet集合的使用
* Comparator:实现定制比较(比较器)
* Comparable:可比较的
*/
public class Demo06 {
public static void main(String[] args) {
//创建集合
TreeSet<Person> person = 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("张三", 21);
Person p2 = new Person("李四", 22);
Person p3 = new Person("王五", 23);
person.add(p1);
person.add(p2);
person.add(p3);
System.out.println("元素个数:"+person.size());
System.out.println(person.toString());
}
}
TreeSet案例
import java.util.Comparator;
import java.util.TreeSet;
/**
* 要求:使用TreeSet集合实现字符串按照长度进行排序
* Comparator接口实现定制比较
*/
public class Demo07 {
public static void main(String[] args) {
//创建集合,并制定比较规则
TreeSet<String> treeSet = new TreeSet<>(new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
int n1 = o1.length()-o2.length();
int n2 = o1.compareTo(o2);
return n1 == 0 ? n2 : n1;
}
});
//添加数据
treeSet.add("abc");
treeSet.add("abcd");
treeSet.add("abcde");
treeSet.add("abcdef");
treeSet.add("abcdefg");
System.out.println(treeSet.toString());
}
}
Map接口与实现类
Map接口
特点:
- 用于存储任意键值对(Key-Value)
- 键:无序.无下标.不允许重复(唯一)
- 值:无序.无下标.允许重复
方法:
- V put(K key,V value) //将对象存入到集合中,关键键值.key重复则覆盖原值
- Object get(Object key) //根据键获取对应的值
- Set
//返回所有的key - Collection
values() //返回包含所有值的Collection集合 - Set<Map.Entry<K,V>> //键值匹配的Set集合
import java.util.HashMap;
import java.util.Map;
/**
* Map接口的使用
* 特点:
* 1.存储键值对
* 2.键不能重复,值可以重复
* 3.无序
*/
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","英国");
System.out.println("元素个数:"+map.size());
System.out.println(map.toString());
//删除元素
map.remove("usa");
System.out.println("删除之后元素个数:"+map.size());
System.out.println(map.toString());
//遍历
//1.使用keySet();
//Set<String> keySet = map.keySet();
for (String key : map.keySet()){
System.out.println(key+"---"+map.get(key));
}
//2.使用entrySet()方法
//Set<Map.Entry<String, String>> entrySet = map.entrySet();
for (Map.Entry<String, String> entry : map.entrySet()){
System.out.println(entry.getKey()+"---"+entry.getValue());
}
//判断
System.out.println(map.containsKey("cn"));
}
}
Map实现类
- HashMap(重点):
- JDK1.2版本,线程不安全,运行效率快;允许用null作为key或是value
import java.util.HashMap;
import java.util.Map;
/**
* HashMap集合的使用
* 存储结构:哈希表(数组+链表+红黑树)
*/
public class Demo02 {
public static void main(String[] args) {
//创建集合
HashMap<Student,String> students = new HashMap<Student,String>();
//添加元素
Student s1 = new Student("张三", 101);
Student s2 = new Student("李四", 102);
Student s3 = new Student("王五", 103);
students.put(s1,"北京");
students.put(s2,"上海");
students.put(s3,"深圳");
students.put(new Student("张三", 101),"北京");
System.out.println("元素个数:"+students.size());
System.out.println(students.toString());
//删除元素
students.remove(s1);
System.out.println("删除之后元素个数:"+students.size());
System.out.println(students.toString());
//遍历
//1.使用keySet();
for (Student key : students.keySet()){
System.out.println(key.toString()+"==="+students.get(key));
}
//2.使用entrySet();
for (Map.Entry<Student,String> entry : students.entrySet()){
System.out.println(entry.getKey()+"==="+entry.getValue());
}
//判断
System.out.println(students.containsKey(s1));
System.out.println(students.containsValue("上海"));
}
}
- Hashtable:
- JDK1.0版本,线程安全,运行效率慢;不允许null作为key或是value
- Properties:
- Hashtable的子类,要求key和value都是String.通常用于配置文件的读取
- TreeMap:
- 实现了SortedMap接口(是Map的子接口),可以对key自动排序
import java.util.Map;
import java.util.TreeMap;
/**
* TreeMap的使用
* 存储结构:红黑树
*/
public class Demo03 {
public static void main(String[] args) {
//创建集合
TreeMap<Student,String> treeMap = new TreeMap<Student,String>();
//添加元素
Student s1 = new Student("张三", 101);
Student s2 = new Student("李四", 102);
Student s3 = new Student("王五", 103);
treeMap.put(s1,"北京");
treeMap.put(s2,"上海");
treeMap.put(s3,"深圳");
System.out.println("元素个数:"+treeMap.size());
System.out.println(treeMap.toString());
//删除元素
treeMap.remove(s1);
System.out.println("删除之后元素个数:"+treeMap.size());
System.out.println(treeMap.toString());
//遍历
//1.keySet();
for (Student keySet : treeMap.keySet()){
System.out.println(keySet.toString()+"==="+treeMap.get(keySet));
}
//2.entrySet();
for (Map.Entry<Student,String> entry : treeMap.entrySet()){
System.out.println(entry.getKey()+"==="+entry.getValue());
}
//判断
System.out.println(treeMap.containsKey(s2));
System.out.println(treeMap.containsValue("深圳"));
}
}
public class Student implements Comparable<Student>{
private String name;
private int stuNo;
public Student() {
}
public Student(String name, int stuNo) {
this.name = name;
this.stuNo = stuNo;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getStuNo() {
return stuNo;
}
public void setStuNo(int stuNo) {
this.stuNo = stuNo;
}
@Override
public String toString() {
return "Student [name=" + name + ",stuNo=" + stuNo +"]";
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Student)) return false;
Student student = (Student) o;
if (stuNo != student.stuNo) return false;
return name != null ? name.equals(student.name) : student.name == null;
}
@Override
public int hashCode() {
int result = name != null ? name.hashCode() : 0;
result = 31 * result + stuNo;
return result;
}
@Override
public int compareTo(Student o) {
int n2 = this.stuNo-o.stuNo;
return n2;
}
}
Collections工具类
概念:集合工具类,定义了除了存取之外的集合常用方法
方法:
- public static void reverse(List<?> list) //反转集合中元素的顺序
- public static void shuffle(List<?> list) //随机重置集合元素的顺序
- public static void sort(List
list) //升序排序(元素类型必须实现Comparable接口)
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class Demo04 {
public static void main(String[] args) {
List<Integer> list = new ArrayList<>();
list.add(20);
list.add(18);
list.add(2);
list.add(200);
list.add(2000);
//sort排序
System.out.println("排序之前:"+list.toString());
Collections.sort(list);
System.out.println("排序之后:"+list.toString());
//binarySearch二分查找
int i = Collections.binarySearch(list,13);
System.out.println(i);
//copy复制
List<Integer> list2 = new ArrayList<>();
for (int j = 0; j < list.size(); j++) {
list2.add(0);
}
Collections.copy(list2,list);
System.out.println(list2.toString());
//reverse反转
Collections.reverse(list);
System.out.println("反转之后:"+list.toString());
//shuffle打乱
Collections.shuffle(list);
System.out.println("打乱之后:"+list.toString());
//补充:list转成数组
Integer[] array = list.toArray(new Integer[0]);
System.out.println("数组长度"+array.length);
System.out.println(Arrays.toString(array));
//数组转成集合
String[] names = {"张三","李四","王五"};
//集合是一个受限集合,不能添加修改和删除
List<String> list3 = Arrays.asList(names);
System.out.println(list3.toString());
//把基本类型数组转成集合时,需要修改为包装类型
Integer[] nums = {100,200,300};
List<Integer> list4 = Arrays.asList(nums);
System.out.println(list4.toString());
}
}
标签:name,框架,System,println,Student,集合,public,out
From: https://www.cnblogs.com/gy486926/p/17580732.html