java集合

 

 具体实例代码展示

/**
 * 
 */
package com.zhiyou.P;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import java.util.Vector;

/**
 * @author Administrator
 *
 */
public class CollectionTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		aboutCollection();
//		list 有序  重复
		aboutList();
		aboutStandardList();
		aboutLinkedList();
		aboutVector();
		
//		set  不可重复
//		HashSet<E>
//		TreeSet<>
		
		aboutHashSet();
		TreeSet();
		
		aboutTreeSetPhone();
		
		
//		其他的集合中存放Phone对象要不要实现Comparable接口?
	}
	private static void aboutTreeSetPhone() {
		Set<Phone> phoneSet = new TreeSet<>();
//		放置在集合中的类,所对应的类必须实现Comparable接口
		phoneSet.add(new Phone("xiaomi",799));
		phoneSet.add(new Phone("apple",299));
		phoneSet.add(new Phone("apple",299));
		phoneSet.add(new Phone("apple",299));
		System.out.println(phoneSet);
		
	}
	private static void TreeSet() {
		Set<String> treeSet = new TreeSet<>();
		treeSet.add("java");
		treeSet.add("java");
		treeSet.add("123");
		treeSet.add("456");
		treeSet.add("");
		System.out.println(treeSet);
		
//		treeSet与hashSet的区别
//		1:hashSet  无序 
//		treeSet有序(按照ASCII值排序),但是把第一个添加的对象放到set的末尾
//		treeSet不能放null,hashSet可以放null
	}
	private static void aboutHashSet() {
		Set<String> hashSet = new HashSet<>();
		hashSet.add("java");
		hashSet.add("eclipse");
		hashSet.add("java");
		hashSet.add("");
		hashSet.add(null);
		System.out.println(hashSet);
//		hashSet  无序  不重复
		
//		Set使用和List一样
//		hashSet.size();
	}
	
//	面试题:ArrayList与Vector的区别
//	1:都是继承与同一个AbstractList
//	2:ArrayList   jdk1.2  非线程安全  (单线程推荐使用) 性能高  支持输出
//	iterator,listIterator
//	vector  jdk 1.0  线程安全    性能相对而言较低。支持输出  foreach
//	ListIterator,Enumeration,内部使用数组存放对象
	
	
	private static void aboutCollection() {
//		1  collection  集合
//		也属于容器类 
//		存放的都是引用类型
//		数组是定长的  一旦创建长度就固定  string
//		集合不定长  StringBuilder StringBuffer

//		collect 接口   
//		List   接口 而且是collection  子接口
		Collection collection = new ArrayList<>();

//		add  添加想要的东西
		collection.add("laosun");
		collection.add(new Date());
//		100   int 转换成integer类型
		collection.add(100);

//		size  集合中对象的个数
		System.out.println(collection.size());

//		remove  移除  与add对应
		collection.remove("laosun");
		System.out.println(collection.size());

//		removeAll
//		collection.removeAll(c)

//		判断集合是否为空
		System.out.println(collection.isEmpty());

//		判断是否包含某一对象
		System.out.println(collection.contains(100));

//		清空 all  of  elements
//		只是清空集合中的内容,集合不会被删除
		collection.clear();
		System.out.println(collection.size());
		System.out.println(collection);

//		collection  顶级的接口,拥有的方法并没有那么多

	}

	private static void aboutList() {

//		list有index  而collection  没有
		List list = new ArrayList();

		list.add("laosun");
		list.add("java");
		list.add(1, "eclipse");

//		通过索引值进行设置  set
		list.set(0, "zhiyou");

		Object object = (String) list.get(0);
		System.out.println(object);

//		for遍历
		for (int i = 0; i < list.size(); i++) {
			System.out.println("--" + list.get(i));
		}

		for (Object object1 : list) {
			System.out.println(object1);
		}

//		获取list对象对应的索引值
		int index = list.indexOf("java");
		System.out.println(index);

//		通过索引值list删除对象
		list.remove("java");

//		collection中有的方法在list中也有,可以继承过来使用

	}

	private static void aboutStandardList() {
//		List列表
//		ArrayList   LinkedList  Vector
		
//		<E>泛型  类型参数   确定集合中要存放的对象类型
		
//		jdk 1.7写法发生改变
		
//		stringList  只能存放字符串对象
		List<String> stringList = new ArrayList<>();
		stringList.add("laosun");
		stringList.add("java");
		stringList.add("come on baby");
		
//		stringList.size();
		
//		只存放date对象的集合
//		List<Date> dateList = new ArrayList<>();
		
//		总结:
//		List首先是可重复的而且有序的
		
	}
	
	private static void aboutLinkedList() {
		List<String> linkedList = new LinkedList<>();
		
		linkedList.add("java");
		linkedList.add("java");
		linkedList.add("laosun");
		
		for (String string : linkedList) {
			System.out.println(string);
		}
		
//		LinkedList和ArrayList区别
//		1:都实现list接口
//		2:ArrayList是针对于数组的封装,实现类数组动态的扩容
//			LinkedList是针对链表的封装
//		3:linkedlist插入和增加的操作效率是要比arraylist高,但是查询的操作arraylist较高
//		从使用上讲  如果以查询为主  选择arraylist 如果频繁的插入操作  优先选择linkedlist
		
	}
	
	private static void aboutVector() {
		
		List<String> vectorList = new Vector<>();
		vectorList.add("java");
		vectorList.add("laosun");
		vectorList.add("java");
		
		for (String string : vectorList) {
			System.out.println("--"+string);
		}
		
//		迭代器
//		1:获得list对应的iterator对象
		Iterator<String> iterator = vectorList.iterator();
//		2:遍历	hasNext  Next
		while (iterator.hasNext()) {
			String string = iterator.next();
			System.out.println(string);
			if ("java".equals(string)) {
//				vectorList.remove(o)  不建议使用
//				不要使用list直接remove对象,反而使用迭代器来完成对象的remove
				iterator.remove();
			}
		}
		
//		list对象可以直接输出
		System.out.println(vectorList);
		
/*//		foreach遍历
		for (String string : vectorList) {
			System.out.println(string);
		}*/
		
		
	}
}
/**
 * 
 */
package com.zhiyou.P;

/**
 * @author Administrator
 *
 */
public class Phone implements Comparable<Phone>{
	private String brand;
	private double price;

	public String getBrand() {
		return brand;
	}

	public void setBrand(String brand) {
		this.brand = brand;
	}

	public double getPrice() {
		return price;
	}

	public void setPrice(double price) {
		this.price = price;
	}

	public Phone() {
		super();
		// TODO Auto-generated constructor stub
	}

	public Phone(String brand, double price) {
		super();
		this.brand = brand;
		this.price = price;
	}

	@Override
	public String toString() {
		return "Phone [brand=" + brand + ", price=" + price + "]";
	}

	@Override
//	规定对象的比较规则
	public int compareTo(Phone o) {
		// TODO Auto-generated method stub
//		如果按照brand比较
//		return this.brand.compareTo(o.brand);
//		按照price比较
//		double result = this.price - o.price;
//		return (int)result;
		
		if (this.brand.compareTo(o.brand)==0) {
			double result = this.price - o.price;
			return (int)result;
		}
		return this.brand .compareTo(o.brand);
	}

}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值