1.容器
1.1 Collection集合
集合按照其存储结构可以分成两大类,单列集合和双列集合。
其中 Collection是单列集合的父接口,主要的单列集合有List和Set。
Collection中主要的方法使用
Collection<String> c1=new ArrayList<String>();
//添加元素
c1.add("AA");
c1.add("BB");
c1.add("CC");
System.out.println("元素数量:"+ c1.size());
System.out.println(c1.isEmpty()); //判空
//移除元素
c1.remove("AA");
System.out.println("移除AA之后元素数量:"+c1.size());
System.out.println(c1.contains("AA"));
Object[] str= c1.toArray();
for (Object s : str) {
System.out.println(s);
}
c1.clear();
System.out.println(c1.isEmpty());
//迭代器
c1.add("DD");
c1.add("EE");
c1.add("FF");
Iterator<String> iterator=c1.iterator();
while(iterator.hasNext()){
String s=iterator.next();
System.out.println(s);
}
Collection c2=new ArrayList();
c2.add("String");
c2.add("ASDF");
c2.add(123);
c2.add(new Person(10,"AA"));
for (Object o : c2) {
System.out.println(o);
}
1.2 List
List中主要有两大类:
ArrayList 和LinkedList;基本实现分别为数组和双向链表
public static void main(String[] args) {
ArrayList<Integer> arr=new ArrayList<>();
arr.add(12);
arr.add(13);
for (Integer integer : arr) {
System.out.println(integer);
}
List<Integer> list=new LinkedList<>();
list.add(123);
list.add(456);
Iterator<Integer> iterator=list.iterator();
while(iterator.hasNext()){
Integer it=iterator.next();
System.out.println(it);
}
}
1.3 Set
Set中主要介绍HashSet的使用
/**
* Set中元素无序,且不可重复
* 无带索引的方法
*遍历可用迭代器,增强for
*
* HashSet 底部结构 哈希表
*/
public class Test6 {
public static void main(String[] args) {
String s1=new String("abcd");
String s2=new String("abcd");
System.out.println(s1==s2); //false
System.out.println(s1.equals(s2)); //true
System.out.println(s1.hashCode()); //2987074 hashcode相同
System.out.println(s2.hashCode()); //2987074
HashSet<String> set1=new HashSet<>();
set1.add("abcd");
set1.add(s1);
set1.add(s2);
System.out.println(set1.size()); //size==1
}
}
1.4 Collections工具类
ArrayList<Person> list=new ArrayList<>();
Collections.addAll(list,new Person(10,"小明"),new Person(10,"大李"),new Person(9,"张帅"),
new Person(100,"李林"));
Collections.sort(list,
new Comparator<Person>() {
@Override
public int compare(Person o1, Person o2) {
if (o1.getAge() != o2.getAge())
return o1.getAge() - o2.getAge();
else {
return o1.getName().charAt(0) - o2.getName().charAt(0);
}
}
});
System.out.println(list);
}
1.5 map
HashMap的底层实现为(数组+链表 | 红黑树)
map中的常用方法:
public static void main(String[] args) {
Map<String,Integer> map=new HashMap<>();
//
map.put("小张",10);
map.put("小明",20);
map.put("小红",30);
System.out.println("小红在吗? "+map.containsKey("小红"));
//由key获取value
int value=map.get("小红");
System.out.println(value);
//遍历map 方法一
Set<String> set=map.keySet();
for(String key:set){
int age=map.get(key);
System.out.println("name="+key+" age="+age );
}
//方法二
Set<Map.Entry<String,Integer>> entryset=map.entrySet();
for (Map.Entry<String, Integer> stringIntegerEntry : entryset) {
System.out.println(stringIntegerEntry.getKey()+stringIntegerEntry.getValue());
}
}
2.泛型
泛型的使用:在类或者方法中预知的使用未知类型
泛型:可以定义泛型类,接口,泛型方法
定义泛型类
class MyGenericClass<T,E>{
private T name;
private E id;
public MyGenericClass() {
this.name = name;
this.id = id;
}
public T getName(){
return name;
}
public void setName(T name){
this.name=name;
}
public E getId(){
return this.id;
}
public void setId(E id){
this.id=id;
}
}
定义泛型接口
interface MyGereInter<E>{
E getNum();
void setNum(E a);
}
//定义泛型接口,在类实现时仍不确定泛型,实例化时候确定泛型的类型
public class Test3<E> implements MyGereInter<E>{
private E num;
@Override
public E getNum() {
return num;
}
@Override
public void setNum(E a) {
this.num=a;
}
public static void main(String[] args) {
Test3<Integer> t1=new Test3<Integer>();
t1.setNum(123);
System.out.println(t1.getNum());
Test3_2 t2=new Test3_2();
t2.setNum("baby!");
System.out.println(t2.getNum());
}
}
//实现接口时确定泛型的类型
class Test3_2 implements MyGereInter<String>{
private String num;
@Override
public String getNum() {
return num;
}
@Override
public void setNum(String a) {
this.num=a;
}
}
泛型通配符
/**
* 定义泛型上下限
* 格式: 类型名称 <? extends 类 > 对象名称
* 意义: 只能接收该类型及其子类
*
*格式: 类型名称 <? super 类 > 对象名称
* 意义: 只能接收该类型及其父类型
*
*/