[] Java development series - a collection of use

Foreword

  In java provide us with a simple set of C ++ - like generics, list, set, map and so on. Here, a brief set of methods using these containers, and custom ordering element complex objects.

  First look at the framework map collection:

  Since the collection also inherited and comparable Iterator interface, so we can use the Iterator to traverse the elements, you can also customize compareTo function to re-write your own sort.

  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 }

operation result

out by for!
test1
test2
test3
out by iterator!
test1
test2
test3

Set, if you encounter duplicate elements, not added

 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 }

operation result

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 }

operation result

size 2
test1
1
2
size 1

Custom sort function

person classes , inheritance Comparable interface, function overloading 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 }

Test category

 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 }

operation result

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

 

Reproduced in: https: //my.oschina.net/u/204616/blog/545359

Guess you like

Origin blog.csdn.net/weixin_34101784/article/details/91989312