集合框架
定义:
集合与数组类似,只不过集合中的数据量可以动态的变化。
集合只能存放自定义类型。
List集合:
存放的数据可以重复,并且有顺序。
List集合转数组Object[] obj = bookList.toArray();
数组转成集合:Arrays.Collections
Set集合:
存放数据无序不可重复
Map集合:
双列型,存放数据无序,key不可以重复,value可以重复。
泛型:
给集合指定具体存放的数据类型,只能存放自定义数据类型。
Collection接口(单列)
List接口
ArrayList(实现类):
默认可以存放Object数据类型,底层是Object数组,增删很慢,查询很快。
ArrayList aList = new ArrayList() 默认为10个
//1.添加数据
aList.add(12);
aList.add('啊');
aList.add(true);
aList.add("yang");
//2.查询数据
Object o = aList.get(0);
System.out.println("o="+o);
System.out.printl("====================================================");
//3.删除集合中指定下标处的数据
aList.remove(2);
//4.按内容删除
aList.remove("yang");
//5.遍历数组
//方式一:传统for循环(size长度)
for (int i = 0; i < aList.size(); i++) {
System.out.println(aList.get(i));
}
System.out.println("====================================================");
//方式二:高级for循环(JDK1.5出)
//for(数据类型 变量名 :集合\数组)
for (Object temp:aList){
System.out.println("temp="+temp);
}
System.out.println("====================================================");
//方式三:Lambda表达式(JDK1.8出现的)
//集合.forEach(temp->{代码块});
aList.forEach(temp->{
System.out.println(temp);
});
System.out.println("====================================================");
//6.修改元素(下标,"要修改的元素")
aList.set(0,"23");
Object i = aList.get(0);
System.out.println(i);
System.out.println("====================================================");
//7.指定类型
List<String> a = new ArrayList<String>();
a.add("啊啊啊");
System.out.println(a);
//8.清空集合中的所有元素
aList.clear();
//9.addAll 增加所有
}
例1:输入5位同学的成绩,排序,找出最大值
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("请输入5位同学的成绩:");
//定义集合容器,用来存放数据
List<Float> scoreList = new ArrayList<Float>();
for (int i = 0; i < 5; i++) {
Float score = input.nextFloat();
scoreList.add(score);
}
//对List集合进行排序
Collections.sort(scoreList);//由小到大排序
int index = scoreList.size()-1;
//写活
Float maxScore = scoreList.get(index);
System.out.println("考试成绩最高分:"+maxScore);
//写死
//Float maxScore = scoreList.get(4);
}
}
例2:原字符序列:a s d f g h 升序排序后:a d f g h s 逆序排序后:s h g f d a
public static void main(String[] args) {
Character[] chs = {'a','s','d','f','g','h'};
//将chs数组转为集合 Arrays、Collections
List<Character> charList = Arrays.asList(chs);
//使用集合工具类对集合进行排序
Collections.sort(charList);
charList.forEach(temp->{
System.out.print(temp+"\t");
});
System.out.println("\n");
//逆序输出
Collections.reverse(charList);
charList.forEach(temp->{
System.out.print(temp+"\t");
});
}
LinekedList(实现类):
底层是链表。增删很快,查询很慢。
Set接口
HashSet
初始值是16,加载因子是0.75(超过16时,就+16*0.75)
底层是哈希码,没有下标
public static void main(String[] args) {
Set<String> aSet = new HashSet<String>();
//1.增加
aSet.add("wang");
aSet.add("啊啊啊");
aSet.add("yang");
//2.删除
aSet.remove("wang");
System.out.println(aSet);
//3.修改:无修改,即先删除后增加
//4.查询:不能取单个的,只能取所有
//5.判断Set集合中指定元素是否存在
boolean flog = aSet.contains("yang");
System.out.println("flog="+flog);
}
TreeSet
底层是二叉树,不可以重复,但是有序(对于实现了Comparable,Comparator接口的类型才可以排序)
Map接口(双列集合)
HashMap
底层是哈希表,没有下标
key对应的记录无序,返回值是此key对应的上一个的旧值
public static void main(String[] args) {
Map<String,String> bookMap = new HashMap<String,String>();
//1.增加数据
String a1 = bookMap.put("1","java");
bookMap.put("2","c");
bookMap.put("3","c++");
System.out.println(bookMap);
//2.通过key查询value
String result = bookMap.get("1");
System.out.println(result);
//3.遍历Map数组
bookMap.forEach((k,v)->{
System.out.println(k+":"+v);
});
//4.直接打印Map(可以直接打印是因为HashMap源码中覆盖了toString方法)
System.out.println(bookMap);
//5.通过添加方法实现,自动覆盖
//6.删除
bookMap.remove("1");
System.out.println(bookMap);
}
TreeMap
底层是二叉树,key不能重复,value可以重复。可以根据key进行排序(一般key的类型实现了Comparable,Comparator接口)
例三:给定一个字符串,统计每个字符出现的次数,判断容器中某个字符是否存在
首先,判断容器中某个字符是否存在;若存在,将字符作为key,取出value,然后value++,重写将数据保存进去;若不存在,将字符作为key,1作为value。
public class Demo03 {
public static void main(String[] args) {
//给定一个字符串:String str,统计每个字符出现的次数
//提示:TreeMap<String,Integer>
String str = "asdfgh";
//1.将字符串转成字符数组
char[] chs = str.toCharArray();
//2.取出chs中的某个元素,看容器中是否存在
Map<String,Integer> aMap = new TreeMap<String,Integer>();
for (char temp:chs){
Integer value = aMap.get(temp+" ");//字符串+任何数据类型都是字符串
if (value==null) {//指定字符不存在
aMap.put(temp + " ", 1);
}else {//指定字符存在
value++;
aMap.put(temp + " ", value);
}
}
//第二种方法
Map<Character,Integer> aMap = new TreeMap<Character, Integer>();
for (char temp:chs){
boolean flag = aMap.containsKey(temp);
if (flag) {//指定key在集合中存在
Integer value = aMap.get(temp);
value++;
aMap.put(temp, value);
}else {//不存在
aMap.put(temp, 1);
}
}
//打印结果
aMap.forEach((k,v)->{
System.out.println(k+"="+v);
});
}