单列集合Collection接口
生活随笔
收集整理的這篇文章主要介紹了
单列集合Collection接口
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
集合是可以存任何類型對象,并且長度可變的類,使用時要導包,java.util.~,不知道使用什么包時可以使用通配符導出所有包,java.util.*;
集合分為單口集合Collction和雙口集合Map;
單口集合Collection有兩個重要子接口List和Set;
List的特點是元素有序,可重復,主要實現類有ArrayList和LinkedList;
Set的特點是元素無序,不可重復,主要實現類有HashSet和TreeSet;
雙口集合是用鍵值對來存儲數據,key--value,主要實現類有HashMap和TreeMap;
Collection是散列值,Set是二叉樹;
Collection接口
Collection是所有單口集合的父接口;
方法見Csdn;
List接口:
LIst subList(int fromindex,int toindex); 獲取從fromindex到toindex的元素集合;
ArrayList集合:
ArrayList是長度可變的數組集合,add()和get()方法實現數組元素的添加和獲取:
package DAAN1;
import java.util.ArrayList;
import java.util.Collection;
public class DIAN {
public static void main(String[] args) {
// TODO 自動生成的方法存根
ArrayList list=new ArrayList();
list.add("李星沐");
list.add("李星星");
list.add("李沐沐");
System.out.println(list.size());
System.out.println(list.get(1));
}
}
3
李星星
ArrayList適合查詢元素;
LinkedList集合:
LinkedList集合是雙向循環鏈表,適合集合元素的添加和刪除;
package DAAN1;
import java.util.LinkedList;
import java.util.*;
public class DIAN {
public static void main(String[] args) {
// TODO 自動生成的方法存根
LinkedList list=new LinkedList();
list.add("李星沐");
list.add("李星星");
list.add("李沐沐");
list.addFirst("firat");
list.addLast("last");
System.out.println(list.getFirst());
System.out.println(list.toString());
}
}
firat
[firat, 李星沐, 李星星, 李沐沐, last]
Iterator接口:
Iterator是迭代器,用于遍歷元素;
Iterator it=list.iterator();? ?//創建迭代變量
while(it.hasNext()) {? ? ? //看是否為空
Object obj=it.next();
System.out.println(obj);
foreach也是迭代器,
for(Object obj1:list) {
System.out.println(obj1);
}
HashSet集合:
使用構造方法時要重寫hashCode()和equals()方法;
package example01;
import java.util.*;
class Student() {
private String name;
private String id;
public Student(String id,String name) {
this.id=id;
this.name=name;
}
public String toString() {
return id+":"+name;
}
public int hashCode() {
return id.hashCode();
}
public boolean equal(Object obj) {
if(this==obj) {
return true;
}
if(!(obj instanceof Student)) {
return false;
}
Student stu=(Student)obj;
boolean b=this.id.equals(stu.id);
return b;
}
}
public class sss
public static void main(String[] args) {
// TODO 自動生成的方法存根
HashSet ha=new HashSet();
Student stu1=new Student("1","李");
Student stu2=new Student("2","星");
Student stu3=new Student("3","沐");
ha.add(stu1);
ha.add(stu2);
ha.add(stu3);
System.out.println(ha);
}
}
TreeSet集合:
TreeSet集合可以實現元素的牌序:
自然排序法:要繼承Comparable接口并重寫compareTo方法;
package example01;
import java.util.*;
class Student() implements Comparable<Student>{
private String name;
private String id;
public Student(String id,String name) {
this.id=id;
this.name=name;
}
public String toString() {
return id+":"+name;
}
@Override
public int compareTo(Student o) {
return 0;
return 1;
return -1;
}
}
public class sss {
public static void main(String[] args) {
// TODO 自動生成的方法存根
TreeSet ha=new TreeSet();
Student stu1=new Student("1","李");
Student stu2=new Student("2","星");
Student stu3=new Student("3","沐");
ha.add(stu1);
ha.add(stu2);
ha.add(stu3);
System.out.println(ha);
}
}
比較器排序:要繼承Comparator接口并重寫compare方法:
package example01;
import java.util.*;
class Student() implements Comparable<Student>{
private String name;
private String id;
public Student(String id,String name) {
this.id=id;
this.name=name;
}
public String toString() {
return id+":"+name;
}
}
public class sss {
public static void main(String[] args) {
// TODO 自動生成的方法存根
TreeSet ha=new TreeSet(new Comparator() {? ? ? ?//使用匿名內部類方法繼承? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?Comparator接口并重寫? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?compara方法;
@Override
public int compara(Object o1,Object o2) {
return -1;
????????????????????}
?????????????});
Student stu1=new Student("1","李");
Student stu2=new Student("2","星");
Student stu3=new Student("3","沐");
ha.add(stu1);
ha.add(stu2);
ha.add(stu3);
System.out.println(ha);
}
} 集合是可以存任何類型對象,并且長度可變的類,使用時要導包,java.util.~,不知道使用什么包時可以使用通配符導出所有包,java.util.*; 集合分為單口集合Collection和雙口集合Map; 單口集合Collection有兩個重要子接口List和Set; List的特點是元素有序,可重復,主要實現類有ArrayList和LinkedList; Set的特點是元素無序,不可重復,主要實現類有HashSet和TreeSet; 雙口集合是用鍵值對來存儲數據,key--value,主要實現類有HashMap和TreeMap; Collection是散列值,Set是二叉樹; Collection接口 Collection是所有單口集合的父接口; 方法見Csdn; List接口: LIst subList(int fromindex,int toindex); 獲取從fromindex到toindex的元素集合; ArrayList集合: ArrayList是長度可變的數組集合,add()和get()方法實現數組元素的添加和獲取: package DAAN1; import java.util.ArrayList; import java.util.Collection; public class DIAN { public static void main(String[] args) { // TODO 自動生成的方法存根 ArrayList list=new ArrayList(); list.add("李星沐"); list.add("李星星"); list.add("李沐沐"); System.out.println(list.size()); System.out.println(list.get(1)); } } 3 李星星 ArrayList適合查詢元素; LinkedList集合: LinkedList集合是雙向循環鏈表,適合集合元素的添加和刪除; package DAAN1; import java.util.LinkedList; import java.util.*; public class DIAN { public static void main(String[] args) { // TODO 自動生成的方法存根 LinkedList list=new LinkedList(); list.add("李星沐"); list.add("李星星"); list.add("李沐沐"); list.addFirst("firat"); list.addLast("last"); System.out.println(list.getFirst()); System.out.println(list.toString()); } } firat [firat, 李星沐, 李星星, 李沐沐, last] Iterator接口: Iterator是迭代器,用于遍歷元素; Iterator it=list.iterator();? ?//創建迭代變量 while(it.hasNext()) {? ? ? //看是否為空 Object obj=it.next(); System.out.println(obj); foreach也是迭代器, for(Object obj1:list) { System.out.println(obj1); } HashSet集合: 使用構造方法時要重寫hashCode()和equals()方法; package example01; import java.util.*; class Student() { private String name; private String id; public Student(String id,String name) { this.id=id; this.name=name; } public String toString() { return id+":"+name; } public int hashCode() { return id.hashCode(); } public boolean equal(Object obj) { if(this==obj) { return true; } if(!(obj instanceof Student)) { return false; } Student stu=(Student)obj; boolean b=this.id.equals(stu.id); return b; } } public class sss public static void main(String[] args) { // TODO 自動生成的方法存根 HashSet ha=new HashSet(); Student stu1=new Student("1","李"); Student stu2=new Student("2","星"); Student stu3=new Student("3","沐"); ha.add(stu1); ha.add(stu2); ha.add(stu3); System.out.println(ha); } } TreeSet集合: TreeSet集合可以實現元素的牌序: 自然排序法:要繼承Comparable接口并重寫compareTo方法; package example01; import java.util.*; class Student() implements Comparable<Student>{ private String name; private String id; public Student(String id,String name) { this.id=id; this.name=name; } public String toString() { return id+":"+name; } @Override public int compareTo(Student o) { return 0; return 1; return -1; } } public class sss { public static void main(String[] args) { // TODO 自動生成的方法存根 TreeSet ha=new TreeSet(); Student stu1=new Student("1","李"); Student stu2=new Student("2","星"); Student stu3=new Student("3","沐"); ha.add(stu1); ha.add(stu2); ha.add(stu3); System.out.println(ha); } } 比較器排序:要繼承Comparator接口并重寫compare方法: package example01; import java.util.*; class Student() implements Comparable<Student>{ private String name; private String id; public Student(String id,String name) { this.id=id; this.name=name; } public String toString() { return id+":"+name; } } public class sss { public static void main(String[] args) { // TODO 自動生成的方法存根 TreeSet ha=new TreeSet(new Comparator() {? ? ? ?//使用匿名內部類方法繼承? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?Comparator接口并重寫? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?compara方法; @Override public int compara(Object o1,Object o2) { return -1; ????????????????????} ?????????????}); Student stu1=new Student("1","李"); Student stu2=new Student("2","星"); Student stu3=new Student("3","沐"); ha.add(stu1); ha.add(stu2); ha.add(stu3); System.out.println(ha); } }
總結
以上是生活随笔為你收集整理的单列集合Collection接口的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 我的Qt作品(5)使用Qt+Halcon
- 下一篇: SDUT实验六7-6 sdut- C语言