一、Collection接口(父类)有继承关系的两个类:
1、List集合(子类):
*1List常用接口ArrayList类
Arraylist类实现List接口,其数据结构与数组相同,又被称为数组列表类,采用的顺序结构来存储数据。类似于动态数组。应用实例:
*2LinkedList类同样实现List接口,其采用的是链表结构来储存数据,所以做被叫为链表类。注意:LinkedList在集合中排序是无序的!!
2、Set集合(子类):Set类中常用的类有HashSet和TreeSet类(Set集合是指一个不包含重复元素的对象的集合!也就是说当集合内部储存了一个
元素时,将无法完成将元素再添加一次的操作,也无法将元素修改成和其他元素相同!!
)
*1HashSet类可创建一个无序的对象集合:
*2TreeSet对象中添加的元素必须是可以进行排序的!
二、Map接口 的使用
package JavaSum;
import java.util.LinkedHashMap;
import java.util.Scanner;
public class TestMap {
private LinkedHashMap<String,TestEmployee> emp=new LinkedHashMap<String,TestEmployee>();
private static TestMap tm=new TestMap();
private static Scanner sc=new Scanner(System.in);
public static void main(String[] args) {
// TODO Auto-generated method stub
boolean flag=false;
while(!flag){
System.out.println("----------------管理系统-----------------");
System.out.println("1、添加 2、删除、 3、替换 、4、查找 5、打印所有、 6、退出");
String choose=sc.next();
switch(choose){
case "1":
tm.add();
break;
case "2":
tm.delete();
break;
case "3":
tm.replace();
break;
case "4":
tm.findone();
break;
case "5":
tm.findall();
break;
case "0":
flag=true;
break;
default:
System.out.println("选择业务错误请从新输入");
break;
}
}
}
private void findall() {
System.out.println(emp);
}
private void findone() {
System.out.println("请输入要查询人的姓名");
String name=sc.next();
if(find(name)){
System.out.println(emp.get(name));
}
else{
System.out.println("查无此人,无法完成请求");
}
}
private void replace() {
System.out.println("请输入要替换的姓名");
String name = sc.next();
if(find(name)){
emp.remove(name);
add();
}
else{
System.out.println("查无此人,无法完成请求");
}
}
private void delete() {
System.out.println("请输入要删除的姓名");
String name=sc.next();
if(find(name)){
emp.remove(name);
}
else{
System.out.println("查无此人,无法删除。");
}
}
private boolean find(String name) {
if(emp.get(name)==null){
return false;
}
else{
return true;
}
}
private void add() {
String name;
String gender;
int age=0;
TestEmployee te =new TestEmployee();
System.out.println("姓名:");
name=sc.next();
te.setName(name);
System.out.println("姓别:");
gender=sc.next();
te.setGender(gender);
System.out.println("年龄:");
age=sc.nextInt();
te.setAge(age);
if(emp.get(te.getName())==null){
emp.put(te.getName(), te);
}
System.out.println("添加成功!");
System.out.println(te);
// System.out.println(emp.get(te.getName()));//用来显示指定键值中的存储值
}
}
import java.util.LinkedHashMap;
import java.util.Scanner;
public class TestMap {
private LinkedHashMap<String,TestEmployee> emp=new LinkedHashMap<String,TestEmployee>();
private static TestMap tm=new TestMap();
private static Scanner sc=new Scanner(System.in);
public static void main(String[] args) {
// TODO Auto-generated method stub
boolean flag=false;
while(!flag){
System.out.println("----------------管理系统-----------------");
System.out.println("1、添加 2、删除、 3、替换 、4、查找 5、打印所有、 6、退出");
String choose=sc.next();
switch(choose){
case "1":
tm.add();
break;
case "2":
tm.delete();
break;
case "3":
tm.replace();
break;
case "4":
tm.findone();
break;
case "5":
tm.findall();
break;
case "0":
flag=true;
break;
default:
System.out.println("选择业务错误请从新输入");
break;
}
}
}
private void findall() {
System.out.println(emp);
}
private void findone() {
System.out.println("请输入要查询人的姓名");
String name=sc.next();
if(find(name)){
System.out.println(emp.get(name));
}
else{
System.out.println("查无此人,无法完成请求");
}
}
private void replace() {
System.out.println("请输入要替换的姓名");
String name = sc.next();
if(find(name)){
emp.remove(name);
add();
}
else{
System.out.println("查无此人,无法完成请求");
}
}
private void delete() {
System.out.println("请输入要删除的姓名");
String name=sc.next();
if(find(name)){
emp.remove(name);
}
else{
System.out.println("查无此人,无法删除。");
}
}
private boolean find(String name) {
if(emp.get(name)==null){
return false;
}
else{
return true;
}
}
private void add() {
String name;
String gender;
int age=0;
TestEmployee te =new TestEmployee();
System.out.println("姓名:");
name=sc.next();
te.setName(name);
System.out.println("姓别:");
gender=sc.next();
te.setGender(gender);
System.out.println("年龄:");
age=sc.nextInt();
te.setAge(age);
if(emp.get(te.getName())==null){
emp.put(te.getName(), te);
}
System.out.println("添加成功!");
System.out.println(te);
// System.out.println(emp.get(te.getName()));//用来显示指定键值中的存储值
}
}
/*********************************TestEmployee Class*************************************************/
package JavaSum;
public class TestEmployee {
private String name;
private String gender;
private int age;
@Override
public String toString() {
return "TestEmployee [name=" + name + ", gender=" + gender + ", age="
+ age + "]";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
public class TestEmployee {
private String name;
private String gender;
private int age;
@Override
public String toString() {
return "TestEmployee [name=" + name + ", gender=" + gender + ", age="
+ age + "]";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}