集合
1、List
有序,可重复,(父接口:Collection:对equals与hashcode方法均重写)
List-ArrayList(数组)
2、Set
无序,不可重复(父接口:Collection)
3、Map
键值对(request,session,application)
4、Collection
1、定义:
1>范型jdk5.0出现的
Collection< String> collection = new ArrayList< String>();
2>jdk1.7之后
Collection< String> collection = new ArrayList<>();
不可装基本数据类型,要装则改为包装类
2、常用方法
1>isEmpty() 测试集合中有无元素
2>size()元素个数
3>add()添加元素
4>contains()集合中是否包括
5>remove()移除元素
6>addAll()//a.add(b)将b内所有元素加到a中
7>containsAll()//a.containsAll(b)a中是否包含b中所有
//测试Colection接口的常用方法
public class Demo1 {
/**
* 范型jdk5.0出现的
* Collection<String> collection = new ArrayList<String>();
* jdk1.7之后
* Collection<String> collection = new ArrayList<>();
* 不可装基本数据类型,要装则改为包装类
*/
public static void main(String[] args) {
Collection<String> collection = new ArrayList<>();
System.out.println(collection.isEmpty());//isEmpty() 测试集合中有无元素
collection.add("hello");
collection.add("abc");
collection.add("123");
boolean b = collection.isEmpty();
System.out.println(b);
System.out.println(collection.size());//size()元素个数
System.out.println(collection.contains("hello"));
System.out.println(collection.remove("hello"));
System.out.println(collection.size());
collection.clear();
System.out.println(collection.isEmpty());
}
}
3、遍历集合方式
1>增强for循环
2>迭代器
3>使用lambda表达式
public class Demo2 {
public static void main(String[] args) {
Collection<String> collection = new ArrayList<>();
System.out.println(collection.isEmpty());//isEmpty() 测试集合中有无元素
collection.add("hello");
collection.add("abc");
collection.add("123");
//第一种方式增强for循环
System.out.println("----1增强for循环---");
for(String str : collection) {
System.out.println(str);
}
//第二种方式迭代器
System.out.println("---2迭代器-----");
Iterator< String> iterator = collection.iterator();
// for (int i = 0; i < collection.size(); i++) {
// if (iterator.hasNext()) {
// System.out.println(iterator.next());
// }
// }
while (iterator.hasNext()) {
String string = iterator.next();
System.out.println(string);
}
//第三种方式:使用lambda表达式、
System.out.println("-----3-使用lambda表达式------");
collection.forEach(e->System.out.println(e));//forEach
}
}
public class Demo3 {
public static void main(String[] args) {
Collection<String> c1 = new ArrayList<>();
c1.add("C");
c1.add("C++");
c1.add("java");
Collection<String> c2 = new ArrayList<String>();
c2.add("jsp");
c2.add("php");
c2.add("java");
boolean b = c1.containsAll(c2);
System.out.println(b);
boolean b2 = c1.addAll(c2);
c1.forEach(str -> System.out.println(str));
System.out.println("------------");
c1.removeAll(c2);
c1.forEach(str -> System.out.println(str));
}
}
5、ArrayList
1、定义方法
List< String > list = new ArrayList<>();
♥list的特殊之处:存数据和取数据用set()、get()方法
public class Demo4 {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("hello");
list.add(0,"abc");
list.forEach(str->System.out.println(str));
System.out.println("----------------");
list.set(1, "1111");
list.forEach(str->System.out.println(str));
System.out.println("-------2----------");
list.remove(0);
list.forEach(str->System.out.println(str));
System.out.println("-------jieshu---------");
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
}
}
2、排序的两种方式
在Sort中加入实现Comparable接口的compare()或者Comparator接口compareTo() 方法
public class Student0805 {
private String name;
private int score;
public Student0805() {
}
public Student0805(String name,int score) {
this.name = name;
this.score = score;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
@Override
public String toString() {
return "[name=" + name + ", score=" + score + "]";
}
}
public class Student08052 implements Comparable<Student08052>{
private String name;
private int score;
public Student08052() {
}
public Student08052(String name,int score) {
this.name = name;
this.score = score;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
@Override
public String toString() {
return "[name=" + name + ", score=" + score + "]";
}
@Override
public int compareTo(Student08052 o) {
// TODO Auto-generated method stub
return this.score - o.score;
}
}
public class DemoStuedent {
public static void main(String[] args) {
List<Student0805> stulist = new ArrayList<>();
stulist.add(new Student0805("zhangsan",90));
stulist.add(new Student0805("lisi", 40));
stulist.add(new Student0805("wangwu", 60));
Collections.sort(stulist,
(stu1,stu2) -> stu1.getScore()-stu2.getScore());
System.out.println(stulist);
List<Student08052> stulist2 = new ArrayList<>();
stulist2.add(new Student08052("zhangsan",90));
stulist2.add(new Student08052("lisi", 40));
stulist2.add(new Student08052("wangwu", 60));
Collections.sort(stulist2);
System.out.println(stulist2);
}
}
6、集合数组之间相互转换
public class Demo5 {
//集合转换成数组
public static void test1() {
List<String> list = new ArrayList<>();
list.add("hello");
list.add("abc");
list.add("bcd");
list.add("zdd");
List<Integer> list2 = new ArrayList<Integer>();
list2.add(1);
list2.add(18);
list2.add(2);
list2.add(12);
Object[] objects = list2.toArray();//转换为数组
int sum = 0;
for (Object object: objects) {
sum += (Integer)object;
}
System.out.println(sum);
Integer[] li =list2.toArray(new Integer[list2.size()]);
for (Integer i :li) {
sum += i;
}
Arrays.sort(li);
System.out.println(Arrays.toString(li));
String[] str2 = new String[list.size()];
Arrays.sort(list.toArray(str2));
System.out.println(Arrays.toString(str2));
}
// static Comparator com = new Comparator<String>() {
//
// @Override
// public int compare(String o1, String o2) {
// //
// //return o1.length() - o2.length();
// return o2.length() - o1.length() ;
// }
//
//
// };
//数组转化为集合asList()
//给集合排序使用的是Collections中的sort()方法
public static void test2() {
String[] strings = {"hello","abjjgc","bdes"};
List<String> list = Arrays.asList(strings);
Collections.sort(list);
System.out.println(list);
//Collections.sort(list,com);
Collections.sort(list,(o1,o2)-> o1.length() - o2.length() );
System.out.println(list);
}
public static void main(String[] args) {
test2();
}
}