黑马程序员—java基础_集合List和Set

------- android培训java培训、期待与您交流! ----------

1.List特有方法:凡是可以操作角标的方法都是该体系的特有方法

增:add(index,elements);addAll(index,Collection)

删:remove(index)

改:set(index,elements)

查:get(index);subList(from,to);listIterator();int indexOf(obj)

List集合特有的迭代器:ListIterator是Iterator的子接口。在迭代时,不可以通过集合对象的方法操作集合中的元素,所以在迭代器时,只能用迭代器的方法操作元素,Iterator方法是有限的,只能对元素进行判断、取出和删除操作。

如果想要其他的操作如添加,修改等,需要使用其子接口:ListIterator,该接口只能通过List集合的ListIterator方法获取。

2.LinkList特有方法:

addFirst();addLast()

getFirst();getLast():获取元素,不删除元素,元素不存在出现NoSuchElementException

removeFirst();removeLast():获取元素,并删除元素,元素不存在出现NoSuchElementException

JDK1.6版本后出现替代方法:

offerFirst();offerLast()

peekFirst();peekLast()获取元素,不删除元素,如果集合没有元素,会返回null

pollFirst();pollLast()获取元素,并删除元素,如果集合没有元素,会返回null

3.ArrayList练习:去除ArrayList集合中的重复元素

import java.util.ArrayList;
import java.util.Iterator;

public class ArrayListTest {

