集合类体系结构
集合中存储的都是引用数据类型,基本数据类型要使用其对应的包装类
Collection集合
一、Collection集合常用方法:
方法名 | 说明 |
---|---|
boolean add(E e) | 添加元素 |
boolean remove(Object o) | 从集合中移除指定元素 |
void clean() | 清空集合中的元素 |
boolean contains(Object o) | 判断集合中是否存在指定的元素 |
boolean isEmpty() | 判断集合是否为空 |
int size() | 集合长度,即集合中元素的个数 |
public class CollectionDemo {
public static void main(String[] args) {
//创建对象
Collection <String> collection = new ArrayList<String>();
//boolean add(E e) 添加元素
collection.add("Have");
collection.add("a");
collection.add("good");
collection.add("day");
//打印输出集合对象
System.out.println(collection);
//boolean remove(Object o) 从集合中移除指定元素
collection.remove("Have");
//boolean contains(Object o) 判断集合中是否存在指定的元素
System.out.println(collection.contains("Have"));
//boolean isEmpty() 判断集合是否为空
System.out.println(collection.isEmpty());
//int size() 集合长度,即集合中元素的个数
System.out.println(collection.size());
//void clear() 清空集合中的元素
collection.clear();
System.out.println(collection.isEmpty());
}
}
二、迭代器Iterator
Iterator:迭代器,集合专用遍历方式
Iterator<E>iterator()
:返回此集合中元素的迭代器,通过集合iterator()方法得到,E的数据类型要与集合数据类型一致。- 迭代器是通过集合的iterator()方法得到的,所以我们说它是依赖于集合而存在的
Iterator中的常用方法:
方法名 | 说明 |
---|---|
E next() | 返回迭代中的下一个元素 |
boolean hasNext() | 如果迭代具有更多元素,则返回true |
/*Iterator:集合专属遍历方式*/
public class IteratorDemo {
public static void main(String[] args) {
// 创建集合对象
Collection<String> collection = new ArrayList<String>();
collection.add("Hello");
collection.add("world");
collection.add("Java");
System.out.println(collection);
//创建迭代器对象
Iterator<String> iterator = collection.iterator();
/*
public Iterator<E> iterator() {
return new Itr();
}
private class Itr implements Iterator<E> {
...
}
*/
// System.out.println(iterator.next());
// System.out.println(iterator.next());
// System.out.println(iterator.next());
// System.out.println(iterator.next());//NoSuchElementException:没有找到指定元素
while (iterator.hasNext()){
String s = iterator.next();
System.out.println(s);
}
}
}
三、集合使用步骤
- 创建集合:
Collection<String> collection = new ArrayList<String>();
- 添加元素到集合中:
collection.add("Hello"); ...
- 集合遍历,通过集合对象获取迭代器对象:
Iterator<String> iterator = collection.iterator();
- 集合遍历,判断是否还有元素:
while (iterator.hasNext()){...}
- 集合遍历,获取下一个元素:
String s = iterator.next();
四、使用Collection集合存储3个学生对象,并在控制台遍历该集合
/*使用Collection集合存储3个学生对象,并在控制台遍历该集合*/
public class Practice {
public static void main(String[] args) {
//创建学生对象
Student student1 = new Student();
student1.setName("张三");
student1.setAge(17);
Student student2 = new Student();
student2.setName("李四");
student2.setAge(17);
Student student3 = new Student();
student3.setName("王五");
student3.setAge(17);
//创建集合对象并添加元素
Collection<Student> collection = new ArrayList<Student>();
collection.add(student1);
collection.add(student2);
collection.add(student3);
//遍历集合
Iterator<Student> iterator = collection.iterator();
while (iterator.hasNext()){
//创建对象接收获取到的集合元素
Student student = iterator.next();
System.out.println(student.getName()+student.getAge());
}
}
}
List集合
(一)List集合概述和特点:
- 有序集合(也称为序列),用户可以精确控制列表中每个元素的插入位置。用户可以通过整数索引访问元素,并搜索列表中的元素
- 与Set集合不同,List通常允许重复的元素
- 有序:存储的元素和取出的元素一致
- 可重复,允许元素可重复
/*有序:存储的元素和取出的元素一致
可重复,允许元素可重复*/
public class ListDemo {
public static void main(String[] args) {
//创建List集合
List<String> list = new ArrayList<String>();
list.add("A");
list.add("happy");
list.add("day");
System.out.println(list);
//使用迭代器的方式遍历
Iterator<String> iterator = list.iterator();
while (iterator.hasNext()){
String s = iterator.next();
System.out.println(s);
}
}
}
(二)List集合特有方法:
方法名 | 说明 |
---|---|
void add(int index ,E element) | 添加指定元素到集合中指定位置 |
E remove(int index) | 删除指定索引处的元素,返回被删除元素 |
E set(int index ,E element) | 修改指定索引处的元素,返回被修改的元素 |
E get(int index) | 返回指定索引处元素 |
public class ListDemo {
public static void main(String[] args) {
//创建List集合
List<String> list = new ArrayList<String>();
//void add(int index ,E element) 添加指定元素到集合中指定位置
// list.add(2,"day");//IndexOutOfBoundsException: Index: 2, Size: 0
// list.add(0,"A");
// list.add(1,"happy");
list.add(0,"A");
list.add(1,"happy");
list.add(2,"day");
//E remove(int index) 删除指定索引处的元素,返回被删除元素
list.remove(0);
//E set(int index ,E element) 修改指定索引处的元素,返回被修改的元素
// list.set(2,"week");//IndexOutOfBoundsException: Index: 2, Size: 2
list.set(1,"week");
//E get(int index) 返回指定索引处元素
System.out.println(list.get(0));
System.out.println(list);
//使用迭代器的方式遍历
Iterator<String> iterator = list.iterator();
while (iterator.hasNext()){
String s = iterator.next();
System.out.println(s);
}
}
}
注:add()
方法添加元素要从0开始,顺序递增添加
(三)并发修改异常
- ConcurrentModificationException,并发修改异常
- 产生原因:迭代器遍历过程中,通过集合对象,修改了集合中元素的长度造成了迭代器获取元素中判断预期修改值与实际修改值不一致。
- 解决方法:使用for()循环替代迭代器Iterator遍历的方式。
public class CollectionDemo {
public static void main(String[] args) {
//创建集合对象
List<String> list = new ArrayList<String>();
list.add("hello");
list.add("world");
list.add("java");
//创建迭代器对象
Iterator <String> iterator = list.iterator();
while (iterator.hasNext()){
//Exception in thread "main" java.util.ConcurrentModificationException,并发修改异常
String string = iterator.next();
if (string == "world"){
list.add("javaee");
}
}
}
}
/*
* Exception in thread "main" java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:911)
at java.util.ArrayList$Itr.next(ArrayList.java:861)
at CollectionDemo.main(CollectionDemo.java:17)
* */
(四)列表迭代器
ListIterator:列表迭代器
- 通过List的listIterator()方法得到,所以说它是List集合的特有迭代器
- 用于允许程序员沿任一方向遍历列表的迭代器,在迭代期间修改列表,并返回当前迭代器的位置。
ListIterator的特有方法:
- E next():返回迭代中的下一个元素
- boolean hasNext():如果迭代具有更多元素,则返回true
- E previous():返回迭代中的上一个元素
- boolean hasPrevious():如果此列表迭代器在相反方向遍历列表时具有更多元素,则返回ture
- void add(E e):将指定元素插入列表
public class ListIteratorDemo {
public static void main(String[] args) {
List <String> list = new ArrayList<>();
list.add("hello");
list.add("world");
list.add("java");
ListIterator<String> listIterator = list.listIterator();
while (listIterator.hasNext()){
String string = listIterator.next();
System.out.println(string);
}
System.out.println("-----------------------");
while (listIterator.hasPrevious()){
String s = listIterator.previous();
System.out.println(s);
}
System.out.println("-----------------------");
while (listIterator.hasNext()){
String string1 = listIterator.next();
if (string1.equals("world")){
listIterator.add("javaee");
}
}
System.out.println(list);
}
}
(五)增强for循环
- 为了简化数组或Collection集合的遍历
- 实现Iterable接口的类允许成为增强for循环(each for loop)的对象
- 内部原理为一个迭代器
- 格式:
for(元素数据类型 变量名 : 数组或者Collection集合){...}
public class EachForLoopDemo {
public static void main(String[] args) {
int[] arr = {1,2,3,4,5};
for (int i : arr){
System.out.println(i);
}
System.out.println("----------------");
String[] strArray = {"hello","world","java"};
for(String s : strArray){
System.out.println(s);
}
System.out.println("----------------");
List<String> list = new ArrayList<>();
list.add("Today");
list.add("is");
list.add("Monday");
for (String s1 : list){
System.out.println(s1);
}
}
}
(六)常见数据结构,栈,队列,数组,链表
- 栈,数据从栈顶进入,从栈底出栈,先进后出
- 队列,数据从队尾进入,对头出去,先进先出
- 数组,是一种查询快,增删慢的数据模型。查询数据通过索引定位,查询任意数据耗时相同,查询效率高。删除数据时,先将原始数据删除,后面的数据再依次前移,删除效率低。添加数据时,将添加位置之后的数据依次后移,再添加元素,更新索引,添加效率低。
- 链表,是一种增删快,查询慢的数据模型。增加数据时,修改前一个结点 的地址指向新增结点,修改新增结点的地址指向下一个结点。删除数据时,修改前一个结点的地址指向被删除结点的下一个结点的地址。查询数据时,都是从头开始。
(七)List集合子类的特点:
- ArrayList,List接口基于数组的可调整大小的实现。查询快,增删慢。
- LinkedList,双链表实现了List和Deque接口。查询慢,增删快。
ArrayList集合
LinkedList集合
LinkedList集合的特有功能:
方法名 | 说明 |
---|---|
public void addFirst(E e) | 将指定元素添加到列表的开头 |
public void addLast(E e) | 将指定元素添加到列表的末尾 |
public E getFirst() | 返回此列表中的第一个元素 |
public E getLast() | 返回此列表中的最后一个元素 |
public E removeFirst() | 从列表中删除并返回第一个元素 |
public E removeFirst() | 从列表中删除并返回最后一个元素 |
Set集合
Set集合特点:
- 不包含重复的元素的集合
- 没有带索引的方法,不能使用普通for循环遍历
哈希值:JDK根据对象的地址或者字符串或数字计算出来的int类型的数值
Object类重有一个方法可以获取对象的哈希值
public int hashCode()
:返回对象的哈希码值
对象的哈希值特点:- 同一个对象多次调用
hashCode()
返回的哈希值是相同的 - 默认情况下,不同对象返回的哈希值也不同,可以通过重写
hashCode()
方法,让不同的对象返回相同的哈希值
public class HashCodeDemo {
public static void main(String[] args) {
Student student = new Student("张三",16);
Student student2 = new Student("张三",16);
System.out.println(student.hashCode());//460141958
System.out.println(student.hashCode());//460141958
System.out.println("--------");
System.out.println(student2.hashCode());//1163157884
System.out.println("--------");
System.out.println("hello".hashCode());//99162322
System.out.println("world".hashCode());//113318802
System.out.println("--------");
System.out.println("重地".hashCode());//1179395
System.out.println("通话".hashCode());//1179395
}
}
HashSet集合
- 底层数据结构是哈希表
- 对集合的迭代顺序不作任何保证,也就是说不保证存储和取出的元素顺序一致
- 没有带索引的方法,所以不能使用普通for循环遍历
- 由于是Set集合,所以是没有重复元素的集合
HashSet集合保证元素唯一性源码分析:
public class HashSetDemo {
public static void main(String[] args) {
//创建集合
Set<String> set = new HashSet<String>();
//添加元素
set.add("This");
set.add("is");
set.add("a");
set.add("wonderful");
set.add("day");
set.add("ahh");
set.add("day");
//遍历
for (String s : set){
System.out.println(s);
//ahh
//a
//This
//is
//wonderful
//day
}
}
}
常见数据结构之哈希表
- 在JDK8之前,底层采用数组+链表实现,可以说是一个以链表作为元素的数组
- JDK8之后,在长度比较长的时候,底层实现了优化
LinkedHashSet集合概述和特点
- 由哈希表和链表实现的Set接口,具有可预测的迭代次序
- 由哈希表保证元素唯一,即没有重复的元素
- 由链表保证迭代次序,即元素取出和存储的顺序是一致的
public class LinkedHashSetDemo {
public static void main(String[] args) {
LinkedHashSet<String> linkedHashSet = new LinkedHashSet<String>();
linkedHashSet.add("hello");
linkedHashSet.add("world");
linkedHashSet.add("java");
linkedHashSet.add("world");
//遍历
for (String s: linkedHashSet
) {
System.out.println(s);
//hello
//world
//java
}
}
}
TreeSet集合
TreeSet集合概述特点
- 元素有序,这里的顺序不是指存储和取出的顺序,而是按照一定的规则进行排序,具体排序方式取决于构造方法,TreeSet(),根据其元素的自然排序进行排序;TreeSet(Camparator camparator),根据指定的比较器进行排序
- 没有带索引的方法,不能使用普通for循环进行排序
- 由于是Set集合,所以不包括重复元素的集合
public class TreeSetDemo {
public static void main(String[] args) {
TreeSet<String> treeSet = new TreeSet<>();
treeSet.add("hello");
treeSet.add("world");
treeSet.add("java");
//遍历
for (String s: treeSet
) {
System.out.println(s);
}
}
}
自然排序Comparable的使用:
/**
* 将学生对象按照年龄降序排列,年龄相同时按照姓名自然排序*/
public class ComparableDemo implements Comparable {
public static void main(String[] args) {
//创建TreeSet集合
TreeSet<Student> treeSet = new TreeSet<>();
//创建学生对象
Student student1 = new Student("张三",16);
Student student2 = new Student("李四",17);
Student student3 = new Student("王五",14);
Student student4 = new Student("赵六",15);
Student student5 = new Student("孙七",16);
//添加学生对象
treeSet.add(student1);
treeSet.add(student2);
treeSet.add(student3);
treeSet.add(student4);
treeSet.add(student5);
//遍历
for (Student s:treeSet
) {
System.out.println(s.getName()+","+s.getAge());
//王五,14
//赵六,15
//孙七,16
//张三,16
//李四,17
}
}
/** @Override
public int compareTo(Student student) {
// return 0;//重复元素
// return 1;//按添加顺序正序
// return -1;//倒序
int num = this.age - student.age;
int num2 = num == 0?(this.name).compareTo(student.name):num;//此处的compareTo()是String类中成员方法
return num2;
}
*/
}
比较器排序Comparator的使用:
//创建TreeSet集合
TreeSet<Student> treeSet = new TreeSet<>(new Comparator<Student>() {
@Override
public int compare(Student s1, Student s2) {
//排序规则逻辑相同
int num = s1.getAge()- s2.getAge();
int num1 = num==0?s1.getName().compareTo(s2.getName()):num;
return num1;
}
});
泛型概述和好处
泛型:是JDK5之后引入的特性,它提供了编译时类型安全检测机制,该机制允许在编译时检测到非法的类型,他的本质是参数化类型,也就是说所操作的数据类型被指定为一个参数,一提到参数,最熟悉的就是定义方法时有形参,然后调用此方法时传递实参。那么参数化类型怎么理解呢?顾名思义,就是将类型由原来的具体的类型参数化,然后在使用/调用时传入具体的类型。这种参数类型可以用在类、方法和接口中,分别被称为泛型类,泛型方法、泛型接口。
泛型定义格式:
- <类型>:指定一种类型的格式,这里的类型可以看成是形参
- <类型1,类型2,…>:指定多种类型的格式,多种类型之间用逗号隔开,这里的类型可以看成是形参
- 将来具体调用时给定的类型可以看成是实参,并且实参的类型只能是引用数据类型
泛型的好处: - 把运行时期的问题提前到了编译时期
- 避免了强制类型转换
泛型类
泛型类的定义格式:修饰符 class 类名<T>{...}
public class GenericDemo {
public static void main(String[] args) {
//创建对象
GenericClass<Integer> genericClass = new GenericClass<Integer>();
//赋值
genericClass.setT(30);
System.out.println(genericClass.getT());
}
}
/**
* public class GenericClass <T> {
* private T t;
*
* public T getT() {
* return t;
* }
*
* public void setT(T t) {
* this.t = t;
* }
* }
*/
泛型方法
泛型方法的定义格式:修饰符<T>返回值类型 方法名(类型 变量名){...}
//创建对象
GennericMethod gennericMethod = new GennericMethod();
//赋值
gennericMethod.<String>show("hello");
gennericMethod.<Boolean>show(true);
gennericMethod.<Integer>show(12);
/**
* public class GennericMethod{
* public <T> void show (T t) {
* System.out.println(t);
* }
* }
*/
泛型接口
泛型接口格式:修饰符 interface
GenericInterfaceImpl<String> genericInterface = new GenericInterfaceImpl<String>();
GenericInterfaceImpl<Boolean> genericInterface1 = new GenericInterfaceImpl<Boolean>();
GenericInterfaceImpl<Integer> genericInterface2 = new GenericInterfaceImpl<Integer>();
genericInterface.show("hello");
genericInterface1.show(true);
genericInterface2.show(21);
/**
* public class GenericInterfaceImpl<T> implements GenericInterface<T>{
* @Override
* public void show(T t) {
* System.out.println(t);
* }
* }
*/
/**
*public interface GenericInterface<T> {
* void show(T t);
* }
*/
类型通配符
为了表示各种泛型List的父类,可以使用类型通配符
- 类型通配符:
<?>
List<?>
:表示元素类型未知的List,它的元素可以匹配任何的类型- 这种带通配符的List仅表示它是各种泛型List的父类,并不能把元素添加到其中
如果说我们不希望List<?>是任何泛型List的父类,只希望它代表某一类泛型List的父类,可以使用类型通配符的上限 - 类型通配符上限:
<?extends类型>
List<?extends Number>
它表示的类型是Number或者其子类型- 类型通配符下线:
<?super类型>
List<?super Number>
:它表示的类型是Number或者其父类型
//通配符
List<?> list = new ArrayList<Object>();
List<?> list1 = new ArrayList<Number>();
List<?> list2 = new ArrayList<Integer>();
//通配符上限
List<?extends Number> ls = new ArrayList<Number>();
List<?extends Number> ls1 = new ArrayList<Integer>();
//通配符下限
List<?super Number> ls2 = new ArrayList<Number>();
List<?super Number> ls3 = new ArrayList<Object>();
可变参数
可变参数又称参数个数可变,用作方法的形参出现,那么方法参数个数就是可变的了
- 格式:
修饰符 返回值类型 方法名(数据类型... 变量名){}
,public static int sum (int... a){}
- 注意事项,这里的变量其实是一个数组
- 当一个方法有多个参数,包含可变参数时,可变参数要放后面
public class VariableParameterDemo {
public static void main(String[] args) {
sum(1,2);
sum(1,2,4,6);
sum(1,2,4,6,7);
}
public static int sum(int b,int... a){
int result = 0;
int sumNum = 0;
for (int i :a
) {
sumNum += i;
}
result = sumNum + b;
System.out.println(result);
return result;
}
}
可变参数的使用
- Arrays工具类中有一个静态方法:
public static <T>List<T> asList(T..a)
:返回由指定数组支持的固定大小的列表,List<String> list = Arrays.asList("hello","world","java");
允许List的set()
方法,不允许add()
,remove()
方法。 - List接口中有一个静态方法:
public static <E>List<E> of (E... elements)
:返回包含任意元素的不可变列表List<String> list = List.of("hello","world","java")
,不允许List的set()
,add()
,remove()
方法。 - Set接口中有一个静态方法:
public static <E>Set<E> of(E..elements)
:返回一个包含任意数量元素的不可变集合,Set<String> set = Set.of("hello","world","java")
,不允许Set的add()
和remove()
方法,不允许重复元素
Map集合
Map集合概述和特点
Interface Map<K,V>
。K:键的类型,V:值的类型。- 将键映射到值的对象,不能包含重复的键,一个键最多对应一个值。
- HashMap.put()方法,第一次出现时是赋值,第二次出现时是修改。
HashMap<String, String> map = new HashMap<>();
map.put("001","张三");
map.put("002","李四");
map.put("003","王五");
map.put("003","赵六");
/**
* {001=张三, 002=李四, 003=赵六}
*/
System.out.println(map);
Map集合的基本功能
方法名 | 说明 |
---|---|
V put(K key,V value) | 添加元素 |
V remove(Object key) | 根据键删除键值对元素 |
void clear() | 移除所有键值对元素 |
boolean containsKey(Object key) | 判断集合是否包含指定的键 |
boolean containsValue(Object value) | 判断集合是否包含指定的值 |
boolean isEmpty() | 判断集合是否为空 |
int size() | 集合的长度,也就是集合中键值对的个数 |
public class MapDemo {
public static void main(String[] args) {
HashMap<String, String> map = new HashMap<>();
map.put("001","张三");
map.put("002","李四");
map.put("003","王五");
System.out.println(map);//{001=张三, 002=李四, 003=王五}
System.out.println("--------");
map.remove("001");
System.out.println("移除键为001的键值对"+","+map);//移除键为001的键值对,{002=李四, 003=王五}
System.out.println("移除不存在的键009"+","+map.remove("009"));//移除不存在的键009,null
System.out.println("集合是否为空"+","+map.isEmpty());//集合是否为空,false
System.out.println("--------");
map.clear();
System.out.println("清除所有键值对元素"+map);//清除所有键值对元素{}
System.out.println("--------");
map.put("006","赵六");
map.put("007","阮小七");
System.out.println("是否存在001键"+","+map.containsKey("001"));//是否存在001键,false
System.out.println("是否存在赵六值"+","+map.containsValue("赵六"));//是否存在赵六值,true
System.out.println("集合长度"+","+map.size());//集合长度,2
}
}
Map集合的获取功能
方法名 | 说明 |
---|---|
V get(Object key) | 根据键获取值 |
Set<K> keySet() | 获取所有键的集合 |
Collection<V> values() | 获取所有值的集合 |
Set<Map.Entry<K,V>> entrySet() | 获取所有键值对对象的集合 |
public class GetMapDemo {
public static void main(String[] args) {
//创建Map集合
Map<Integer,String> map = new HashMap<Integer,String>();
//添加元素
map.put(1,"吕布");
map.put(2,"赵云");
map.put(3,"马超");
System.out.println("获取1键对应的值"+","+map.get(1));//获取1键对应的值,吕布
System.out.println("获取不存在的键对应的值"+","+map.get(4));//获取不存在的键对应的值,null
System.out.println("获取所有键的集合"+","+map.keySet());//获取所有键的集合,[1, 2, 3]
System.out.println("获取所有值的集合"+","+map.values());//获取所有值的集合,[吕布, 赵云, 马超]
System.out.println("获取所有键值对对象的集合"+","+map.entrySet());//获取所有键值对对象的集合,[1=吕布, 2=赵云, 3=马超]
}
}
Map集合的遍历方式
遍历方式1
- 获取所有键的集合,使用
keySet()
方法实现 - 遍历键的集合,获取到每一个键,用增强for实现
- 根据键去找值,用
get(Object key)
方法实现
public static void main(String[] args) {
//创建Map集合
Map<Integer,String> map = new HashMap<Integer,String>();
//添加元素
map.put(1,"熊大");
map.put(2,"熊二");
map.put(3,"光头强");
//获取所有键的集合
Set<Integer> set = map.keySet();
//遍历集合获取每一个键
for (Integer key : set
) {
//根据键去获取每一个值
String value = map.get(key);
System.out.println(key+","+value);
/**
* 1,熊大
* 2,熊二
* 3,光头强
*/
}
}
遍历方式2
- 获取所有键值对对象的集合
Set<Map.Entry<K,V>> entrySet()
- 遍历键值对对象的集合,得到每一个键值对对象
- 根据键值对对象获取键和值,
getKey()
得到键,getValue()
得到值
//创建Map集合
Map<Integer,String> map = new HashMap<Integer,String>();
//添加元素
map.put(1,"熊大");
map.put(2,"熊二");
map.put(3,"光头强");
//获取所有键值对的对象的集合
Set<Map.Entry<Integer,String>> set = map.entrySet();
//遍历键值对对象的集合,得到每一个键值对对象
for (Map.Entry<Integer,String> mapObject: set
) {
//根据键值对对象获取键和值
Integer key = mapObject.getKey();
String value = mapObject.getValue();
System.out.println(key + "," + value);
}
HashMap集合
统计字符出现次数
public class StatisticsTimes {
public static void main(String[] args) {
//获取键盘输入的字符串
Scanner scanner = new Scanner(System.in);
System.out.println("请输入一个字符串");
String Line = scanner.nextLine();
//创建HashMap的键存储出现的字符,值统计该字符出现的次数
HashMap<Character,Integer> hashMap = new HashMap<Character,Integer>();
//遍历字符串
for (int i = 0;i < Line.length(); i++){
char key = Line.charAt(i);
int cout = 1;
//将每一个字符作为键去HashMap中找对应的值
if (hashMap.get(key) == null){
hashMap.put(key,cout);
}else {
hashMap.put(key,cout+1);
}
}
//遍历HashMap集合
Set<Map.Entry<Character, Integer>> set = hashMap.entrySet();
for (Map.Entry<Character, Integer> mapObject : set
) {
Character mapKey = mapObject.getKey();
Integer mapValue = mapObject.getValue();
System.out.println(mapKey+"("+mapValue+")");
}
}
}
Collections概述和使用
- 是针对集合操作的工具类
public static <T extends Comparable<?super T>> void sort (List<T> list)
:将指定列表按升序排序public static void reverse(List<?> list)
:反转指定列表中元素的顺序public static void shuffle(List<?> list)
:使用默认的随机源随机排列指定的列表
public class CollectionsDemo {
public static void main(String[] args) {
//创建ArrayList集合
ArrayList<Integer> arrayList = new ArrayList<>();
arrayList.add(22);
arrayList.add(44);
arrayList.add(11);
arrayList.add(33);
// System.out.println(arrayList);//[22, 44, 11, 33]
// Collections.sort(arrayList);
// System.out.println(arrayList);//[11, 22, 33, 44]
// Collections.reverse(arrayList);
// System.out.println(arrayList);//[33, 11, 44, 22]
Collections.shuffle(arrayList);
System.out.println(arrayList);//[33, 22, 11, 44]
}
}