java 集合框架
map<k,v> 接口
不可以有重复的键
HashMap<k,v>() 自带排序去重功能
并且允许使用 null键 null值
这里和treemap区别就在于treemap不可以使用 null键值
package com.sxt.map;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.Map;
import java.util.Set;
public class m1 {
public static void main(String[] args) {
Map map = new HashMap<>();
map.put("a", null);
map.put("b", "1");
map.put("c", "2");
map.put("d", "3");
map.put("e", "4");
Set keySet = map.keySet();
// for(Object s:keySet){ //for增强输出方法
// System.out.println(map.get(s));
// }
//System.out.println(KeySet+"-->"map.get(key));
//System.out.println(map.size());
print(map);;
}
Map map = new HashMap<>();
map.put("a", null);
map.put("b", "1");
map.put("c", "2");
map.put("d", "3");
map.put("e", "4");
Set keySet = map.keySet();
// for(Object s:keySet){ //for增强输出方法
// System.out.println(map.get(s));
// }
//System.out.println(KeySet+"-->"map.get(key));
//System.out.println(map.size());
print(map);;
}
private static void print(Map map) {
Set ss = map.keySet();
for(Object s:ss){
System.out.println(s+"--->"+map.get(s));
}
}
}
输出结果
a--->null
b--->1
c--->2
d--->3
e--->4
b--->1
c--->2
d--->3
e--->4
在写一个小例子
要新建一个学生类,用来测试
其中用map.entrySet();方法,返回的是键值对整体
package com.sxt.map;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Set;
import java.util.Map.Entry;
import java.util.Set;
public class Hamap {
public static void main(String[] args) {
HashMap<String, Student> map = new HashMap<>();
map.put("a", new Student("张一",10));
map.put("c", new Student("张三",15));
map.put("d", new Student("张四",18));
map.put("b", new Student("张二",20));
map.put("b", new Student("张二",20));
print(map);
}
HashMap<String, Student> map = new HashMap<>();
map.put("a", new Student("张一",10));
map.put("c", new Student("张三",15));
map.put("d", new Student("张四",18));
map.put("b", new Student("张二",20));
map.put("b", new Student("张二",20));
print(map);
}
private static void print(HashMap<String, Student> map) {
Set<Entry<String, Student>> entrySet = map.entrySet();//使用map.entrySet()方法 获得键值对整体
for(Entry<String , Student>entry:entrySet){
System.out.println(entry.getKey()+"-->"+entry.getValue());
}
}
Set<Entry<String, Student>> entrySet = map.entrySet();//使用map.entrySet()方法 获得键值对整体
for(Entry<String , Student>entry:entrySet){
System.out.println(entry.getKey()+"-->"+entry.getValue());
}
}
}
泛型
在类中加泛型 ,就是在类名后面有<>
可以给成员方法,成员属性定义方法
自定义一个类型MyList<T>
package com.sxt.map;
import java.util.ArrayList;
import java.util.List;
import java.util.ArrayList;
import java.util.List;
public class MyList<T> {
List<T> list =new ArrayList<>();
public void add(T t){
list.add(t);
}
public T get(int index){
return list.get(index);
}
}
List<T> list =new ArrayList<>();
public void add(T t){
list.add(t);
}
public T get(int index){
return list.get(index);
}
}
测试自定义泛型
package com.sxt.map;
public class TestMyList {
public static void main(String[] args) {
MyList<Student> list = new MyList<>();
list.add(new Student("张三",10));
list.add(new Student("张一",15));
list.add(new Student("张二",20));
Student student = list.get(2);
System.out.println(student);
}
MyList<Student> list = new MyList<>();
list.add(new Student("张三",10));
list.add(new Student("张一",15));
list.add(new Student("张二",20));
Student student = list.get(2);
System.out.println(student);
}
}
输出结果
名字:张二 年龄20
在静态方法当中添加泛型
在方法中定义泛型,传什么返回什么
package com.sxt.map;
public class F {
public static void main(String[] args) {
Student student = show( new Student("张三",10));
Person show = show( new Person("李四","男"));
}
public static <T> T show(T t){
System.out.println(t);
return t;
}
Student student = show( new Student("张三",10));
Person show = show( new Person("李四","男"));
}
public static <T> T show(T t){
System.out.println(t);
return t;
}
}
打印结果
名字:张三 年龄10
名字:李四 性别:男
名字:李四 性别:男
Collections 集合当中的工具类
有方法
Collections.sort(list); 正序
Collections.reverse(list);倒叙
Collections.shuffle(list);随机排放元素
Collections.binarySearch 查找位置,但是要先排序
Arrays.asList将数组转化成集合
list.toArray将集合转化为数组
package com.sxt.collect;
import java.awt.datatransfer.ClipboardOwner;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class collect {
public static void main(String[] args) {
List<String > list = new ArrayList<>();
list.add("cc");
list.add("dd");
list.add("aa");
list.add("ww");
list.add("ff");
Collections.sort(list);//排序
print(list);
Collections.reverse(list);//倒叙
print(list);
Collections.sort(list);
//查找方法
// int binarySearch = Collections.binarySearch(list, "ff",new Comparator<String>() {
//
// @Override
// public int compare(String o1, String o2) {
// return o1.compareTo(o2);
// }
// });
// System.out.println(binarySearch);
Collections.shuffle(list);//随机排放元素
print(list);
String[] array = list.toArray(new String[]{});
List<String> asList = Arrays.asList(new String[]{"123"});
}
List<String > list = new ArrayList<>();
list.add("cc");
list.add("dd");
list.add("aa");
list.add("ww");
list.add("ff");
Collections.sort(list);//排序
print(list);
Collections.reverse(list);//倒叙
print(list);
Collections.sort(list);
//查找方法
// int binarySearch = Collections.binarySearch(list, "ff",new Comparator<String>() {
//
// @Override
// public int compare(String o1, String o2) {
// return o1.compareTo(o2);
// }
// });
// System.out.println(binarySearch);
Collections.shuffle(list);//随机排放元素
print(list);
String[] array = list.toArray(new String[]{});
List<String> asList = Arrays.asList(new String[]{"123"});
}
private static void print(List<String> list) {
for(String s:list){
System.out.println(s);
}
System.out.println("-----");
}
for(String s:list){
System.out.println(s);
}
System.out.println("-----");
}
}