Translate

Friday, July 24, 2015

Java collections tutorial

The two most popular collections used in Java are List and Map. Both are generic interfaces. Java framework library provides many implementations of both these interfaces.

   The most popular implementation of List is the ArrayList class. ( If you are from the C# world, note that Arraylist in java has nothing to do with Arraylist in C#. ArrayList in java is strongly typed and more similar to List<T> in C#.).

The most popular implementation of Map in Java is the HashMap.



An example of List in java

 
import java.util.ArrayList;
import java.util.List;

public class Main {

    public static void main(String[] args) {

        List<Student> students = new ArrayList<Student>();

        Student student1 = new Student() {{
            setName("Jack");
            setCollege("MIT");
            setMajor("Computer Science");
            setSsn("123-45-5678");
        }};

        Student student2 = new Student() {{
            setName("Sandra");
            setCollege("Harvard");
            setMajor("Medicine");
            setSsn("999-45-5678");
        }};

        students.add(student1);
        students.add(student2); 

        for (Student s : students) {
            System.out.println(s.getName());
        }

    }
}

 
public class Student {

    private String name;
    private String college;
    private String major;
    private String ssn;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getMajor() {
        return major;
    }

    public void setMajor(String major) {
        this.major = major;
    }

    public String getCollege() {
        return college;
    }

    public void setCollege(String college) {
        this.college = college;
    }

    public String getSsn() {
        return ssn;
    }

    public void setSsn(String ssn) {
        this.ssn = ssn;
    }
}


Note here that Lists are allowed to have duplicates. In the above example you can enter the same student object multiple times into the List.





An example of Map in java

You use a Map when you want to look up values by a key. For example you could store people's information in a Map and look them up by their Social Security number (key).  

 
import java.util.HashMap;
import java.util.Map;

public class Main {

    public static void main(String[] args) {

        Map<String, Student> students = new HashMap<String, Student>();

        Student student1 = new Student() {{
            setName("Jack");
            setCollege("MIT");
            setMajor("Computer Science");
            setSsn("123-45-5678");
        }};

        Student student2 = new Student() {{
            setName("Sandra");
            setCollege("Harvard");
            setMajor("Medicine");
            setSsn("999-45-5678");
        }};

        students.put(student1.getSsn(), student1);
        students.put(student2.getSsn(), student2);


        System.out.println(students.get("999-45-5678").getName());//Sandra
    }
}



No comments:

Post a Comment

Comments will appear once they have been approved by the moderator