java 集合框架

collection

 ----- LIst

---ArrayList

---LinkedList

---Vector

------Set 

----HashSet

----TreeSet


ArrayList 的基本用法

import java.util.*;
class ArrayListDemo
{
	public static void main(String[] args)
	{
		ArrayList al = new ArrayList();
		al.add("haha");
		al.add("hello list");
		/*
		Iterator it = al.iterator();
		while(it.hasNext()) {
			System.out.println(it.next());
		}*/
		//for 循环节省内存;
		for(Iterator it = al.iterator();it.hasNext();){
			System.out.println(it.next());
		}
		
		//ListIterator 接口对象比 iterator 对象具有更多的方法;但是只有list 对象有;
		ListIterator all = al.listIterator();
		while(all.hasNext())
		{
			Object obj = all.next();
			if(obj.equals("haha")){
				all.set("fasfdsaf");
			}
		}
		print(al);
	}
	
	public static void print(Object obj)
	{
		System.out.println(obj);
	}
}

LinkedList 的基本用法

//添加一个person 对象中 并且 除去重复的
import java.util.*;
class Person
{
	private int age;
	private String name;
	Person(int age, String name)
	{
		this.age = age;
		this.name = name;
	}
	public int sayAge()
	{
		return age;
	}
	
	public String sayName()
	{
		return name;
	}
	
	public boolean equals(Object obj)
	{
		if(!(obj instanceof Person)){
			return false;
		}
		Person p = (Person) obj;
		return p.name == name && p.age == age;
	}
}

class linkListDemo
{
	public static void main(String[] args)
	{
		ArrayList al = new ArrayList();
		al.add(new Person(12,"tang"));
		al.add(new Person(12,"tang"));
		al.add(new Person(13,"wang"));
		al = singleElement(al);
		Iterator it = al.iterator();
		while(it.hasNext()){
			Person p = (Person) it.next();
			sop(p.sayName()+"----"+p.sayAge());
		}
	}
	
	public static void sop(Object obj)
	{
		System.out.println(obj);
	}
	
	public static ArrayList singleElement(ArrayList al)
	{
		ArrayList newal = new ArrayList();
		Iterator it = al.iterator();
		while(it.hasNext())
		{
			Object obj = it.next();
			if (!newal.contains(obj)) {
				newal.add(obj);
			}
		}
		return newal;
	}
}


vector的基本用法

import java.util.*;
class VectorDemo
{
	public static void main(String[] args){
		Vector vc = new Vector();
		vc.add("val1");
		vc.add("val2");
		//其实和迭代器一样;
		Enumeration enums = vc.elements();
		while(enums.hasMoreElements()) {
			System.out.println(enums.nextElement());
		}
	}
	
}


HashSet的基本用法

import java.util.*;
class Person
{
	private int age;
	private String name;
	Person(int age, String name)
	{
		this.age = age;
		this.name = name;
	}
	
	public int getAge()
	{
		return this.age;
	}
	
	public String getName()
	{
		return this.name;
	}
	
	public int hashCode()
	{
		System.out.println(this.name+"----hashCode");
		return this.name.hashCode()+this.age*37;
	}
	
	public boolean equals(Object obj)
	{
		if(!(obj instanceof Person)){
			return false;
		}
		System.out.println(this.name+"----equals");
		Person p = (Person) obj;
		return this.name.equals(p.getName()) && this.age == p.getAge();
	}
}
class HashSetTest
{
	public static void main(String[] args)
	{
		HashSet hs = new HashSet();
		hs.add(new Person(11,"a1"));
		hs.add(new Person(12,"a2"));
		hs.add(new Person(13,"a3"));
		hs.add(new Person(13,"a3"));
		Iterator it = hs.iterator();
		while(it.hasNext())
		{
			Person p = (Person) it.next();
			sop(p.getName()+"---"+p.getAge());
		}
	}
	public static void sop(Object obj)
	{
		System.out.println(obj);
	}
}


TreeSet的基本用法

import java.util.*;
import java.lang.*;
class Person implements Comparable
{
	public int age;
	public String name;
	
	Person(int age, String name)
	{
		this.age = age;
		this.name = name;
	}

	public int compareTo(Object obj)
	{
		if(!(obj instanceof Person)){
			throw new RuntimeException("非 Person 对象");
		}
		Person p = (Person) obj;
		if(this.age > p.age) {
			return 1;
		}
		if(this.age == p.age){
			if(this.name.hashCode() > p.name.hashCode()){
				return 1;
			}
			if(this.name.hashCode() < p.name.hashCode()){
				return -1;
			}
			if(this.name.hashCode() == p.name.hashCode()){
				return 0;
			}
		}
		if(this.age < p.age){
			return -1;
		}
		return 0;
	}
	
	public int getAge()
	{
		return this.age;
	}
	
	public String getName()
	{
		return this.name;
	}
}

class TreeSetDemo
{
	public static void main(String[] args)
	{
		TreeSet ts = new TreeSet();
		ts.add(new Person(13,"a3"));
		ts.add(new Person(12,"a2"));
		ts.add(new Person(11,"a1"));
		ts.add(new Person(11,"a5"));
		Iterator it = ts.iterator();
		while(it.hasNext())
		{
			Person p = (Person) it.next();
			sop(p.getName()+"---"+p.getAge());
		}
		
	}
	
	public static void sop(Object obj)
	{
		System.out.println(obj);
	}
}





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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值