Map集合
基本的用法:
/**
* HashMap
*
* @author kiosk
*/
public class Test {
public static void main(String[] args) {
HashMap<String, Integer> map = new HashMap<>();
Integer integer1 = map.put("张三", 123);
Integer integer2 = map.put("李四", 123);
Integer integer3 = map.put("王五", 123);
Integer integer4 = map.put("张三", 777);
//覆盖的值为原来的null
System.out.println(integer1);
//覆盖原来的123的值
System.out.println(integer4);
Integer remove = map.remove("李四");
//会返回原来的键对应的value值
System.out.println(remove);
//map集合包不包括这个key
System.out.println(map.containsKey("张三"));
//是否包含传入的值
System.out.println(map.containsValue(123));
}
}
public class Test {
public static void main(String[] args) {
//demo();
HashMap<String, Integer> map = new HashMap<>();
Integer integer1 = map.put("张三", 123);
Integer integer2 = map.put("李四", 123);
Integer integer3 = map.put("王五", 123);
Integer integer4 = map.put("张三", 777);
//获取的是所有的值的集合
Collection<Integer> values = map.values();
for (Integer value : values) {
System.out.println(value);
}
}
Map集合的迭代
(1)第一种比遍历的方式:利用迭代器进行遍历
public class Test {
public static void main(String[] args) {
//demo();
//demo2();
HashMap<String, Integer> map = new HashMap<>();
Integer integer1 = map.put("张三", 123);
Integer integer2 = map.put("李四", 123);
Integer integer3 = map.put("王五", 123);
Integer integer4 = map.put("张三", 777);
//根据键获取值
Integer integer = map.get("张三");
System.out.println(integer);
//HashMap的迭代,先拿到map的所有的键值
Set<String> strings = map.keySet();
//获取迭代器
Iterator<String> iterator = strings.iterator();
while (iterator.hasNext()){
//获取每一个键值
String key = iterator.next();
Integer value = map.get(key);
System.out.println("键:" + key + " 值:" + value);
} }
private static void demo2() {
HashMap<String, Integer> map = new HashMap<>();
Integer integer1 = map.put("张三", 123);
Integer integer2 = map.put("李四", 123);
Integer integer3 = map.put("王五", 123);
Integer integer4 = map.put("张三", 777);
//获取的是所有的值的集合
Collection<Integer> values = map.values();
for (Integer value : values) {
System.out.println(value);
}
}
使用增强for循环进行遍历
public class Test {
public static void main(String[] args) {
//demo();
//demo2();
//demo4();
//第二种遍历的方式值增强for循环
HashMap<String, Integer> map = new HashMap<>();
Integer integer1 = map.put("张三", 123);
Integer integer2 = map.put("李四", 123);
Integer integer3 = map.put("王五", 123);
Integer integer4 = map.put("张三", 777);
for (String s : map.keySet()) {
System.out.println("key:" + s + " value:" + map.get(s));
}
}
利用键值对对象进行遍历
public class Test {
public static void main(String[] args) {
//demo();
//demo2();
//demo4();
//第二种遍历的方式值增强for循环
//demo5();
//另一种遍历的方式
HashMap<String, Integer> map = new HashMap<>();
Integer integer1 = map.put("张三", 123);
Integer integer2 = map.put("李四", 123);
Integer integer3 = map.put("王五", 123);
Integer integer4 = map.put("张三", 777);
Set<Map.Entry<String, Integer>> entries = map.entrySet();
for (Map.Entry<String, Integer> entry : entries) {
System.out.println("键:" + entry.getKey() + " 值:" + entry.getValue());
}
}
LinkedHashMap
public class TestLinkedHashMap {
public static void main(String[] args) {
LinkedHashMap<String,Integer> map = new LinkedHashMap<>();
map.put("张三",23);
map.put("李四",24);
map.put("王五",25);
map.put("赵六",26);
System.out.println(map);
}
}
利用Map集合统计字符串中字符出现的次数
代码:
import java.util.HashMap;
/**
* 统计一个字符串中字符出现的次数
* 1)将字符串转换为字符数组
* 2)定义双列集合,统计字符和字符的出现次数
* 3)遍历存储,每次进行判断,集合中包含键,加1,如果不包含就当作1存储
* 4)最后进行打印
*
* @author kiosk
*/
public class TestCount {
public static void main(String[] args) {
String string = "ioejwioghiowhgiohowhgioqqqq";
char[] chars = string.toCharArray();
HashMap<Character, Integer> map = new HashMap<>();
for (char aChar : chars) {
map.put(aChar,map.containsKey(aChar) ? map.get(aChar) + 1 : 1 );
}
for (Character character : map.keySet()) {
System.out.println("键 :" + character + " 值:" + map.get(character));
}
}
}
集合的嵌套和遍历
import com.westos.domain.Student;
import java.util.HashMap;
/**
* HashMap集合的嵌套
*
* @author kiosk
*/
public class Test {
public static void main(String[] args) {
HashMap<Student, String> map1 = new HashMap<>();
map1.put(new Student("张三", 23), "北京");
map1.put(new Student("李四", 24), "北京");
map1.put(new Student("王五", 25), "上海");
map1.put(new Student("赵六", 26), "广州");
HashMap<Student, String> map2 = new HashMap<>();
map1.put(new Student("唐僧", 1023), "北京");
map1.put(new Student("孙悟空", 1838), "北京");
map1.put(new Student("猪八戒", 3999), "上海");
map1.put(new Student("沙僧", 2724), "广州");
HashMap<HashMap<Student,String>,String> map = new HashMap<>();
map.put(map1,"班级一");
map.put(map2,"班级二");
//嵌套的遍历
for (HashMap<Student,String> m :map.keySet()){
for (Student student : m.keySet()){
System.out.println("学生:" + student + " 班级:" + map.get(m) + " 籍贯:" + m.get(student));
}
}
}
}
Collections工具类
(1)binaraySearch()
private static <T>
int indexedBinarySearch(List<? extends Comparable<? super T>> list, T key) {
int low = 0;
int high = list.size()-1;
while (low <= high) {
int mid = (low + high) >>> 1;
Comparable<? super T> midVal = list.get(mid);
int cmp = midVal.compareTo(key);
if (cmp < 0)
low = mid + 1;
else if (cmp > 0)
high = mid - 1;
else
return mid; // key found
}
//如果没找到的话,返回的是负插入点的值,如果找到的话,返回的是对应的索引值
//插入点(第一个比他)的值是从1开始的
return -(low + 1); // key not found
}
/**
* 二分查找法
* @author kiosk
*/
public class Test {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("a");
list.add("b");
list.add("c");
list.add("d");
list.add("f");
//如果查找的内容是存在的话,返回的值为当前的值对应的索引
System.out.println(Collections.binarySearch(list, "e"));
}
}
(2)获取最值
/**
* @author kiosk
*/
public class Test4 {
public static void main(String[] args) {
List<Student> list = new ArrayList<>();
list.add(new Student("a",23));
list.add(new Student("b",23));
list.add(new Student("c",23));
list.add(new Student("d",23));
//获取最大值
Student max = Collections.max(list);
System.out.println(max);
Student min = Collections.min(list);
System.out.println(min);
}
}
(3)集合的反转
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Test3 {
public static void main(String[] args) {
List<Student> list = new ArrayList<>();
list.add(new Student("a",23));
list.add(new Student("b",23));
list.add(new Student("c",23));
list.add(new Student("d",23));
//集合的反转
Collections.reverse(list);
System.out.println(list);
}
}
(4)随机排序
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Test3 {
public static void main(String[] args) {
List<Student> list = new ArrayList<>();
list.add(new Student("a",23));
list.add(new Student("b",23));
list.add(new Student("c",23));
list.add(new Student("d",23));
//集合的反转
Collections.reverse(list);
System.out.println(list);
//集合的随机洗牌
Collections.shuffle(list);
System.out.println(list);
}
}