Collection
Collection is an interface of collection class,which defines common methods in collection(集合共有的方法)
-|List(列表) (An Array)
Duplicate objects are allowed &It is sequential(有序的)
ArrayList
linear list structure(线性表结构)
Methods connected with 'get' are used frequently.
Pay attention to the ‘remove' method uesd in for-loop.
iterator
remove() & removeIf()
contains();
set();
class Person implements Comparable {
protected float score;
protected String name;
protected int age;
public Person(String name,float score,int age){
this.name = name;
this.score = score;
this.age = age;
}
@Override
public String toString() {
return "name:"+name+ " "+"score:" + score + " "+"age:"+age;
}
// Ascending order(升序)
@Override
public int compareTo(Person person) {
// age can be score as well
if(age > person.age){
return 1;
}else if(age == person.age){
return 0;
}
return -1;
}
// Descending order(降序)
/*public int compareTo(Person person) {
if(age > person.age){
return -1;
}else if(age == person.age){
return 0;
}
return 1;
}*/
}
public class MyClass {
public static void main(String[] args) {
// Create an array
// polymorphism
// The Collection Object can only save one same kind of data
List people = new ArrayList<>();
ArrayList personList = new ArrayList<>();
// Add an object at the end of array
//Person xw = new Person("小王",90,15);
personList.add(new Person("Jack",90,15));
personList.add(new Person("John",90,16));
personList.add(new Person("Merry",90,17));
// Add data in a certain position
personList.add(0,new Person("小王",90,14));
// Get one object
Person person = personList.get(0);
System.out.println(personList);
// Traverse this array
//1. for cyclic sentences
for (int i = 0; i < personList.size(); i++){
Person p = personList.get(i);
// ↓ Delete the corresponding object,but this exists problems
// ↓ And this normally won't be the method we want
//personList.remove(i);
// Delete one certain object
//personList.remove(p);
//System.out.println(p);
}
System.out.println("______for-each________");
// 2.The enhanced for-each loop(增强for循环)
for(Person p : personList){
// Fail
//personList.remove(p);
System.out.println(p);
}
System.out.println("_______iterator_______");
// 3.Ues Iterator (使用Iterator遍历器)
// Main difference: The object will be deleted when traversed
// Get the corresponding iterator object
Iterator iterator = personList.iterator();
// Ues hasNext() to judge whether exists another one object
// Use next() to get the next object
/* while(iterator.hasNext()){
Person p = iterator.next();
//personList.remove(p);
iterator.remove();
}*/
System.out.println("*********");
System.out.println(personList);
System.out.println("*****removeIf******");
// The Delete of the Data
// 1.Delete the data under certain condition
// Create a subclass object of Predicate Interface
//MyPredicate predicate = new MyPredicate();
//personList.removeIf(predicate);
//System.out.println("****removeIf***");
//System.out.println(personList);
// 2.Use a anonymous inner class(匿名内部类)
/*personList.removeIf(new Predicate() {
@Override
public boolean test(Person person) {
if(person.age > 15){
// If it is true ,this object will be deleted
return true;
}
return false;
}
});*/
// Sort(排序)
// 1.The defined class by oneself should implements the Comparable Interface ↑(Person class) and override compareTo method
//Collections.sort(personList);
personList.sort(new Comparator() {
@Override
public int compare(Person person, Person t1) {
// Make your own rules of comparison
return person.name.compareTo(t1.name);
}
});
System.out.println(personList);
// Get one object
Person person1 = personList.get(0);
// This is a new object.corresponding to a new person
person = new Person("John",90,16);
// Judge whether this object contains certain element
if (personList.contains(person)){
System.out.println("Here exist this person");
}else{
System.out.println("Here doesn't exist this person");
}
// Get index of certain object
System.out.println(personList.indexOf(person1));
// To change the index of certain object
personList.set(0,person1);
// Clear all data
personList.clear();
System.out.println(personList);
}
// Create a class to implement the methods in the Predicate Interface
// For the 'removeIf' method
static class MyPredicate implements Predicate {
@Override
public boolean test(Person person){
if(person.age > 15 ){
return true;
}
return false;
}
}
}
LinkList
chain list(链表结构)
Methods connected with 'add' & 'remove' methods are used fequently.
-|Set (A Collection)
Unordered(无序的)
Duplicate objects are not allowed.
HashSet method is used frequently.