您的位置:首页 > 编程语言 > Java开发

Java 集合

2016-03-07 14:24 696 查看
1.散列表Map

- 容量       散列表中散列数组大小
- 散列运算    key->散列值的算法
如:"mm".hashCode()%10->8
- 散列桶     散列值相同的元素的”线性几何“
- 加载因子   就是散列数组加载率,一般小于75%性能比较理想
- 散列查找   根据key计算散列值,根据散列值(下标),找到散列桶
顺序比较key,如果一样,就返回value
散列表中key不同,value可以重复


2.HashMap

- HashMap以键值对的形式存储对象,关键字key是唯一的,不重复的
key可以是任何对象,value可以是任何对象
key:value 成对放在集合中
重复的key算一个,重复添加时替换操作
根据key的散列值计算散列表,元素按照散列值排序
HashMap默认的容量是16,默认加载因子0.75
HashMap根据key检索查找value值


Hash可以在构造时指定参数:初始容量和加载因子



案例:HashMap

public class HashMapDemo {
public static void main(String[] args) {
HashMap users = new HashMap();
//注册用户
users.put("Tom", new User("Tom", "123", 5));
users.put("Jerry", new User("Jerry", "123", 6));
users.put("Andy", new User("Andy", "abc", 6)); //将被覆盖
users.put("Andy", new User("Andy", "123", 8));
System.out.println(users);
//登录查找
Scanner s = new Scanner(System.in);
while(true) {
System.out.println("用户名:");
String name = s.nextLine();
System.out.println("密码:");
String pwd = s.nextLine();
if (!users.containsKey(name)) {
System.out.println("没有注册!");
continue;
}
User user = (User) users.get(name);
if (user.pwd.equals(pwd)) {
System.out.println("欢迎"+user.name+"   age:"+user.age);
break;
}
}
}
}
class User {
String name;
String pwd;
int age;
public User(String name, String pwd, int age) {
super();
this.name = name;
this.pwd = pwd;
this.age = age;
}
public String toString() {
return name+":"+age;
}
}


HashMap VS HashTable

HashMap   新,非线程安全,不检查锁,快
HashTable 旧,线程安全,检查锁,慢一点


3.集合框架

- 集合框架包括集合不映射(Collection and Map), 以及它们的子类(容器类)

