List接口
Arraylist
Collection接口是最基本的集合接口,可以存储一组不唯一,无序的对象。List接口继承自Collection接口,是有序集合。用户可使用索引访问接口中的元素,类似于数组。List接口中允许存放重复的元素,也就是说List可以存储一组不唯一,有序的对象。
LinkedList
LinkedList类是List接口的链接列表实现类。它支持实现所有List接口可选的列表的操作,并且允许元素值是如何数据,包括null。
LinkedList类采用链表方式存储数据,优点在于插入,删除元素时效率比较高,但是查找效率很低。
Set接口
Set接口时Collection接口的另外一个常用子接口,Set接口描述的是一种比较简单的集合,集合中的对象并不按特定的方式排序,并且不能保存重复的对象,也就是说Set接口可以存储一组唯一,无序的对象。
HashSet
假如现在需要在很多数据中查找某个数据,LinkedList类就无需考虑了,它的数据结构决定了它的查找效率低下如果使用ArryList类,在不知道数据的索引且需要全部遍历的情况下。为此Java集合框架提供了一个查找效率高的集合类HashSet,HashSet类实现了Set接口,是使用Set集合时最常用的一个实现类。
HashSet特点:
1.集合内的元素是无序排序的
2.HashSet类是非线程安全的
3.允许集合元素为null
HashSet常用方法:
1、add(Object obj):向Set集合中添加元素,添加成功返回true,否则返回false。
2、size():返回Set集合中的元素个数。
3、remove(Object obj): 删除Set集合中的元素,删除成功返回true,否则返回false。
4、isEmpty():如果Set不包含元素,则返回 true ,否则返回false。
5、clear(): 移除此Set中的所有元素。
6、contains(Object o):如果Set包含指定的元素,则返回 true,否则返回false。
Arrylist和LinkedList代码
package top.xiaoguyouqu.shangke;
public class NewTitle {
private int ID; //ID
private String name; //名称
private String author; //作者
//无参构造器
public NewTitle() {
}
//有参构造器
public NewTitle(int ID, String name,String author) {
this.ID = ID;
this.name = name;
this.author = author;
}
public int getID() {
return ID;
}
public void setID(int ID) {
this.ID = ID;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
}
package top.xiaoguyouqu.shangke;
import java.util.ArrayList;
import java.util.LinkedList;
public class test {
public static void main(String[] args) {
//new出多个新闻标题实例
NewTitle NBA = new NewTitle(1,"保罗乔治","小明");
NewTitle CBA = new NewTitle(2,"郭艾伦1","小利");
NewTitle CBA1 = new NewTitle(3,"郭艾伦2","小李2");
NewTitle CBA2 = new NewTitle(4,"郭艾伦3","小李3");
NewTitle CBA3 = new NewTitle(5,"郭艾伦4","小李4");
NewTitle first = new NewTitle(6,"头标题","小李5");
NewTitle end = new NewTitle(7,"尾标题","小李6");
//创建集合对象
// ArrayList<NewTitle> newTitles = new ArrayList<NewTitle>();
LinkedList<NewTitle> newTitles = new LinkedList<>();
//添加数据
newTitles.add(NBA);
newTitles.add(CBA);
newTitles.add(CBA1);
// 添加头条和末条新闻标题
newTitles.addFirst(first);
newTitles.addLast(end);
// 获取头条和末条新闻标题
System.out.println(newTitles.getFirst());
System.out.println(newTitles.getLast());
//删除头条和末条新闻标题
newTitles.removeFirst();
newTitles.removeLast();
//添加新闻标题
// newTitles.add(NBA);
// newTitles.add(CBA);
// newTitles.add(CBA1);
// newTitles.add(CBA2);
// newTitles.add(CBA3);
// newTitles.add(CBA4);
// newTitles.add(CBA5);
//获取标题总数
System.out.println(newTitles.size());
//根据位置获取相应的新闻标题,逐条打印每条新闻标题
for (int i = 0; i < newTitles.size(); i++) {
System.out.println(newTitles.get(i).getName());
}
//增强型for循环(遍历对象)
// for (NewTitle element :newTitles){
// System.out.println(element);
// NewTitle title = (NewTitle) element;
// System.out.println(title.getName());
// }
}
}
迭代器
Iterator接口表示对集合进行迭代的迭代器。Iterator接口为集合而生,专门实现集合的遍历。主要有两个方法:
1.hasNext():判断是否存在下一个可访问的元素,如果仍有元素可以迭代,则放回ture
2.next():返回要访问的下一个元素
tips:凡是由Cooleection接口派生而来的接口或者类,都实现了ArryList集合
代码演示
package top.xiaoguyouqu.shangke;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
public class test {
public static void main(String[] args) {
//new出多个新闻标题实例
NewTitle NBA = new NewTitle(1, "保罗乔治", "小明");
NewTitle CBA = new NewTitle(2, "郭艾伦1", "小利");
NewTitle CBA1 = new NewTitle(3, "郭艾伦2", "小李2");
NewTitle CBA2 = new NewTitle(4, "郭艾伦3", "小李3");
NewTitle CBA3 = new NewTitle(5, "郭艾伦4", "小李4");
NewTitle first = new NewTitle(6, "头标题", "小李5");
NewTitle end = new NewTitle(7, "尾标题", "小李6");
//创建集合对象
// ArrayList<NewTitle> newTitles = new ArrayList<NewTitle>();
LinkedList<NewTitle> newTitles = new LinkedList<>();
//添加数据
newTitles.add(NBA);
newTitles.add(CBA);
newTitles.add(CBA1);
// 添加头条和末条新闻标题
newTitles.addFirst(first);
newTitles.addLast(end);
// 获取头条和末条新闻标题
System.out.println(newTitles.getFirst());
System.out.println(newTitles.getLast());
// 使用迭代器进行遍历
Iterator it = newTitles.iterator();//使用集合的iterator方法返回一个Iterator对象
while (it.hasNext()){
NewTitle title = (NewTitle) it.next();
System.out.println(title.getName());
}
}
}
运行结果
Map接口
1.Map接口概述
Map接口存储成对的键(key)和值(value)对象,提供key到value的映射,通过key来检索。Map接口中的key不要求有序,不允许重复。value同样不要求有序,但允许重复。
最常用的Map实现类是HashMap,其优点是查询指定元素效率高。
案例:使用HashMap类存储学生信息,要求可以根据学生英文名检索学生信息。
package top.xiaoguyouqu.shangke;
public class student {
private String name;
private int age ;
private double score;
public student() {
}
public student(String name, int age, double score) {
this.name = name;
this.age = age;
this.score = score;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
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;
}
}
package top.xiaoguyouqu.shangke;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class test2 {
//使用HashMap类存储学生信息,要求可以根据英文名检索学生信息
public static void main(String[] args) {
student s1 = new student("小明",19,88);
student s2 = new student("小李",20,98);
student s3 = new student("小黄·",17,87);
student s4 = new student("小红",18,68);
Map students = new HashMap();
students.put("jake",s1);
students.put("juny",s2);
students.put("hay",s3);
students.put("may",s4);
System.out.println("已经添加了"+students.size()+"个学生信息");
while (true) {
System.out.println("请输入要查找学生的英文名:");
Scanner sc = new Scanner(System.in);
String englishName = sc.next();
if (students.containsKey(englishName)) {
student s = (student) students.get(englishName);
System.out.println(s.getName());
System.out.println(s.getAge());
System.out.println(s.getScore());
} else {
System.out.println("很抱歉!系统中没有这个人。");
}
}
}
}
2 条评论
hashset 没重写方法之前判断的是地址相等不相等,所以值相等的时候也会被添加进去,想要放入的时候值不一样就要重写方法
set存储唯一,无序的元素