1.set接口
1.1 特性
Set体系的集合:
A:存入集合的顺序和取出集合的顺序不一致
B:没有索引
C:存入集合的元素没有重复
1.2 HashSet
基本使用:
package com.daigua17;
import java.util.HashSet;
public class SetDemo {
public static void main(String[] args) {
HashSet<Student> h = new HashSet<Student>();
h.add(new Student("呆瓜", 18));
h.add(new Student("猪娃", 19));
for (Student student : h) {
System.out.println(student);
}
}
}
class Student {
private String name;
private int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + "]";
}
}
唯一性原理:
规则:新添加到HashSet集合的元素都会与集合中已有的元素一一比较
首先比较哈希值(每个元素都会调用hashCode()产生一个哈希值)
如果新添加的元素与集合中已有的元素的哈希值都不同,新添加的元素存入集合
如果新添加的元素与集合中已有的某个元素哈希值相同,此时还需要调用equals(Object obj)比较
如果equals(Object obj)方法返回true,说明新添加的元素与集合中已有的某个元素的属性值相同,那么新添加的元素不存入集合
如果equals(Object obj)方法返回false, 说明新添加的元素与集合中已有的元素的属性值都不同, 那么新添加的元素存入集合
package com.daigua17;
import java.util.HashSet;
public class HashSetDemo2 {
public static void main(String[] args) {
HashSet<Student> hs = new HashSet<Student>();
Student s1 = new Student("aa", 1);
Student s2 = new Student("bb", 1);
Student s3 = new Student("cc", 1);
hs.add(s1);
hs.add(s2);
hs.add(s3);
hs.add(s3);
for (Student student : hs) {
System.out.println(student);
}
}
}
class Student {
private String name;
private int age;
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + "]";
}
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student other = (Student) obj;
if (age != other.age)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
}
1.3 洗牌练习
package com.daigua17;
import java.util.ArrayList;
import java.util.Collections;
/*
* 模拟斗地主发牌
买牌
洗牌
发牌
*/
public class PokerTest {
public static void main(String[] args) {
// 创建花色数组
String[] huase = {"黑桃", "红桃", "梅花", "方块"};
// 创建卡牌数字数组
String[] value = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
// 创建卡牌集合
ArrayList<String> cardBox = new ArrayList<String>();
// 组成一副牌
for (int i = 0; i < huase.length; i++) {
String temp_huase = huase[i];
for (int j = 0; j < value.length; j++) {
String temp_value = value[j];
cardBox.add(temp_huase + temp_value);
}
}
cardBox.add("大王");
cardBox.add("小王");
// 洗牌 打乱顺序
Collections.shuffle(cardBox);
// 三个玩家
ArrayList<String> 林志玲 = new ArrayList<String>();
ArrayList<String> 波多野结衣 = new ArrayList<String>();
ArrayList<String> 大桥未久 = new ArrayList<String>();
// 发牌,要留三张底牌
for (int i = 0; i < cardBox.size() - 3; i++) {
if (i % 3 == 0) {
林志玲.add(cardBox.get(i));
} else if (i % 3 == 1) {
波多野结衣.add(cardBox.get(i));
} else if (i % 3 == 2) {
大桥未久.add(cardBox.get(i));
}
}
System.out.println("林志玲:" + 林志玲);
System.out.println("波多野结衣:" + 波多野结衣);
System.out.println("大桥未久:" + 大桥未久);
System.out.println("底牌:");
for (int i = cardBox.size() - 3; i < cardBox.size(); i++) {
System.out.println(cardBox.get(i));
}
}
}
2.Map
package com.daigua17;
import java.util.HashMap;
import java.util.Map;
/*
* Map的常用功能:
* 映射功能:
* V put(K key, V value)
* 获取功能:
* V get(Object key)
* int size()
* 判断功能:
* boolean containsKey(Object key)
boolean containsValue(Object value)
boolean isEmpty()
* 删除功能:
* void clear()
* V remove(Object key)
*
* 遍历功能:
* Set<Map.Entry<K,V>> entrySet()
*
*
* Set<K> keySet()
* Collection<V> values()
*/
public class MapDemo {
public static void main(String[] args) {
// 多态,向上转型
Map<String, String> hm = new HashMap<String, String>();
// 映射 添加一个键值对
hm.put("1", "100");
hm.put("2", "101");
System.out.println(hm);
// 获取
System.out.println(hm.get("1"));
// 判断
System.out.println(hm.containsKey("1"));
System.out.println(hm.containsValue("100"));
System.out.println(hm.isEmpty());
// 删除
// System.out.println(hm.remove("1"));
// hm.clear();
// System.out.println(hm);
// 遍历1:用keySet
for (String s : hm.keySet()) {
System.out.println(s + ":" + hm.get(s));
}
System.out.println("--------------");
// 遍历2:用entrySet
for (Map.Entry<String, String> stringStringEntry : hm.entrySet()) {
System.out.println(stringStringEntry);
System.out.println(stringStringEntry.getKey());
System.out.println(stringStringEntry.getValue());
System.out.println("++++++");
}
}
}