	/**
	 * 去除ArrayList集合中的重复元素。
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ArrayList<String> al=new ArrayList<String>();
		al.add("java01");
		al.add("java03");
		al.add("java02");
		al.add("java02");
		al.add("java03");
		al.add("java01");
		
		al=singleElement(al);//调用方法
		//使用迭代器打印去除重复元素后的集合
		
		for(Iterator<String> it=al.iterator();it.hasNext();)
			System.out.println(it.next());
		
	}
	//定义功能去除集合中的重复元素
	public static ArrayList<String> singleElement(ArrayList<String> al)
	{
		ArrayList<String> newAl=new ArrayList<String>();//定义临时集合存储去除后的集合
		//使用迭代器
		Iterator<String> it=al.iterator();
		while (it.hasNext())
		{
			String str=it.next();
			if(!newAl.contains(str))//判断newA1集合里面的元素是否存在,若不存在就添加
				newAl.add(str);
		}
		return newAl;
	}

}

4.LinkList练习:模拟队列的数据结构

import java.util.*;
public class LinkListTest {

	/**
	 * 使用LinkedList模拟一个队列数据结构。
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		DuiLie dl=new DuiLie();
		dl.myAdd("java01");
		dl.myAdd("java02");
		dl.myAdd("java03");
		dl.myAdd("java04");
		dl.myAdd("java05");
		
		while(!dl.isNull())
			System.out.println(dl.myGet());

	}
	
}
//定义一个类实现队列的数据结构:先进先出
class DuiLie{
	private LinkedList link;//新建一个LinkList集合
	DuiLie(){
		link=new LinkedList();//构造函数一初始化就有一个LinkList集合
	}
	public void myAdd(Object obj)
	{
		link.addLast(obj);//添加对象,依次在对象后面添加
	}
	public Object myGet()
	{
		return link.removeFirst();//队列先进,先取出
	}
	public boolean isNull()
	{
		return link.isEmpty();//判断link的对象是否为空
	}
}
5.HashSet练习:往HashSet集合存入自定义对象,姓名和年龄相同为同一人,重复元素。
class HashSetTest 
{
	
	public static void main(String[] args) 
	{
		HashSet hs = new HashSet();

		hs.add(new Person("a1",11));
		hs.add(new Person("a2",12));
		hs.add(new Person("a3",13));
		hs.add(new Person("a2",12));
		hs.add(new Person("a4",14));		

		Iterator it = hs.iterator();

		while(it.hasNext())
		{
			Person p = (Person)it.next();//强转,需要用到Person类的方法
			System.out.println(p.getName()+"::"+p.getAge());
		}
	}
}
//新建人类
class Person
{
	private String name;
	private int age;
	Person(String name,int age)
	{
		this.name = name;
		this.age = age;
	}
	
	public int hashCode()//复写hashCode方法,HashSet集合判断唯一性是根据hashCode方法
	{
		System.out.println(this.name+"....hashCode");
		return name.hashCode()+age*37;//判断姓名字符串的hashCode是否相同
	}

	public boolean equals(Object obj)//若hashCode方法判断相同,再调用equals方法判断
	{

		if(!(obj instanceof Person))//判断是否为Person类
			return false;

		Person p = (Person)obj;
		System.out.println(this.name+"...equals.."+p.name);

		return this.name.equals(p.name) && this.age == p.age;//姓名和年龄想同才为重复元素
	}

	
	public String getName()
	{
		return name;
	}
	public int getAge()
	{
		return age;
	}
}
6.TreeSet:

可以对Set集合中的元素进行排序,底层数据是二叉树,保证元素唯一性的依据:compareTo方法 return 0。

TreeSet排序的第一种方式:让元素自身具备比较性,元素需要实现Comparable接口,覆盖compareTo方法,即元素的自然顺序

TreeSet的第二种排序方式:当元素自身不具备比较时,或者具备的比较性不是所需的。这时需要让集合自身具备比较性,可以定义比较器,将比较器对象作为参数传递给TreeSet集合的构造函数。定义一个类,实现Comparator接口,覆盖compare方法

练习1:在TreeSet集合中存储自定义对象学生。按照学生的年龄进行比较

class TreeSetDemo 
{
	public static void main(String[] args) 
	{
		TreeSet ts = new TreeSet();

		ts.add(new Student("lisi02",22));
		ts.add(new Student("lisi007",20));
		ts.add(new Student("lisi09",19));
		ts.add(new Student("lisi08",19));
		ts.add(new Student("lisi007",20));
		ts.add(new Student("lisi01",40));

		Iterator it = ts.iterator();
		while(it.hasNext())
		{
			Student stu = (Student)it.next();//强转
			System.out.println(stu.getName()+"..."+stu.getAge());
		}
	}
}


class Student implements Comparable//该接口强制让学生具备比较性,实现Comparable接口
{
	private String name;
	private int age;

	Student(String name,int age)
	{
		this.name = name;
		this.age = age;
	}

	public int compareTo(Object obj)//覆盖compareTo方法
	{
		
		if(!(obj instanceof Student))//判断是否为学生类
			throw new RuntimeException("不是学生对象");//抛出RuntimeException异常,函数不用声明
		Student s = (Student)obj;

		if(this.age>s.age)
			return 1;
		if(this.age==s.age)//年龄相同要定义次要方法
		{
			return this.name.compareTo(s.name);//字符串类的compareTo比较姓名是否相同。
		}
		return -1;
	}

	public String getName()
	{
		return name;

	}
	public int getAge()
	{
		return age;
	}
}
练习2:按照字符串长度排序,使用比较器

import java.util.*;
class  TreeSetTest
{
	public static void main(String[] args) 
	{
		TreeSet ts = new TreeSet(new StrLenComparator());//传入自定义的比较器

		ts.add("abcd");
		ts.add("cc");
		ts.add("cba");
		ts.add("aaa");
		ts.add("z");
		ts.add("hahaha");

		Iterator it = ts.iterator();

		while(it.hasNext())
		{
			System.out.println(it.next());
		}
	}
}

class StrLenComparator implements Comparator//自定义比较器实现Comparator接口
{
	public int compare(Object o1,Object o2)//覆盖compare方法
	{
		String s1 = (String)o1;
		String s2 = (String)o2;

		int num = new Integer(s1.length()).compareTo(new Integer(s2.length()));//字符串长度进行比较
		if(num==0)
			return s1.compareTo(s2);//次要比较按照字符串自然顺序进行比较

		return num;
	}
}
7.泛型:JDK1.5版本以后出现新特性,用于解决安全问题。

好处:1.将运行时期出现的问题ClassCastException转移到编译时期。2.避免强制转换的麻烦

格式:通过<>来定义要操作的引用数据类型,当使用集合时,将集合中要存储的数据类型作为参数传递到<>中,

ArrayList<String> al = new ArrayList<String>();
泛型类定义的泛型,在整个类中有效。如果被方法使用,那么泛型类的对象明确要操作的具体类型后,所有要操作的类型就已经固定了。为了让不同方法可以操作不同类型,而且类型还不确定那么可以将泛型定义在方法上。? extends E: 可以接收E类型或者E的子类型。上限。? super E: 可以接收E类型或者E的父类型。下限

class Demo<T>;public <Q> void print(Q q)



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值