- List元素有先后次序的集合, 元素有index位置, 元素可以重复,继承自Collection接口
实现类:ArrayList,Vector,LinkedList
- Set元素无续,不能重复添加,是数学意义上的集合,继承自Collection接口
 实现类:HashSet(是一个只有Key的HashMap
- Collection集概念,没有说明元素是否重复和有序,使用集合的跟接口,很少直接使用
其他集合都是实现类: ArrayList, HashSet
- Map描述了(key: value)成对放置的集合,key不重复,Value可以重复(key重复算一个)
 实现类:HashMap(散列表算法实现)
TreeMap(二叉排序树实现,利用Key排序)
 Map适合检查查找


java集合架构支持3种类型的集合:规则集(Set),线性表(List),和图(Map)

- Collection接口
Set接口:HashSet具体类
LinkedHashSet具体类
TreeSet具体类
List接口:ArrayList具体类
LinkedList具体类
向量类Vector具体类
Stack具体类
- Map接口
HashMap类
LinkedHashMap类
TreeMap类    


案例:HashSet

public class SetDemo {
public static void main(String[] args) {
Set<Node> foods = new HashSet<Node>();
//1.放入5个豆豆
foods.add(new Node(3, 4));
foods.add(new Node(6, 8));
foods.add(new Node(2, 12));
foods.add(new Node(5, 10));
foods.add(new Node(9, 9));
System.out.println(foods.size());
System.out.println(foods);
//2.吃了一个豆豆
foods.remove(new Node(9, 9));
//3.在面板中画出豆豆
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 20; j++) {
if (foods.contains(new Node(i, j))){
System.out.print("o");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
}
class Node {
int i;
int j;
public Node(int i, int j) {
super();
this.i = i;
this.j = j;
}
//注意:必须写equals()方法
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (this == obj) {
return true;
}
if (obj instanceof Node) {
Node node = (Node) obj;
return i == node.i && j == node.j;
}
return false;
}
//注意:必须写hashCode()方法
public int hashCode() {
return (i << 16) | j;
}
public String toString() {
return "["+i+","+j+"]";
}
}


案例:链表数据结构

public class LinkedDemo {
public static void main(String[] args) {
Node head = new Node("黑桃10");
head.next = new Node("黑桃J");
head.next.next = new Node("黑桃Q");
head.next.next.next = new Node("黑桃K");
System.out.println(head);
}
}
class Node {
Object value;
Node next;
public Node (Object obj) {
value = obj;
}
public String toString() {
return next == null ? value.toString() : value+","+next ;//递归的写法
}
}


4.Java泛型

泛型是Java5以后提出的语法现象
作用是在编译期检查的类型约束(运行期不检查泛型)
泛型可以用来约束类中元素的类型


案例:泛型

public class ShopDemo {
public static void main(String[] args) {
Shop<Food> foodShop = new Shop<Food>(new Food("土豆")); //食品店
Shop<Pet> petShop = new Shop<Pet>(new Pet("旺财")); //宠物店
System.out.println(foodShop.buy());
System.out.println(petShop.buy());
// 如果不适用泛型,默认泛型是<Object>
Shop shop = new Shop(new Object());
}
}
class Shop<P> { //P代表商品类型
P product; //销售商品
public Shop(P p) {
product = p;
}
P buy() { // 进货方法
return product;
}
}
class Food {
String name;
public Food(String name) {
super();
this.name = name;
}
public String toString() {
return name;
}
}
class Pet {
String name;
public Pet(String name) {
super();
this.name = name;
}
public String toString() {
return name;
}
}


5.集合的迭代

集合的迭代,是一种遍历算法

- 迭代操作举例:播放列表的“逐个播放”,将扑克牌“逐一发放”
- Java使用Iterator接口描述了迭代模式操作
Iterator中的方法,与门为while循环设计
- Iterator的实例可以从集合对象获得,是这个集合的一个元素序列视图,
默认包含一个操作游标(在第一个元素之前)
hasNext()方法,可以检查游标是否有下一个元素
next) 方法,移动游标到下一个元素,并丏返回这个元素引用
使用while循环配合这个两个方法, 可以迭代处理集合的所有元素
- 迭代时可以使用迭代器remove()方法删除刚刚迭代的元素在迭代过程中
 迭代时不能使用集合方法(add, remove, set) 更改集合元素!


案例:Iterator

public class IteratorDemo {
public static void main(String[] args) {
Collection<String> eggs = new HashSet<String>();
eggs.add("鸡蛋");
eggs.add("鸭蛋");
eggs.add("鸟蛋");
Iterator<String> ite = eggs.iterator();
while (ite.hasNext()) {
String egg = ite.next();
System.out.println(egg);
}
}
}


6.集合的工具类 Collections

同数组的工具类 Arrays 相同,集合的工具类为 Collections,
其中提供了许多的方法,诸如排序、 二分查找、打乱、填充等操作。


案例:Collections

public class CollectionsDemo {
public static void main(String[] args) {
List<String> names = new ArrayList<String>();
names.add("Tom");
names.add("Jerry");
names.add("Black");
names.add("Andy");
names.add("Lee");
// 排序
Collections.sort(names);
System.out.println(names);
// 二分法查找
int index = Collections.binarySearch(names, "Jerry");
System.out.println(index);
// 乱序
Collections.shuffle(names);
System.out.println(names);
}
}


7.Comparable和Comparator

- Comparable
表示可以比较的(用于类实现)
实现这个接口表示:这个类的实例可以比较大小,可以迚行自然排序
compareTo() 返回正数表示大, 返回负数表示小, 返回 0 表示相等
Comparable 的实现必须与equals()的结果一致,就是相等的对象时候, 比较结果一定是0!
- Comparator
比较工具
用于临时定义比较规则,不是默认比较规则


案例:Comparator

public class Demo {
public static void main(String[] args) {
//String重写的compareTo方法
int a = "Tom".compareTo("Jerry");
System.out.println(a);
a = "Tom".compareTo("Tom");
System.out.println(a);
a = "Jerry".compareTo("Tom");
System.out.println(a);
List<String> names = new ArrayList<String>();
names.add("Tom");
names.add("Jerry");
names.add("Black");
names.add("Andy");
names.add("Lee");
ByLength byLength = new ByLength();
Collections.sort(names, byLength);
System.out.println(names);
}
}
//自定义比较规则
class ByLength implements Comparator<String> {
@Override
public int compare(String o1, String o2) {
return -(o1.length() - o2.length());//按照长度比
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: