Collections

Collection classes

 Types of collection

   -Collection

   -Map

 ArrayList

 HashMap

 Iterator

Java Arrays

  Declaring an array int[] myArray;

  int[] myArray = new int[5];

  String[] stringArray = new String[10];

  String[] strings = new String[] {“one”, “two”};

  Checking an arrays length

  int arrayLength = myArray.length;

  Looping over an array

  for(int i=0; i<myArray.length; i++) {

    String s = myArray[i];

  }

Or

  for(String a: myArray) String s=a;

 

Advantages of arrays

  Very efficient, quick to access and add to

  Type-safe, can only add items that match the declared type of the array

Disadvantages of arrays

  Fixed size, some overhead in copying/resizing

  Can’t tell how many items in the array, just how large it was declared to be Limited functionality, need more general functionality

 

Java Collections

  The Java Collections API's provide Java developers with a set of classes and interfaces that makes it easier to handle collections of objects.

  A bit like arrays, but their size can change dynamically, and they have more advanced behaviour than arrays.

  Part of the java.util package.

Advantages

  Very flexible, can hold any kind of object

Disadvantages

  Not as efficient as arrays (for some uses)

Collection interfaces:

  Collections List: an ordered collection permitting duplicates

  Set: a collection that contains no duplicate elements.

   Maps: describes a mapping from keys to values, without duplicate keys.

      

 

Iterator

 

  Getting all elements from a collection:

  For Lists, we could use a for loop, and loop through the list to get() each item But this doesn’t work for Maps.

  To allow generic handling of collections, Java defines an object called an Iterator

  An object whose function is to walk through a Collection of objects and provide access to each object in sequence

  Iterator objects have three methods:

    next() – gets the next item in the collection

    hasNext() – tests whether it has reached the end

    remove() – removes the item just returned

 

  Iterator it = myList.iterator();

   while( it.hasNext() ) {

    System.out.print((String)it.next());

   }

 

Set:

  Provides an un-ordered collection of unique objects

  Does NOT allow duplicates

HashSet:

  HashSet<Integer> numbers = new HashSet();

  Scanner in=new Scanner(System.in);

  while(in.hasNextInt()){

    Integer num = in.nextInt();

    numbers.add(num);

  }

  for(Integer i: numbers){

    System.out.println(i);

  }

List:

  Ordered and indexed collection

  May contain duplicates

ArrayList:

  ArrayList myList = new ArrayList();

  myList.add(“A”);

  myList.add(Integer.valueOf(“1”));

  myList.add( 1, “C”);

 

  Replacing an element :

    myList.set ( 2, “ Milan”);

  Getting the elements:

    myList.get(1);

    Object o = myList.get(1);

    String s = (String) myList.get(1);

  Removing elements:

    myList.remove ( 1 );

     myList.remove ( “A”);

LinkedList:

  Stores each object in a separate link

  Each link stores the reference of the next/previous element in the sequence

  Inexpensive to remove an element from the middle

 

Map:

   Based on key value pair and hashing

    No duplicated keys!!!

Constructors :

  Map myMap = new HashMap();

  HashMap myMap = new HashMap();

Adding elements:

  myMap. put ( “One”, new Integer(1) );

Getting elements:

  Integer i= (Integer)myMap. get ( “One” );

 

Remove elements:

  myMap.remove(“One”);

 

Get all keys:

  Set keySet=myMap.keySet();

 Get all values:

  Collection valueCollection=myMap.values();

 1      HashMap hm=new HashMap();      
 2         ArrayList keyList=new ArrayList();
 3         ArrayList valueList=new ArrayList(); 
 4        
 5         hm.put(“trademe","www.trademe.co.nz");                
 6         hm.put(“aut",“www.aut.ac.nz");        
 7         hm.put(“grab1","http://www.grabone.co.nz");
 8         hm.put(“stuff","stuff.co.nz");
 9 
10 
11         for(Object s: hm.keySet()){
12             System.out.println((String)s);
13             keyList.add((String)s);
14             
15         }
16         for(Object v: hm.values()){
17             System.out.println((int)v);
18             valueList.add((String)v);
19         }

 

 

 

 

 

例子:

Student stuA=new Student(123);
Student stuB=new Student(123);

System.out.println(stuA.equals(stuB));

System.out.println(stuA==stuB);

HashSet hs=new HashSet();
hs.add(stuA);
hs.add(stuB);

System.out.println(hs.size());
public class Student {
    int id;
    public Student(int n){
        this.id=n;    
    }
}

False False 2

 

 

 

Student stuA=new Student(123);
Student stuB=new Student(123);

System.out.println(stuA.equals(stuB);

System.out.println(stuA= =stuB);

HashSet hs=new HashSet();
hs.add(stuA);
hs.add(stuB);

System.out.println(hs.size());
 1 public class Student {
 2     int id;   
 3     public Student(int n){
 4          this.id=n;    
 5     }    
 6     public boolean equals(Object o){    
 7         if(o == null)   return false;
 8         if(!(o instanceof Student)) return false;
 9         Student other = (Student) o;
10         return (this.id == other.id);
11     }
12     
13    public int hashCode(){
14         int hashCode = 1;
15         hashCode=37*hashCode+this.id;        
16         return hashCode;
17     } 
18 }

True False 1

 

posted @ 2017-12-18 18:54  CaiCongyu  阅读(176)  评论(0编辑  收藏  举报