Collection、List、set
- collection接口中的实现类ArrayList
- List接口中的实现类ArrayList,linkedList,List
一些方法:add、remove、Iterator(迭代)、contains(判断是否包含)isEmpty(判断是否为空) - 泛型
泛型类Geberic:书写方式:public class a{};1.可以为数据类型创建变量。2.可以作为方法入参3.可以作为方法的返回值
泛型接口:书写方式:public interface MyGenericjk {},不能创建泛型静态常量
泛型方法:书写方式:public void show(){} - Set集合中实现类:继承colllection方法,hashSet、treeSet
- Comparator接口(定制比较器)在匿名内部类中定制规则。
- Map集合中的实现方法:HashMap,TreeMap
小结:集合的概念:对象的容器,和数组类似,定义对多个对象进行操作的常用方法
List集合:有序、有下标、元素可以重复(ArrayList、LinkedList、Vector)
Set集合:无序、无下标、元素不可以重复、(HashSet、TreeSet)(linkedHashSet,是有序的)
Map集合:存储一对数据,无序、无下标、键不可以重复、值可以重复(HashMap、HashTable、TreeMap)
Colletions:集合工具类,定义除了存取以外的集合常用方法
1. Collection collection=new ArrayList();
package com.f.www.Gather;
import java.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class Demo01 {
public static void main(String[] args) {
Collection collection=new ArrayList();
collection.add("苹果");
collection.add("香蕉");
collection.add("梨");
System.out.println("元素个数:"+collection.size());
System.out.println(collection);
collection.remove("梨");
System.out.println("元素个数:"+collection.size());
System.out.println(collection);
collection.clear();
System.out.println("元素个数:"+collection.size());
for (Object o:collection
) {
System.out.println("元素个数:"+collection.size());
System.out.println(o);
}
System.out.println("--------迭代器————————");
Iterator iterator = collection.iterator();
while (iterator.hasNext()){
String s=(String)iterator.next();
iterator.remove();
}
System.out.println("元素个数:"+collection.size());
System.out.println(collection.contains("西瓜"));
System.out.println(collection.isEmpty());
Collection c=new ArrayList<>();
Student s1=new Student("张三",20);
Student s2=new Student("张四",21);
Student s3=new Student("张无",22);
collection.add(s1);
collection.add(s2);
collection.add(s3);
System.out.println("元素个数"+collection.size());
System.out.println(collection.toString());
System.out.println("元素个数:"+collection.size());
collection.remove(new Student("张三",20) );
System.out.println();
}
}
2. List list1=new ArrayList<>();
package com.f.www.Gather;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
public class ListDemo {
public static void main(String[] args) {
List list1=new ArrayList<>();
list1.add("苹果");
list1.add("小米");
list1.add("华为");
System.out.println("元素个数:"+list1.size());
System.out.println(list1.toString());
list1.remove("苹果");
list1.remove(0);
System.out.println("删除之后"+list1.size());
System.out.println(list1.toString());
for (int i = 0; i < list1.size(); i++) {
System.out.println(list1.get(i));
}
for (Object o:list1
) {
System.out.println(o);
}
Iterator it =list1.iterator();
while (it.hasNext()){
System.out.println(it.next());
}
ListIterator lit=list1.listIterator();
System.out.println("----从前往后迭代------");
while (lit.hasNext()){
System.out.println(lit.nextIndex()+":"+lit.next());
}
System.out.println("----从后往前迭代------");
while (lit.hasPrevious()){
System.out.println(lit.previousIndex()+":"+lit.previous());
}
System.out.println(list1.contains("苹果"));
System.out.println(list1.isEmpty());
System.out.println(list1.indexOf("苹果"));
}
}
3. LinkedList linkedList=new LinkedList();
package com.f.www.Gather;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.ListIterator;
public class LinkedListDemo {
public static void main(String[] args) {
LinkedList linkedList=new LinkedList();
Student s1=new Student("张三",20);
Student s2=new Student("张四",21);
Student s3=new Student("张无",22);
linkedList.addLast(s1);
linkedList.addLast(s2);
linkedList.addLast(s3);
System.out.println("元素个数:"+linkedList.size());
System.out.println(linkedList.toString());
linkedList.remove(new Student("张三",20));
System.out.println("删除之后:"+linkedList.size()+linkedList.toString());
System.out.println("------- for-----");
for (int i = 0; i < linkedList.size(); i++) {
System.out.println(linkedList.get(i));
}
System.out.println("------- 使用增强for-----");
for (Object o:linkedList
) {Student student=(Student)o;
System.out.println(student.toString());
}
System.out.println("------- 使用迭代器-----");
Iterator it= linkedList.iterator();
while (it.hasNext()){
Student student=(Student)it.next();
System.out.println(student.toString());
}
System.out.println("--------使用列表迭代器-------");
ListIterator listIterator= linkedList.listIterator();
while (listIterator.hasNext()){
Student student=(Student)listIterator.next();
System.out.println(student.toString());
}
System.out.println(linkedList.contains("张三"));
System.out.println(linkedList.isEmpty());
System.out.println(linkedList.indexOf(s1));
}
}
4. ArrayList arrayList=new ArrayList();
package com.f.www.Gather;
import jichuyufa.Array.Arrayslei;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
public class ListRealize {
public static void main(String[] args) {
ArrayList arrayList=new ArrayList();
Student s1=new Student(" 老聂",25);
Student s2=new Student(" 老郑",30);
Student s3=new Student(" 老刘",22);
arrayList.add(s1);
arrayList.add(s2);
arrayList.add(s3);
System.out.println("元素个数:"+arrayList.size());
System.out.println(arrayList.toString());
System.out.println("删除之后元素个数:"+arrayList.size());
System.out.println("删除之后:"+arrayList.toString());
System.out.println("---------使用迭代器---------");
ListIterator l=arrayList.listIterator();
while(l.hasNext()){
Student s =(Student)l.next();
System.out.println(s.toString());
}
System.out.println("---------使用列表迭代器-------");
ListIterator li= arrayList.listIterator();
while (li.hasNext()){
Student s11=(Student)li.next();
System.out.println(s11.toString());
}
System.out.println("-----使用列表迭代器逆序-------");
while (li.hasPrevious()){
Student s11=(Student)li.previous();
System.out.println(s11.toString());
}
System.out.println(arrayList.contains(new Student("老聂",25)));
System.out.println(arrayList.isEmpty());
System.out.println(arrayList.indexOf(s1));
}
}
5.泛型类
package com.f.www.Gather;
public class MyGeneric <T>{
T t;
public void show(T t){
System.out.println(t
);
}
public T getT() {
return t;
}
}
6.泛型接口
package com.f.www.Gather;
public interface MyGenericjk<T> {
String name="张三";
T s(String string);
}
7.泛型接口的实现类
package com.f.www.Gather;
import javax.print.DocFlavor;
public class MyGennericImpl<T> implements MyGenericjk<DocFlavor.STRING> {
@Override
public DocFlavor.STRING s(String string) {
System.out.println(string);
return null;
}
}
}
8.泛型方法
package com.f.www.Gather;
public class MyGenericMethod {
public <T>T show(T t){
System.out.println("泛型方法"+t);
return t;
}
}
9. TestGenneric测试泛型
package com.f.www.Gather;
public class TestGenneric {
public static void main(String[] args) {
MyGeneric<String>myGeneric=new MyGeneric<String>();
myGeneric.t="hello";
System.out.println(myGeneric.t);
myGeneric.show("hello1");
String string=myGeneric.getT();
System.out.println(string);
System.out.println("----泛型接口使用-----");
MyGennericImpl impl=new MyGennericImpl();
impl.s("xxxxxx");
System.out.println("---------泛型方法------");
MyGenericMethod myGenericMethod=new MyGenericMethod();
myGenericMethod.show("中国加油");
myGenericMethod.show(1);
}
}
10.vector集合使用
package com.f.www.Gather;
import java.util.Enumeration;
import java.util.Vector;
public class Vetoruse {
public static void main(String[] args) {
Vector vector=new Vector();
vector.add("草莓");
vector.add("苹果");
vector.add("香蕉");
System.out.println("元素个数:"+vector.size());
System.out.println(vector.toString());
vector.remove(0);
vector.remove("苹果");
vector.clear();
Enumeration enumeration=vector.elements();
while (enumeration.hasMoreElements()){
String o=(String) enumeration.nextElement();
System.out.println(o);
}
System.out.println(vector.contains("苹果"));
System.out.println(vector.isEmpty());
vector.firstElement();
vector.lastElement();
vector.get(5);
}
}
11.用到的类Student类
package com.f.www.Gather;
import java.util.Objects;
public class Student implements Comparable<Student> {
private String name;
private int age;
public String getName() {
return name;
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public Student setName(String name) {
this.name = name;
return this;
}
public int getAge() {
return age;
}
public Student setAge(int age) {
this.age = age;
return this;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
@Override
public boolean equals(Object o) {
if(this==o){
return true;
}
if (o==null){
return false;
}
if (o instanceof Student){
Student s=(Student)o;
if (this.name.equals((s.getName()))&&this.age==s.getAge());
return true;
}
return false;
}
@Override
public int hashCode() {
int n1=this.name.hashCode();
int n2=this.age+31;
return n1+n2;
}
@Override
public int compareTo(Student student) {
int n1=this. getName().compareTo(student.getName());
int n2=age-student.age ;
return n1==0?n2:n1;
}
}