栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

单列集合Collection接口

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

单列集合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{         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{         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{         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{         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);        } }
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/351236.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号