Java集合(上)java
Java中的集合是工具類,能夠存儲任意數量的具備共同屬性的對象編程
集合是屬於 java.util 包的(想要具體瞭解能夠查找API文檔,下面我所有列舉)
Collectionswift
List數組
ArrayListapp
package com.sh.set;import java.util.ArrayList;框架
import java.util.List;編程語言
public class ListDemo1 {編輯器
public static void main(String[] args) {ide
// 用ArrayList存儲編程語言的名稱,並輸出工具
List list=new ArrayList();
list.add("Java");
list.add("C");
list.add("C++");
list.add("Go");
list.add("swift");
//輸出列表中元素的個數
System.out.println("列表中元素的個數爲:"+list.size());
//遍歷輸出全部編程語言
System.out.println("**");
for(int i=0;i<list.size();i++){
System.out.print(list.get(i)+",");
}
//移除列表中的C++
System.out.println();
list.remove(2);
// list.remove("C++");
System.out.println("**");
System.out.println("移除C++之後的列表元素爲:");
for(int i=0;i<list.size();i++){
System.out.print(list.get(i)+",");
}
}
}
package com.sh.set;import java.util.Date;
public class Notice {
private int id;//ID
private String title;//標題
private String creator;//建立人
private Date createTime;//建立時間
public Notice(int id, String title, String creator, Date createTime) {
super();
this.id = id;
this.title = title;
this.creator = creator;
this.createTime = createTime;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getCreator() {
return creator;
}
public void setCreator(String creator) {
this.creator = creator;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
}
//測試類
package com.sh.set;
import java.util.ArrayList;
import java.util.Date;
public class NoticeTest {
public static void main(String[] args) {
// 建立Notice類的對象,生成三條公告
Notice notice1 = new Notice(1, "歡迎來到學習網!", "管理員", new Date());
Notice notice2 = new Notice(2, "請同窗們按時提交做業!", "老師", new Date());
Notice notice3 = new Notice(3, "考勤通知!", "老師", new Date());
// 添加公告
ArrayList noticeList = new ArrayList();
noticeList.add(notice1);
noticeList.add(notice2);
noticeList.add(notice3);
// 顯示公告
System.out.println("公告的內容爲:");
for (int i = 0; i < noticeList.size(); i++) {
System.out.println(i + 1 + ":" + ((Notice) (noticeList.get(i))).getTitle());
}
System.out.println("時間:"+((Notice)(noticeList.get(1))).getCreateTime());
System.out.println("**");
// 在第一條公告後面添加一條新公告
Notice notice4 = new Notice(4, "在線編輯器能夠使用啦!", "管理員", new Date());
noticeList.add(1, notice4);
// 顯示公告
System.out.println("公告的內容爲:");
for (int i = 0; i < noticeList.size(); i++) {
System.out.println(i + 1 + ":" + ((Notice) (noticeList.get(i))).getTitle());
}
System.out.println("**");
// 刪除按時提交做業的公告
noticeList.remove(2);
// 顯示公告
System.out.println("刪除公告後的內容爲:");
for (int i = 0; i < noticeList.size(); i++) {
System.out.println(i + 1 + ":" + ((Notice) (noticeList.get(i))).getTitle());
}
//將第二條公告改成:Java在線編輯器能夠使用啦!
System.out.println("**");
//修改第二條公告中title的值
notice4.setTitle("Java在線編輯器能夠使用啦!");
noticeList.set(1, notice4);
System.out.println("修改後公告的內容爲:");
for (int i = 0; i < noticeList.size(); i++) {
System.out.println(i + 1 + ":" + ((Notice) (noticeList.get(i))).getTitle());
}
}
}
運行結果
構造方法
package com.sh.set;import java.util.LinkedList;
public class LinkedListDemo1 {
public static void main(String[] args) {
LinkedList<String> list = new LinkedList<String>();
//向鏈表添加數據
list.add("apple");
list.add("pear");
//將數據添加到鏈表的開始
list.addFirst("banana");
//將數據添加到鏈表的末尾
list.addLast("grape");
//在指定位置處添加數據,第一個參數爲index值,從0開始
list.add(2, "orange");
//顯示鏈表中的全部數據
System.out.println(list);
//判斷列表中是否包含指定的元素,並輸出相應的結果
boolean flag = list.contains("grape");
if (flag) {
System.out.println("grape找到了!");
} else {
System.out.println("grape沒找到!");
}
//返回index值爲3的數據並輸出
System.out.println("index值爲3的數據爲:" + list.get(3));
//返回第一個元素
System.out.println("第一個元素爲:" + list.getFirst());
//返回最後一個元素
System.out.println("最後一個元素爲:" + list.getLast());
}
}
Student類package com.sh.set;
public class Student {
private String stuNum;
private String stuName;
private int age;
public Student(String stuNum, String stuName, int age) {
this.stuNum = stuNum;
this.stuName = stuName;
this.age = age;
}
public String getStuNum() {
return stuNum;
}
public void setStuNum(String stuNum) {
this.stuNum = stuNum;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return " [學號:" + stuNum + ", 姓名:" + stuName + ", 年齡:" + age + "]";
}
}
LinkedListDemo2類
package com.sh.set;
import java.util.LinkedList;
public class LinkedListDemo2 {
public static void main(String[] args) {
LinkedList<Student> stuList = new LinkedList<Student>();
Student stu1 = new Student("001", "Mike", 18);
Student stu2 = new Student("002", "Jack", 20);
Student stu3 = new Student("003", "Lucy", 19);
// 將學生添加到鏈表,使用push完成
// LinkedList實現List接口的同時,也實現了Queue接口
// push和pop就是針對Queue進行添加和取出數據的操做的
stuList.push(stu1);
stuList.push(stu2);
stuList.push(stu3);
System.out.println("鏈表爲:" + stuList);
// 彈出一個元素,這裏能夠把鏈表當作一個容器,先加入到鏈表的數據後彈出,
// 依據的原則是先進後出
System.out.println("彈出的數據爲:" + stuList.pop());
System.out.println("調用pop()方法後的鏈表爲:n" + stuList);
// peek()方法獲取並不移除元素
System.out.println("調用peek()方法的數據爲:" + stuList.peek());
System.out.println("調用peek()方法後的鏈表爲:n" + stuList);
// 再次調用pop()方法,發現調用pop()方法後數據從鏈表中移除了,而peek()方法不會
System.out.println("再次調用pop()方法" + stuList.pop());
System.out.println("再次調用pop()方法後的鏈表爲:n" + stuList);
// 在鏈表中再從新添加元素
stuList.push(stu2);
stuList.push(stu3);
System.out.println("再次添加元素後的鏈表爲:n" + stuList);
// 調用poll()方法
System.out.println("調用poll()方法輸出元素" + stuList.poll());
// 調用poll()方法將獲取元素的同時從鏈表中刪除了元素
System.out.println("調用poll()方法後的鏈表爲:n" + stuList);
}
}
Set
HashSet
package com.sh.set;import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
public class WordDemo {
public static void main(String[] args) {
// 將英文單詞添加到HashSet中
Set set = new HashSet();
// 向集合中添加元素
set.add("blue");
set.add("red");
set.add("black");
set.add("yellow");
set.add("white");
// 顯示集合的內容
System.out.println("集合中的元素爲:");
Iterator it = set.iterator();
// 遍歷迭代器並輸出元素
while (it.hasNext()) {
System.out.print(it.next() + " ");
}
// for(Iterator<Set> it1=set.iterator();it1.hasNext();) {
// System.out.println(it1.next());
// }
System.out.println();
// 在集合中插入一個新的單詞
// set.add("green");
set.add("white");
it = set.iterator();
// 遍歷迭代器並輸出元素
System.out.println("");
System.out.println("插入重複元素後的輸出結果爲:");
while (it.hasNext()) {
System.out.print(it.next() + " ");
}
//插入失敗,可是不會報錯
}
}
哈希表和普通存儲序列 (哈希表能夠提升數據查找速度)
哈希表分三個(多個)區域來存放數據,什麼數據放到哪一個區域 有一的規則(hashCode)
package com.sh.set;public class Cat {
private String name; //名字
private int month; //年齡
private String species;//品種
//構造方法
public Cat(String name, int month, String species) {
super();
this.name = name;
this.month = month;
this.species = species;
}
//getter與setter方法
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public String getSpecies() {
return species;
}
public void setSpecies(String species) {
this.species = species;
}
@Override
public String toString() {
return "[姓名:" + name + ", 年齡:" + month + ", 品種:" + species + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + month;
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((species == null) ? 0 : species.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
//判斷對象是否相等,相等則返回true,不用繼續比較屬性了
if(this==obj)
return true;
//判斷obj是不是Cat類的對象
if(obj.getClass()==Cat.class){
Cat cat=(Cat)obj;
return cat.getName().equals(name)&&(cat.getMonth()==month)&&(cat.getSpecies().equals(species));
}
return false;
}
}
//測試類
package com.sh.set;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
public class CatTest {
public static void main(String[] args) {
// 定義寵物貓對象
Cat huahua = new Cat("花花", 12, "英國短毛貓");
Cat fanfan = new Cat("凡凡", 3, "中華田園貓");
// 將寵物貓對象放入HashSet中
Set<Cat> set = new HashSet<Cat>();
set.add(huahua);
set.add(fanfan);
// 顯示寵物貓信息
Iterator<Cat> it = set.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
// 再添加一個與花花屬性同樣的貓
Cat huahua01 = new Cat("花花", 12, "英國短毛貓");
set.add(huahua01);
System.out.println("");
System.out.println("添加劇複數據後的寵物貓信息:");
it = set.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
System.out.println("");
// 從新插入一個新寵物貓
Cat huahua02 = new Cat("花花二代", 2, "英國短毛貓");
set.add(huahua02);
System.out.println("添加花花二代後的寵物貓信息:");
it = set.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
System.out.println("");
// 在集合中查找花花的信息並輸出
if (set.contains(huahua)) {
System.out.println("花花找到了!");
System.out.println(huahua);
} else {
System.out.println("花花沒找到!");
}
// 在集合中使用名字查找花花的信息
System.out.println("");
System.out.println("經過名字查找花花信息");
boolean flag = false;
Cat c = null;
it = set.iterator();
while (it.hasNext()) {
c = it.next();
if (c.getName().equals("花花")) {
flag = true;// 找到了
break;
}
}
if (flag) {
System.out.println("花花找到了");
System.out.println(c);
} else {
System.out.println("花花沒找到");
}
// 刪除花花二代的信息並從新輸出
for (Cat cat : set) {
if ("花花二代".equals(cat.getName())) {
set.remove(cat);
break;
}
}
System.out.println("");
System.out.println("刪除花花二代後的數據");
for(Cat cat:set){
System.out.println(cat);
}
//刪除集合中的全部寵物貓信息
System.out.println("");
boolean flag1=set.removeAll(set);
if(set.isEmpty()){
System.out.println("貓都不見了。。。");
}else{
System.out.println("貓還在。。。");
}
}
}
運行截圖