Java 集合框架(链表、集合、映射),适合Java初学者
程序员文章站
2022-06-24 12:36:55
本文参考自我的Java老师,详情看代码注释,有写得不明白的地方欢迎留言,有错误地方欢迎大佬批评指正目录结构Cat类是共用的,在不同的框架会相对应的更改一、链表List链表是一种有序集合,也称为序列。常用的有ArrayList和LinedList1.0 Catpackage connection.List;public class Cat{ private String name; private int age; //无参构造 public Cat(){}...
本文参考自我的Java老师,详情看代码注释,有写得不明白的地方欢迎留言,有错误地方欢迎大佬批评指正
目录结构
Cat类是共用的,在不同的框架会相对应的更改
一、链表
List链表是一种有序集合,也称为序列。
常用的有ArrayList和LinedList
1.0 Cat
package connection.List;
public class Cat{
private String name;
private int age;
//无参构造
public Cat(){}
//全参构造
public Cat(String name,int age){
this.name=name;
this.age=age;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName(){
return name;
}
public void setName(String name){
this.name=name;
}
}
1.1 ArrayList
package connection.List;
import java.util.Iterator;
import java.util.List;
public class ArrayList {
public static void main(String[] args) {
Cat tomcat=new Cat();
tomcat.setName("Tom");
Cat JackCat = new Cat();
JackCat.setName("Jack");
Cat JohnCat=new Cat();
JohnCat.setName("John");
Cat AnnCat=new Cat();
AnnCat.setName("Ann");
List<Cat> cats=new java.util.ArrayList<Cat>();
cats.add(tomcat);
cats.add(JackCat);
cats.add(JohnCat);
System.out.print("First value of list:");
System.out.println(cats.get(0).getName());
cats.set(0,JackCat);
System.out.print("First value of list:");
System.out.println(cats.get(0).getName());
//判断元素及其位置
System.out.print("Dose list 'cats' contains 'JackCat'? ");
System.out.println(cats.contains(JackCat));
System.out.print("So,tell me the index of JackCat in list 'cats':");
System.out.println(cats.indexOf(JackCat));
//清除元素
//cats.clear();
//链表判空
System.out.print("Is the list empty?:");
System.out.println(cats.isEmpty());
//ArrayList转化为数组1
System.out.print("Turn ArrayList to Array: ");
Cat[] c=cats.toArray(new Cat[0]);
System.out.println(c[0].getName());
//ArrayList转化为数组1
Cat[] array=new Cat[cats.size()];
cats.toArray(array);
for (int i = 0; i < array.length; i++) {
System.out.print(array[i].getName()+" ");
}
System.out.println();
//遍历
//1、通过索引访问
System.out.println("traversal thought index:");
for (int i = 0; i < cats.size(); i++) {
System.out.print(cats.get(i).getName()+" ");
}
System.out.println();
//2、通过迭代器访问
System.out.println("traversal thought iterator:");
Iterator it=cats.iterator();
while (it.hasNext()){
Cat cat=(Cat) it.next();
System.out.print(cat.getName()+" ");
}
System.out.println();
//3、通过ForEach循环遍历
System.out.println("traversal thought ForEach");
for (Cat cat:cats){
System.out.print(cat.getName()+" ");
}
}
}
1.2 LinkedList
package connection.List;
public class LinkedList {
public static void main(String[] args) {
Cat tomcat=new Cat();
tomcat.setName("Tom");
Cat JackCat = new Cat();
JackCat.setName("Jack");
Cat JohnCat=new Cat();
JohnCat.setName("John");
Cat AnnCat=new Cat();
AnnCat.setName("Ann");
// 新建一个LinkedList
java.util.LinkedList<Cat> stack=new java.util.LinkedList<Cat>();
// 元素压入栈中
stack.push(tomcat);
stack.push(JackCat);
stack.push(JohnCat);
stack.push(AnnCat);
// 删除栈顶元素
System.out.println("stack.pop():"+stack.pop().getName());
// 取出栈顶元素
System.out.println("stack.peek():"+stack.peek().getName());
}
}
二、集合
2.0 Cat 类
package connection.List;
public class Cat implements Comparable{
private String name;
private int age;
public Cat(){}
public Cat(String name,int age){
this.name=name;
this.age=age;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName(){
return name;
}
public void setName(String name){
this.name=name;
}
@Override
//通过年龄进行比较
public int compareTo(Object o) {
if(!(o instanceof Cat)){
throw new RuntimeException("Not the Object of 'Cat'");
}
//强制类型转换
Cat p =(Cat)o;
if(this.age>p.age){
return 1;
}
//年龄相同则根据名字的字典序排序
if(this.age==p.age) {
return this.name.compareTo(p.name);
}
return -1;
}
}
2.1 HashSet
package connection.set;
import connection.List.Cat;
import java.util.*;
/**
* HashSet是无序得,由HashMap实现
*/
public class HashSetTest {
public static void main(String[] args) {
Cat tomcat=new Cat();
tomcat.setName("Tom");
Cat JackCat = new Cat();
JackCat.setName("Jack");
Cat JohnCat=new Cat();
JohnCat.setName("John");
Cat AnnCat=new Cat();
AnnCat.setName("Ann");
Set<Cat> cats=new HashSet<>();
//tomcat加了两次
cats.add(tomcat);
cats.add(tomcat);
cats.add(JackCat);
cats.add(JohnCat);
// 通过迭代器访问可以看到,Set不允许有重复元素
Iterator<Cat> it=cats.iterator();
while (it.hasNext()){
Cat cat=it.next();
System.out.println(cat.getName());
}
}
}
2.2 TreeSet
package connection.set;
import connection.List.Cat;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;
/**
* TreeSet 是一个有序的集合,它的作用是提供有序的Set集合.
* TreeSet中的元素支持2种排序方式.
* 自然排序或者根据创建TreeSet时提供的 Comparator 进行排序.
*/
public class TreeSetTest {
public static void main(String[] args) {
Set<Cat> cats=new TreeSet<>();
Cat tomcat = new Cat();
tomcat.setName("Tom");
tomcat.setAge(8);
Cat JackCat = new Cat();
JackCat.setName("Jack");
JackCat.setAge(4);
Cat JohnCat = new Cat();
JohnCat.setName("John");
JohnCat.setAge(6);
Cat AnnCat = new Cat();
AnnCat.setName("Ann");
//插入数据
cats.add(tomcat);
cats.add(tomcat);
cats.add(JackCat);
cats.add(JohnCat);
Iterator<Cat> it=cats.iterator();
while (it.hasNext()){
Cat cat=it.next();
System.out.println(cat.getName());
}
}
}
三、映射
3.0 Cat类与上相同
3.1 HashMap
package connection.map;
/**
* Map是映射接口,Map中存储的内容是键值对(key-value).
* Map映射中不能包含重复的键,每个键最多只能映射到一个值
*/
import connection.List.Cat;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
/**
*HashMap 继承于AbstractMap,但没实现NavigableMap接口.
* 因此,HashMap的内容是“键值对,但不保证次序”.
*/
public class HashMapTest {
public static void main(String[] args) {
Map<String, Cat> cats=new HashMap<String,Cat>();
cats.put("Tom",new Cat("Tom",2));
cats.put("Jack",new Cat("Jack",5));
cats.put("John",new Cat("John",7));
Cat cat=cats.get("Tom");
System.out.println(cat.getName());
Iterator iter=cats.entrySet().iterator();
while (iter.hasNext()){
Map.Entry entry=(Map.Entry) iter.next();
System.out.println("next:"+entry.getKey()+"-"+((Cat)entry.getValue()).getName()+((Cat)entry.getValue()).getAge());
}
}
}
3.2 TreeMap
package connection.map;
/**
* Map是映射接口,Map中存储的内容是键值对(key-value).
* Map映射中不能包含重复的键,每个键最多只能映射到一个值
*/
import connection.List.Cat;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;
/**
* Tree继承于AbstractMap,实现Map接口.
* TreeMap 是一个有序的 key-value,它是通过红黑树实现的
*/
public class TreeMapTest {
public static void main(String[] args) {
Map<String, Cat> cats=new TreeMap<String, Cat>();
cats.put("Tom",new Cat("Tom",2));
cats.put("Jack",new Cat("Jack",2));
cats.put("John",new Cat("hello",12));
Cat cat=cats.get("John");
System.out.println(cat.getName()+" "+cat.getAge());
Iterator iter=cats.entrySet().iterator();
while (iter.hasNext()){
Map.Entry entry=(Map.Entry) iter.next();
System.out.println("next:"+entry.getKey()+
"-"+((Cat)entry.getValue()).getName()+
"-"+((Cat)entry.getValue()).getAge());
}
}
}
本文地址:https://blog.csdn.net/weixin_45728842/article/details/110879799