前言
在java中提供给我们了一些类似C++泛型的简单集合,list,set,map等。这里,简单介绍一下这些集合容器的使用方法,以及复杂对象元素的自定义排序。
首先看一下集合的框架图:
由于collection也继承了Iterator和comparable接口,因此我们可以使用Iterator来遍历元素,也可以通过自定义compareTo函数来重新编写自己的排序。
List
1 package testCollection; 2 3 import java.util.ArrayList; 4 import java.util.Iterator; 5 import java.util.List; 6 7 public class testList { 8 public static void main(String[] args){ 9 List list = new ArrayList(); 10 list.add("test1"); 11 list.add("test2"); 12 list.add("test3"); 13 14 System.out.println("out by for!"); 15 for(Object o : list){ 16 System.out.println(o); 17 } 18 19 System.out.println("out by iterator!"); 20 Iterator iterator = list.iterator(); 21 while(iterator.hasNext()){ 22 String element = (String)iterator.next(); 23 System.out.println(element); 24 } 25 } 26 }
运行结果
out by for! test1 test2 test3 out by iterator! test1 test2 test3
Set,如果遇到重复的元素,不会添加
1 package testCollection; 2 3 import java.util.HashSet; 4 import java.util.Set; 5 6 public class testSet { 7 public static void main(String[] args){ 8 Set set = new HashSet();//使用set一般都是用hashset,这个会快一些 9 set.add("test1"); 10 set.add("test2"); 11 if(set.add("test2")){ 12 System.out.println("add successful"); 13 }else{ 14 System.out.println("add failed"); 15 } 16 } 17 }
运行结果
add failed
Map
1 package testCollection; 2 3 import java.util.HashMap; 4 import java.util.Map; 5 import java.util.Set; 6 7 public class testMap { 8 public static void main(String[] args){ 9 Map map = new HashMap(); 10 11 map.put(1, "test1"); 12 map.put(2, "test2"); 13 14 System.out.println("size "+map.size()); 15 System.out.println(map.get(1)); 16 17 Set keys = map.keySet(); 18 for(Object key : keys){ 19 System.out.println(key); 20 } 21 22 map.remove(2); 23 System.out.println("size "+map.size()); 24 } 25 }
运行结果
size 2 test1 1 2 size 1
自定义排序函数
person类,继承Comparable接口,重载compareTo函数
1 package testCollection; 2 3 public class Person implements Comparable{ 4 private String name; 5 private int age; 6 public String getName() { 7 return name; 8 } 9 public void setName(String name) { 10 this.name = name; 11 } 12 public int getAge() { 13 return age; 14 } 15 public void setAge(int age) { 16 this.age = age; 17 } 18 public Person(String name,int age){ 19 this.name = name; 20 this.age = age; 21 } 22 @Override 23 public int compareTo(Object person) throws ClassCastException { 24 if(!(person instanceof Person)){ 25 throw new ClassCastException("A Person perspected!"); 26 } 27 int age = ((Person)person).getAge(); 28 return this.age-age; 29 } 30 }
测试类
1 package testCollection; 2 3 import java.util.Arrays; 4 5 public class testComparable { 6 public static void main(String[] args){ 7 Person[] persons = new Person[4]; 8 persons[0] = new Person("test1",18); 9 persons[1] = new Person("test2",20); 10 persons[2] = new Person("test3",15); 11 persons[3] = new Person("test4",19); 12 13 System.out.println("before sorting!"); 14 15 for(Person p : persons){ 16 System.out.println("name: "+p.getName()+" age: "+p.getAge()); 17 } 18 19 System.out.println("after sorting!"); 20 Arrays.sort(persons); 21 for(Person p : persons){ 22 System.out.println("name: "+p.getName()+" age: "+p.getAge()); 23 } 24 } 25 }
运行结果
before sorting! name: test1 age: 18 name: test2 age: 20 name: test3 age: 15 name: test4 age: 19 after sorting! name: test3 age: 15 name: test1 age: 18 name: test4 age: 19 name: test2 age: 20
本文转自博客园xingoo的博客,原文链接:【java开发系列】—— 集合使用方法,如需转载请自行联系原博主。
时间: 2024-10-25 20:39:24