摘要:本文主要向大家介绍了JAVA语言之集合,通过具体的内容向大家展示,希望对大家学习JAVA语言有所帮助。
本文主要向大家介绍了JAVA语言之集合,通过具体的内容向大家展示,希望对大家学习JAVA语言有所帮助。
内容:j集合顶层共性的方法,集合下的两个分支List和Set集合下的小分支——LinkedList、ArrayList、TreeSet、HashSet
在util包
###顶层的共性接口collection方法
//添加
Collection<String> c1 = new ArrayList<String>();
Collection<String> c2 = new ArrayList<String>();
c1.add("x");
c1.add("i");
c1.add("u");
c2.add("g");
c2.add("ha");
c2.add("ai");//添加所有c1.addAll(c2);//System.out.println(c1);
//删除相同的元素c1.removeAll(c2);//System.out.println(c1);
//删除不同的元素c1.retainAll(c2);
//获取长度c1.size()
//判断是否包含元素
c1.contains("xia") //调用的是对象的equals方法
//将集合转成数组.toArray()
//取出元素
Iterator<String> it = c1.iterator();while(it.hasNext()){
String x = it.next();
System.out.println(x);
}
开发中用的是forfor (Iterator<String> it = c1.iterator(); it.hasNext();) {
String s = (String) it.next();
System.out.println(s);
}
两种集合
List:有序(存入的顺序和取出的顺序一致)。有索引,允许重复元素
Set:不允许重复元素
###List特有方法
import java.util.List;
List<String> list = new ArrayList<String>();
list.add("abc1");
list.add("abc2");
list.add("abc3");
//索引插入元素
list.add(1,"haha");
//索引删除元素
list.remove(1);
//获取
System.out.println(list.get(1));
System.out.println(list.indexOf("abc3"));
//修改
list.set(1, "kk");
//索引取出for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
//列表迭代器,解决迭代器过程中的增删改查for (ListIterator<String> it = list.listIterator(); it.hasNext();) {
Object obj = it.next();
if(obj.equals("abc2")){
it.add("ai q");
}
}
list集合具体子类
Vector:数据结构是数组,可变长度的(不断new新数据并将元数组复制到新数组)。线程同步的。增删和查询都慢
ArrayList:也是数组结构,长度可变。线程不同步,代替了Vector。增删速度不快,查询速度快
LinkedList:链表结构,线程不同步。增删速度快,查询速度慢
存储自定义对象
对象的set、get方法快速创建,Alt+Shift+S
##ArrayList
存储个人的对象
ArrayList<Person_arraylist> al = new ArrayList<Person_arraylist>();
Person_arraylist p1 = new Person_arraylist("ia", 21);
Person_arraylist p2 = new Person_arraylist("u",20);
al.add(p1);
al.add(p2);
al.add(new Person_arraylist("ai", 7));
for (Iterator it = al.iterator(); it.hasNext();) {
Person_arraylist p = (Person_arraylist) it.next(); //需要向下转型,才能进去的时候是Object类 System.out.println(p.getName());
}
#去除重复对象练习
Person_arraylist类中的equals方法public boolean equals(Object obj){
if(this == obj){
return true;
}
if(!(obj instanceof Person_arraylist)){
throw new ClassCastException("类型错误");
}
Person_arraylist p = (Person_arraylist)obj;
return this.getName().equals(p.getName()) && this.getAge() == p.getAge();
public static void single_list(ArrayList<Person_arraylist> al){
ArrayList<Person_arraylist> tmp = new ArrayList<Person_arraylist>();
for (Iterator it = al.iterator(); it.hasNext();) {
Person_arraylist p = (Person_arraylist) it.next();
if(!tmp.contains(p)){ //通过contains调用Person_arraylist的比较方法,上面 tmp.add(p);
}
}
al.clear();
al.addAll(tmp);
}
##LinkedList
一般都是头尾操作
LinkedList<String> link = new LinkedList<String>();
link.addFirst("a");
link.addFirst("ai");
link.addFirst("iu");//获取元素,只是获取没有删除System.out.println(link.getFirst());//获取并且删除元素System.out.println(link.removeFirst());
判断是否为空:isEmpty()
###Set集合:不允许重复元素
和collection方法相同,只有一个迭代器
HashSet:哈希(散列)表结构
TreeSet:
##HashSet
Set<String> set = new HashSet<String>();
set.add("ia");
set.add("u");
set.add("ai");
set.add("pingpang");
set.add("xingfu");//只能通过迭代器取出for (Iterator it = set.iterator(); it.hasNext();) {
String str = (String) it.next();
System.out.println(str);
}
HashSet默认调用的是Object里面的hashcode()方法,一般保存对象都需要对象覆盖hashcode和equals方法
hashcode()方法,根据自身特点定义hash值,返回一个整数值,当返回的整数值相同就会调用equals方法进行比较。
equals()方法是为了解决hash冲突
要实现有序的话,使用LinkedHashSet对象
##TreeSet
1)第一种要求保存的对象要实现Comparable接口,实现compareTo方法。public int compareTo(Object o)
{
Student stu = (Student)o;
if(this.age > stu.age){
return 1;
}
if(this.age < stu.age){
return -1;
}
return 0;
} //简单的compareTo方法实现,或者直接return this.age-stu.age
public int compareTo(Object o)
{
Student stu = (Student)o;
return this.name.compareTo(stu.name);
}
TreeSet是根据compareTo方法返回的值来保证唯一性,所以单单使用一个没有办法保证不会出错,万一两个年龄相同姓名不同,或者姓名相同年林不同的,这样子会漏掉要保存的。
这里解决办法可以把return 0改成 return this.name.compareTo(stu.name)
2)第二种要求建立指定排序方式的
在创建集合对象的时候,参数使用实现comparator接口的一个比较对象public class Comparator_by_name implements Comparator<Student>
{
public int compare(Student o1, Student o2)
{
int tmp = o1.getName().compareTo(o2.getName());
return tmp==0?o1.getAge()-o2.getAge():tmp;
}
}
ArrayList:数组结构。数组查询快,list,可以重复
LinkedList:链表结构。增删快,***First ***Last ***为add get remove
HashSet:哈希表,查询速度更快,不保证有序。
LinkedHashSet:链表+哈希表。可以实现有序
TreeSet:二叉树,可以排序。两种比较方式:一种是自然排序Comparable,一种是比较器Comparator
##foreach语句:for循环增强型
for(元素类型 变量:collection容器or数组){}
Collection<String> coll=new ArrayList<String>();
coll.add("a");
coll.add("ai");
coll.add("q");
coll.add("xa");for(String s:coll){
System.out.println(s);
}
###############################
//作业:List集合中存储的重复字符串元素,按照长度进行排序。
View Code
#############################
//在一个已排序的List集合中插入一个字符串,继续保证这个集合原有排序,问如何获取这个位置。
View Code
本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注编程语言JAVA频道!
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号