ArrayList l = new ArrayList();
l.add("a");
l.add("ab");
l.add("abc");
l.add(4);
Iterator it = l.iterator();
while(it.hasNext()){
String s = (String)it.next();
System.out.println(s);
}
ArrayList l = new ArrayList();
l.add("a");
l.add("ab");
l.add("abc");
l.add(4);
Iterator it = l.iterator();
while(it.hasNext()){
String s = it.next();
System.out.println(s);
}
泛型的格式,通过<>来定义要操作的引用数据类型。
class Worker
{
}
class Teacher
{
}
class Tools
{
private Object obj;
public Object getObject() {
return obj;
}
public void setObject(Object obj) {
this.obj = obj;
}
}
public class GenericTest
{
public static void main(String[] args) {
Tools t = new Tools();
t.setObject(new Worker());
Teacher tc = (Teacher)t.getObject();
}
}
使用泛型以后就可以避免这样的问题产生了。
class Utils
{
private T t;
public T getObject() {
return t;
}
public void setObject(T t) {
this.t = t;
}
}
public static void main(String[] args) {
Utils u = new Utils();
u.setObject(new Worker());
Worker w = u.getObject();
}
class Tools
{
public void show(T t){
System.out.println("this is show " + t);
}
public void print(E e){
System.out.println("this is print " + e);
}
}
public static void main(String[] args) {
Tools t = new Tools();
t.show("haha");
t.show(new Integer(4));
t.print("haha");
t.print(new Integer(4));
}
class Tools
{
public void show(T t){
System.out.println("this is show " + t);
}
public void print(E e){
System.out.println("this is print " + e);
}
}
//增
Map map = new HashMap();
map.put("01","zhangsan");
map.put("02","wangwu");
map.put("03","lisi");
System.out.println(map);
Map map1 = new HashMap();
map1.put("04", "chenqi");
map1.put("05", "yangba");
map.putAll(map1);
System.out.println(map);
//判断
System.out.println("判断是否为空:"+map.isEmpty());
System.out.println("判断是否包含指定键02:"+map.containsKey("02"));
System.out.println("判断是否包含指定值chenqi:"+map.containsValue("chenqi"));
//获取
System.out.println("03对应的值是" + map.get("03"));//根据键获取值
System.out.println(map.size());//获取map集合键值对个数
System.out.println(map.keySet());//返回键的set集合
System.out.println(map.values());//返回值的 Collection 视图
System.out.println(map.entrySet());//返回此映射中包含的映射关系的 Set 视图
//删
System.out.println(map.remove("01"));//删除键01对应的键值对,如果成功,返回01对应的值,否则为null
System.out.println(map);
map.clear();//清空map中的所有键值对
System.out.println(map);
1.通过set集合
Map map = new HashMap();
map.put("01","zhangsan");
map.put("02","wangwu");
map.put("03","lisi");
for(String key: map.keySet()){//获取键的set集合
System.out.println("key:"+key+"..."+"value:"+map.get(key));
}
2.通过entrySet取出map集合中所有映射关系的Set集合
Map map = new HashMap();
map.put("01","zhangsan");
map.put("02","wangwu");
map.put("03","lisi");
Set> me = map.entrySet();
Iterator> it = me.iterator();
while(it.hasNext()){
Entry e = it.next();
System.out.println(e.getKey());
System.out.println(e.getValue());
}
class Student implements Comparable
{
private String name;
private int age;
Student(String name, int age){
this.name = name;
this.age = age;
}
@Override
public int hashCode(){
return name.hashCode() + age;
}
@Override
public boolean equals(Object obj){
if(!(obj instanceof Student)){
throw new ClassCastException("类型不匹配");
}
Student s = (Student) obj;
return s.name.equals(name) && s.age == age;
}
@Override
public String toString(){
return this.name + "...." + this.age;
}
@Override
public int compareTo(Student s){
int num = new Integer(age).compareTo(new Integer(s.age));
if(num == 0){
return this.name.compareTo(s.name);
}
return num;
}
}
//main方法
Map sm = new HashMap();
sm.put(new Student("zhangsan", 5), "beijing");
sm.put(new Student("zhangsan", 5), "tianjing");
sm.put(new Student("lisi", 6), "beijing");
sm.put(new Student("wangwu", 7), "beijing");
System.out.println(sm);
如果注释掉hashCode和equals方法:
class Student implements Comparable
{
private String name;
private int age;
Student(String name, int age){
this.name = name;
this.age = age;
}
// @Override
// public int hashCode(){
// return name.hashCode() + age;
// }
// @Override
// public boolean equals(Object obj){
// if(!(obj instanceof Student)){
// throw new ClassCastException("类型不匹配");
// }
// Student s = (Student) obj;
// return s.name.equals(name) && s.age == age;
// }
@Override
public String toString(){
return this.name + "...." + this.age;
}
@Override
public int compareTo(Student s){
//先按照年龄排序
int num = new Integer(age).compareTo(new Integer(s.age));
if(num == 0){
//年龄相同时按照姓名排序
return this.name.compareTo(s.name);
}
return num;
}
}
Map tm = new TreeMap();
tm.put(new Student("zhangsan", 5), "beijing");
tm.put(new Student("lisi", 5), "beijing");
tm.put(new Student("wangwu", 7), "beijing");
tm.put(new Student("zhaoliu", 8), "beijing");
for(Student stu: tm.keySet()){//获取键的set集合
System.out.println("stu:" + stu + "..." + "addr:" + tm.get(stu));
}
List> l = new ArrayList>();
List l1 = new ArrayList();
List l2 = new ArrayList();
l1.add("l101");
l1.add("l102");
l2.add("l201");
l2.add("l202");
l.add(l1);
l.add(l2);
System.out.println(l);
List中嵌套Map
List
Map> ml = new HashMap>();
List l1 = new ArrayList();
List l2 = new ArrayList();
l1.add("01");
l1.add("02");
l2.add("01");
l2.add("02");
ml.put("l1",l1);
ml.put("l2",l2);
System.out.println(ml);
Map> m = new HashMap>();
Map m1 = new HashMap();
Map m2 = new HashMap();
m1.put("01", "m1");
m1.put("02", "m1");
m2.put("01", "m2");
m2.put("02", "m2");
m.put("m1", m1);
m.put("m2", m2);
System.out.println(m);
注意:嵌套可以多层,层数也没有限定,但是不建议对集合进行多层嵌套,会降低代码的可读性和维护性。
List l = new ArrayList();
l.add(3);
l.add(1);
l.add(2);
System.out.println(l);
Collections.sort(l);
System.out.println(l);
Collections.reverse(l);
System.out.println(l);
List l1 = new ArrayList();
l1.add(1);
l1.add(2);
l1.add(3);
System.out.println(l1);
Collections.swap(l1, 1, 2);
System.out.println(l1);
List l2 = new ArrayList();
l2.add(1);
l2.add(2);
l2.add(3);
System.out.println(l2);
Collections.fill(l2, 0);
System.out.println(l2);
List l3 = new ArrayList();
l3.add(1);
l3.add(2);
l3.add(3);
l3.add(4);
System.out.println(Collections.binarySearch(l3, 3));
System.out.println(Collections.binarySearch(l3, 5));
List l4 = new ArrayList();
l4.add("aaaaa");
l4.add("bbbb");
l4.add("ccc");
System.out.println(Collections.max(l4));
class StrComparator implements Comparator
{
@Override
public int compare(String str1, String str2) {
return new Integer(str1.length()).compareTo(new Integer(str2.length()));
}
}
//main方法
List l4 = new ArrayList();
l4.add("aaaaa");
l4.add("bbbb");
l4.add("ccc");
System.out.println(Collections.max(l4, new StrComparator()));
List l4 = new ArrayList();
l4.add("aaaaa");
l4.add("bbbb");
l4.add("ccc");
Collections.replaceAll(l4, "ccc", "ddd");
System.out.println(l4);
Integer[] arr = {2,3,4};
List l = Arrays.asList(arr);
System.out.println(l);