Java基础
Java集合框架
Map接口
- 用于存储任意键值对key-value
- 键:无序、无下标、不允许重复
- 值:无序、无下标、运行重复
方法
-
put(key,value) 将对象存入到集合中,关联键值。若key值重复则覆盖原值
-
get(Object key) 根据键获取对应的值
-
Set(key) 返回所有的key
-
values() 返回包含所有值的collection集合
-
Set<Map.Entry<k,v>> 键值匹配的set集合
/** * Map接口的使用 * 特点 * 1、存储键值对 * 2、键不能重复、值可以 * 3、无序 * @author xue */ public class Demo1 { public static void main(String[] args) { //创建Map集合 Map<String, String> hashMap = new HashMap<>(); //添加元素 hashMap.put("cn","china"); hashMap.put("uk","english"); hashMap.put("usa","amarica"); hashMap.put("usa","shabi");//后面出现相同的key值是,value值会被替换最新 System.out.println("元素个数"+hashMap.size()); System.out.println(hashMap.toString()); //删除元素 hashMap.remove("usa");//可以只指定key值,也可以key-value都指定 //遍历 System.out.println("使用keyset集合"); Set<String> keySet = hashMap.keySet();//返回的是所有key的set集合 for (String str:keySet) { System.out.println(str+":"+hashMap.get(str)); } System.out.println("使用entrySet方法"); Set<Map.Entry<String,String>> entries = hashMap.entrySet(); for (Map.Entry<String,String> entry:entries) { System.out.println(entry); System.out.println("4444"); System.out.println(entry.getKey()+":"+entry.getValue()); } //entrySet的效率要由于keySet } }
HashMap
- 线程不安全,运行效率快
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return age == student.age && Objects.equals(name, student.name);
}
@Override
public int hashCode() {
return Objects.hash(name, age);
}
/**
* Map接口的使用
* 存储结构:哈希表(数组+链表+红黑树)
* key值唯一的原理是使用key的hashcode和euals方法为依据
* 也就是说重写这两个方法可以改变key值比较的规则
* @author xue
*/
public class Demo2 {
public static void main(String[] args) {
//创建集合
HashMap<Student,String> hashMap = new HashMap<>();
//添加元素
Student stu1 = new Student("aaa", 12);
Student stu2 = new Student("bbb", 13);
Student stu3 = new Student("ccc", 18);
Student stu4 = new Student("ddd", 10);
hashMap.put(stu1,"1");
hashMap.put(stu2,"2");
hashMap.put(stu3,"4");
hashMap.put(stu4,"3");
//重写hashCode与equals方法后下面的元素便无法添加
hashMap.put(new Student("ddd", 10),"3");
System.out.println("元素个数:"+hashMap.size());
System.out.println(hashMap);
//删除
hashMap.remove(stu1);
//重写hashCode与equals方法后下面的元素这样可以删除
hashMap.remove(new Student("ddd", 10));
System.out.println("元素个数:"+hashMap.size());
System.out.println(hashMap);
//遍历
System.out.println("keySet");
Set<Student> keySet = hashMap.keySet();
for (Student stu:keySet) {
System.out.println(stu+":"+hashMap.get(stu));
}
System.out.println("entrySet");
Set<Map.Entry<Student, String>> entries = hashMap.entrySet();
for (Map.Entry entry:entries) {
System.out.println(entry.getKey()+":"+entry.getValue());
}
}
}
TreeMap
-
实现了SortedMap接口,可以对key自动排序
/** * TreeMap的使用 * 存储结构:红黑树 * @author xue */ public class Demo3 { public static void main(String[] args) { //创建TreeMap集合 TreeMap<Student, String> treeMap = new TreeMap<Student, String>(); //添加元素 Student stu1 = new Student("aaa", 12); Student stu2 = new Student("bbb", 13); Student stu3 = new Student("ccc", 18); Student stu4 = new Student("ddd", 10); Student stu5 = new Student("ddd", 10); treeMap.put(stu1,"1"); treeMap.put(stu2,"2"); treeMap.put(stu4,"4"); treeMap.put(stu3,"3"); treeMap.put(stu5,"3"); // treeMap.put(stu1,"1"); System.out.println("元素个数:"+treeMap.size()); System.out.println(treeMap); //删除 treeMap.remove(stu1); //遍历 System.out.println("keySet"); Set<Student> keySet = treeMap.keySet(); for (Student stu:keySet) { System.out.println(stu+":"+treeMap.get(stu)); } System.out.println("entrySet"); Set<Map.Entry<Student, String>> entries = treeMap.entrySet(); for (Map.Entry entry:entries) { System.out.println(entry.getKey()+":"+entry.getValue()); } } }
看了源码后就能直到HashSet与TreeSet其实都是调用了HashMap与TreeMap,有兴趣可以看一看
Colletions工具类
-
集合的工具类,定义了一下集合的常用方法
/** * Collections工具类的使用 * @author xue */ public class Demo4 { public static void main(String[] args) { ArrayList<Integer> aList = new ArrayList<>(); aList.add(234); aList.add(2); aList.add(34); aList.add(346); aList.add(56457); System.out.println("排序前"); System.out.println(aList); System.out.println("排序后"); Collections.sort(aList);//也可以自己写比较规则 System.out.println(aList); //二分查找 System.out.println( Collections.binarySearch(aList, 34));//返回的是int类型的位置序号 //翻转 System.out.println("翻转后"); Collections.reverse(aList); System.out.println(aList); //打乱 System.out.println("打乱后"); Collections.shuffle(aList); System.out.println(aList); //list转数组 Integer[] arr = (Integer[]) aList.toArray(new Integer[aList.size()]); System.out.println(arr.length); System.out.println(Arrays.toString(arr)); //数组转List List<Integer> list = Arrays.asList(arr);//受限集合:只读 // list.add(12);//添加就报错 System.out.println(list); //这两个方法有很多限制 } }