Set framework
List and Set are sub-interfaces of Collection interface.
- Collection interface stores a set of non-unique, disordered objects.
- List interface stores a set of non-unique, ordered (insertion order) objects.
- The set interface stores a set of unique, disordered objects.
Map does not inherit COllection. The interface stores a set of key-value objects and provides a mapping from key to value.
List interface
- ArrayList and LikedLiet are implementation classes of List
- ArrayList implements variable-length arrays to allocate continuous space in memory. Traversal elements and random access elements are more efficient.
- LinkedList uses linked list storage. Efficiency of inserting and deleting elements.
ArrayList
- The Construction Method of ArrayList
- ArrayList (int size);
- ArrayList (Colection c);
- ArrayList ();
- Common methods of ArrayList.
- .add();
- remove(); // can be index and Object types. Object removes the specified element that appears for the first time.
- .indexOf("ab");
- .lastIndexOf("a");
- .size();
- .get(index);
- set(idnex,obj); // Modify the value of the set
- Contains (Object o)//list contains object o;
- clear // Remove all elements;
ArrayList list = new ArrayList();
list.add("aaa");
list.add("ccc");
list.add(2,"ccc");
for (int i=0;i<list.size();i++) {
String obj =(String) list.get(i);
System.out.orintln(obj);
}
list.set(1,"bbb"); //Set modification.
list.remove(2); //Delete the value of the collection by index. list.remove("ccc") is deleted by value
System.out.println();
for (Object obj:list){
System.out.println(obj);
}
LikedList
- Headlines and last order can be added.
- LinkedList class is a concrete implementation class of List interface
- LinkedList class for creating linked list data structures
- When the latter is inserted to delete elements, it provides better mental energy.
Common methods of LikedList
addFirst(Object o) adds elements at the top of the list
addLast(Object o) adds elements at the end of the list
getFirst() returns the first element in the list
getLast() returns Object;
removeFirst() returns Object;
removeLast() returns Object;
.size();
Set interface
- Set interface stores a set of unique, disordered objects
- HashSet is a common implementation class of Set interface
- Store references to objects in Set s
HashSet
- An unordered set traversal method.
- Traversal method 1: Traverse by for eat
for (Object obj :hashset){
String str = (HashSet)obj;
System.out.println(str);
}
- Traversal Method 2: Traversing through an iterator
Iterator iterator = hashset.iterator();
while(iterator.hasNext()){
Object obj= iterator.next();
String str = (String)obj;
System.out.println(str);
}
Example:
- Traversal method 1: Traverse by for eat
import java.util.HashSet;
import java.util.Iterator;
/**
* Create a Dog class
* @author Wondf
*
*/
class Dog{
private String name;
private String type;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public Dog() {
}
public Dog(String name, String type) {
super();
this.name = name;
this.type = type;
}
public String toString() {
return "Dog [name=" + name + ", type=" + type + "]";
}
}
public class DogHashSet {
public static void main(String[] args) {
HashSet<Dog> hashSet = new HashSet<Dog>();
for (int i = 0; i < 100; i++) {
Dog dog = new Dog("name"+i,"Dog"+i);
hashSet.add(dog);
}
System.out.println("forEach ergodic");
showForEach(hashSet);
System.out.println("Iterator traversal");
showIterator(hashSet);
}
private static void showIterator(HashSet<Dog> hashSet){
Iterator<Dog> iterator = hashSet.iterator();
while (iterator.hasNext()){
Dog dog = iterator.next();
System.out.println(dog);
}
}
private static void showForEach(HashSet<Dog> hashSet) {
for (Dog dog :hashSet){
System.out.println(dog);
}
}
}
HashMap
- Implementation of Map Interface
- Used to store key/value mapping relationships;
- The storage order of its elements cannot be guaranteed.
- HashCode
- The concept of indexation
- The key key key is unique, null, but only one.
Common construction methods and methods of HashMap()
- .put(key,value); //Add and modify. - .size(); //Number of elements returned - .get(key); //Returns the associated value based on the key, and null if no specified key exists - .keySet() //Return Set, the collection of keys - .values //Returns Collection, the collection of values
Traversal examples:
import java.util.HashMap;
import java.util.Iterator;
import java.util.Scanner;
import java.util.Set;
/**
* Create Student Classes
* @author Wondf
*
*/
class Student {
private String name;
private String sex;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Student(){
super();
}
public Student(String name, String sex) {
super();
this.name = name;
this.sex = sex;
}
@Override
public String toString() {
return "Student [name=" + name + ", sex=" + sex + "]";
}
}
public class JobHashMap {
public static void main(String[] args) {
HashMap<String, Student> hashMap = new HashMap<String, Student>();
for (int i = 0; i < 5; i++) {
Student student = new Student("nanme" + i, "male");
hashMap.put("enName" + i, student);
}
System.out.println("Please enter the English name of the student you are looking for:");
Scanner scanner = new Scanner(System.in);
String enName = scanner.next();
Student student = hashMap.get(enName);
System.out.println("The student information you are looking for is:" + "\n" + student);
System.out.println("*****************");
System.out.println("Enhance for ergodic");
for (String key : hashMap.keySet()) {
Student stu = hashMap.get(key);
System.out.println(stu);
}
System.out.println("Iterator traversal");
Set<String> keySet = hashMap.keySet();
Iterator<String> iterator = keySet.iterator();
while (iterator.hasNext()) {
String obj = iterator.next();
Student stu = hashMap.get(obj);
System.out.println(stu);
}
}
}
Additionally, by searching sequentially, you can first create an ArrayList, store the value of HashMap in it, and then traverse it.
Iterator iterator
.hasNext(); //Returns boolean, and true if there are elements that can be iterated over. .next(); //E returns the next element of the iteration. .remove(); //Remove the last element returned by the iterator from the collection pointed by the iterator.
Java Container Classes
generic paradigm
Generic set
- Generic sets can constrain the types of elements in a set
Set + Generic==> Generic Set
Safety and efficiency.
- ArrayList <Integer> = new ArrayList <Integer> (); //Types must be wrapper classes
- HashMap <String,String> = new HashMap <String,string>();
- Interface can not be instantiated, only through its implementation class.
// Traversal method of HashMap 2:
for (Map.Entry<String,student> entry :list.entrySet()){
String key =entity.getKey();
Student value =entity.getValue();
System.out.println("key="+key+",value+"+value);
}
generic class
"`
public classs pair {
max ;
min ;
}
generic method
To define a generic method, simply place the generic parameter list before the return value. Such as: