package test0915.collectiontest;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
/**
* 容器的概念: 存储多个对象的对象。
* 1 存储的元素只能是引用类型的对象;
* 2 可以随意的添加和删除元素,容器的容量是无限的;
*
* 一张图
*
* Collection Map
* / \ |
* Set List SortedMap
* |
* SortedSet
*
*
* 三个知识点
* 泛型
* 迭代器: 先判断后获取; hasNext(); nest();
* 比较器: Comparable Comparator 排序;
*
*
* 六个接口 *
* Collection
* Set
* List
* Map
* Iterator
* Comparable
*
*
* 九个常用类 *
* ArrayList
* HashMap
* LinkedList
* Properties
* HashSet
* Collections
* Hashtable
* TreeSet
* Voctor,Stack ...
*
*
* 一、Collection
*
1、添加: add(E e)
2、删除: remove(Object o) clear()
3、修改:
4、查询: size() contains(Object o)
5、遍历
1)、foreach
2)、iterator()
二、List 新增大量关于索引的方法
1、添加: add(int index, E element) add(E e)
2、删除: remove(int index) remove(Object o) clear()
3、修改: set(int index, E element)
4、查询: get(int index) size() contains(Object o) indexOf(Object o)
5、遍历
1)、foreach
2)、iterator()
3)、for(int i=0;i<容器.size();i++){
容器.get(i)
}
三、Set:去重 ,与Collection同理
四、Map :HashMap
1、增加:put(K key, V value)
2、修改:put(K key, V value)
3、删除: remove(Object key) clear()
4、查看: get(Object key) size() containsKey(Object key) containsValue(Object value) isEmpty()
5、遍历
1)、条目集合 entrySet()
2)、键集合 keySet()
3)、值的容器 values()
* @author Administrator
*/
public class CollectionTest {
public static void main(String[] args) {
Collection<String> co = new ArrayList<String>();
// 添加元素
co.add("大毛");
co.add("二毛");
co.add("小白");
co.add("红豆");
//遍历 使用迭代器
System.out.println("===========迭代器==========");
Iterator<String> it = co.iterator();
while(it.hasNext() ){
System.out.println(it.next() );
}
System.out.println("=========foreach==========");
for(String tem : co){
System.out.println(tem);
}
//其他方法
boolean flag = co.contains("小白"); //查找
System.out.println(flag);
co.remove("红豆"); //删除
System.out.println(co.size() ); //查看元素个数
co.clear(); //清空
System.out.println(co.size() );
}
}
package test0915;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
/**
* * 模拟购物车 ,挑选物品,结账
*
*
* 1、添加: add(E e)
2、删除: remove(Object o) clear()
3、修改:
4、查询: size() contains(Object o)
5、遍历
1)、foreach
2)、iterator()
*
* @author Administrator
*/
public class GoodsTest {
public static void main(String[] args) {
Collection <Goods> col = new ArrayList<Goods>();
//挑选商品
col.add(new Goods("苹果", 10, 20) );
col.add(new Goods("香蕉", 15, 20) );
col.add(new Goods("梨", 8, 20) );
//结账 == 遍历
System.out.println("==============商品列表==============");
double total = 0. ; //定义变量 ----- 总金额
Iterator<Goods> it = col.iterator();
while(it.hasNext() ){
Goods temp = it.next();
System.out.println(temp.getName()+"-->"+temp.getN()+"-->"+temp.getPrice());
total +=temp.getN()*temp.getPrice();
}
System.out.println("应付:"+total);
}
}
/**
* 创建一个物品类 根据需求。
*/
class Goods {
private String name;
private double price;
private int n;
public Goods() { //alt+/
}
//alt+shift+s --> o
public Goods(String name, double price, int n) {
super();
this.name = name;
this.price = price;
this.n = n;
}
//alt+shift+s -->r -->tab+回车 --->shift+tab -->回车
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getN() {
return n;
}
public void setN(int n) {
this.n = n;
}
//alt+shift+s -->h
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + n;
result = prime * result + ((name == null) ? 0 : name.hashCode());
long temp;
temp = Double.doubleToLongBits(price);
result = prime * result + (int) (temp ^ (temp >>> 32));
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
final Goods other = (Goods) obj;
if (n != other.n)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (Double.doubleToLongBits(price) != Double
.doubleToLongBits(other.price))
return false;
return true;
}
}