Map是双列集合的根接口
1.是以键值对的形式存在 key --value
2. 键值对保持唯一
//map中的添加方法
HashMap<Stirng ,Integer> map = new Hashmap<>();
map.put("as",20);
Integer a = map.put("as",18);
map.put("ad",19);
//如果添加相同的key后面的那个value会替
换前面那个key的value,并且返回被替换掉的value
//是否包含 key
boolean b1 = map.containsKey("as")
//是否包含value
boolean b2 = map.values(19);
//通过key 来 获得value
Integet integer = map.get("as");
//获得 所有的key的set集合
Set<String > keySet = map.keySet();
//通过key来删除整个键值对
Integer remove = map.remove();
//获取键值对
Set<Entry<String,Integer>> a = map.entrySet();
private static void fun3() {
//创建一个map key保存学生,value保存 户集
HashMap<Student, String > map = new HashMap<>();
map.put(new Student("鹏鹏", 26), "安徽");
map.put(new Student("北北", 21), "河南");
//重写之后key相同 value 会被新值覆盖
map.put(new Student("北北", 21), "河西");
map.put(new Student("轩轩", 24), "江苏");
//如何保持键的唯一性 map如何保持键的唯一性
//相当于set集合去重
//Arraylist 去重 在类中重写equalis 方法
System.out.println(map);
}
//遍历Map
1 通过keySet来遍历
HashMap<String ,Integer> map = new HashMap<>();
Integer put3 = map.put("正北", 20);
Integer put = map.put("正西", 20);
Integer put2 = map.put("正南", 34);
Integer put4 = map.put("正冬", 35);
Set<String> a = map.keySet;
Iterator iterator = a.iterator();
while( itertor.hasNext()){
String key = iterator.next()
Interator b = map.get(key);
System.out.printlnj(key + " =" + value)
}
2.通过快速遍历来遍历
HashMap<String ,Integer> map = new HashMap<>();
Integer put3 = map.put("正北", 20);
Integer put = map.put("正西", 20);
Integer put2 = map.put("正南", 34);
Integer put4 = map.put("正冬", 35);
for ( String a : map.keySet()){
Integer b = map.get(a);
System.out.println( a +" = "+ b)
}
//通过键值对来进行遍历,通过其中的特有的方法
HashMap<String ,Integer> map = new HashMap<>();
Integer put3 = map.put("正北", 20);
Integer put = map.put("正西", 20);
Integer put2 = map.put("正南", 34);
Integer put4 = map.put("正冬", 35);
Set<Entry<String,INteger>> a = map.EntrySet();
Iterator iterator b =a.iterator();
while(b.hasNext()){
Entry<String, Integer> entry= iterator.next();
String key = entry.getKey();
Integer value = entry.getValue();
System.out.println(key + "=" + value);
}
Hashset和HashMap 之间有什么联系
底层都是Hash算法,一套算法两个类使用
Hashset 底层是依赖HashMap去实现的 ,添加到HashMap的键的位置
HashMap 去重功能
treeMap 排序功能
都是根据键啊
// LinkedHashMap
//特点 怎么存 的怎么打印出来
LinkedHashMap<String, Integer> map = new LinkedHashMap<>();
map.put("丁鹏", 11);
map.put("水", 12);
map.put("倩倩", 34);
map.put("忠", 19);
System.out.println(map);
//collections中的一些操作集合的方法
ArrayList<Integer> arrayList = new ArrayList<>();
arrayList.add(12);
arrayList.add(34);
arrayList.add(45);
arrayList.add(2);
//随机交换集合中的值的位置
Collections.shuffle(arraylist);
//排序
Collections.sort(arraylist);
//反转集合
Collections.reserve();
//二分查找,要是有序的
int a =Collections.binarySerach( arraylist , 12);
//返回的是角标
public class Student implements Comparable<Student> {
private String name;
private int age;
public Student() {
super();
// TODO Auto-generated constructor stub
}
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return " [name=" + name + ", 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;
}
//@Override
public int compareTo(Student o) {
int num = this.getAge() - o.getAge();
return num ==0 ? 1 : num;
}
}
ArrayList<Student> list = new ArrayList<>();
list.add(new Student("丁鹏", 13));
list.add(new Student("水", 15));
list.add(new Student("北", 19));
//排序也要实现comparable 接口 只要排对象就要编写规则
Collections.sort(list);
System.out.println(list);
//小练习
/*
* 使用Arraylist 模拟斗地主三人
* 1.洗牌
* 2. 发牌
* 3. 看牌
*
* 分析
* 1 创建一副牌(使用Arraylist)
* 2 洗牌(shuffle)
* 3 发牌(分三人和 底牌) 相当于三个 list
* 4.看牌(打印list)
*
*
*/
import java.util.ArrayList;
import java.util.Collections;
public class Fuxi {
public static void main(String[] args) {
ArrayList<String> poker = new ArrayList<>();
String[] s1 = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
String [] s2 = {"黑桃","红桃","梅花","方块"};
for (int i = 0; i < s1.length; i++) {
for (int j = 0; j < s2.length; j++) {
String string = s1[i] +s2[j];
poker.add(string);
}
}
poker.add("小王");
poker.add("大王");
Collections.shuffle(poker);
ArrayList<String> list0 = new ArrayList<>();
ArrayList<String> list1 = new ArrayList<>();
ArrayList<String> list2 = new ArrayList<>();
ArrayList<String> dipai = new ArrayList<>();
for (int i = 0; i < poker.size(); i++) {
if(i >= (poker.size()-3)) {
dipai.add(poker.get(i));
} else if(i% 3 ==0) {
list0.add(poker.get(i));
}else if ( i % 3 ==1) {
list1.add(poker.get(i));
}else {
list2.add(poker.get(i));
}
}
System.out.println(list0.size());
System.out.println(list1.size());
System.out.println(list2.size());
System.out.println(dipai);
}
}
/*
* Java学科
* java 1班 -- 保存是学生 和 户籍
* java 2班 -- 保存是学生 和 户籍
*
* Java学科 是一个map
* 保存了两个键值对 ---- 1班 和 2班
*
* 1班 也是一个 map
* 保存 ---- 学生 户籍
*
* 遍历
*
*/
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
public class Fuxi {
public static void main(String[] args) {
HashMap<Student, String> class1 = new HashMap<>();
HashMap<Student, String> class2 = new HashMap<>();
class1.put(new Student("丁鹏", 15), "安徽");
class1.put(new Student("xzb", 15), "河南");
class1.put(new Student("xx", 15), "江苏");
class2.put(new Student("ygs", 15), "四川");
class2.put(new Student("xsd", 15), "浙江");
class2.put(new Student("j", 15), "江苏");
// 把班放到学科中
// 班是key 几班是value
HashMap<HashMap<Student, String>, String> map = new HashMap<>();
map.put(class1, "j1班");
map.put(class2, "j2班");
for (HashMap<Student, String> a : map.keySet()) {
for (Student student : a.keySet()) {
System.out.println(student.getName() + "年龄为"+ student.getAge() );
}
}
}
}