当前位置:网站首页>Set code exercise

Set code exercise

2022-06-27 21:39:00 continueLR

Catalog

ArrayList Code

 HashSet  Code

TreeSet Code

HashMap Code  

 Properties Code


List Set code :ArrayList And LinkedList

package com.java.jiheWork;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
/*
    1.1、 The creation of each collection object (new)
	1.2、 Add elements to the collection 
	1.3、 Take an element out of a collection 
	1.4、 Ergodic set 
 */
public class ArrayListTest {
    public static void main(String[] args) {
        //  Create a collection object 
        ArrayList<String> list = new ArrayList<>();
        //  Additive elements 
        list.add(" Zhang San ");
        list.add(" Li Si ");
        list.add(" Wang Wu ");
        //  Take an element out of a collection 
        // List Sets have subscripts 
        String firstElt = list.get(0);
        System.out.println(firstElt);
        System.out.println(list.get(2));
        System.out.println("-------------------------------");
        //  Traverse ( Subscript mode )
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }
        System.out.println("-------------------------------");
        //  Traverse ( Iterator mode , This one is universal , all Collection Can be used )
        Iterator<String> it = list.iterator();
        while (it.hasNext()) {
            System.out.println(it.next());
        }
        System.out.println("-------------------------------");

        // while Change the loop to for loop 
        /*for(Iterator<String> it2 = list.iterator(); it2.hasNext(); ){
            System.out.println("====>" + it2.next());
        }*/

        //  Traverse (foreach The way )
        for (String s : list) {
            System.out.println(s);
        }
        System.out.println("-------------------------------");
        LinkedList<Student> students = new LinkedList<Student>();
        Student S1 = new Student(111," Zhang San ");
        Student S2 = new Student(122," Li Si ");
        Student S3 = new Student(123," Wang Wu ");
        students.add(S1);
        students.add(S2);
        students.add(S3);
        Iterator<Student> it2 = students.iterator();
        while (it2.hasNext()){
            System.out.println(it2.next());
        }
        System.out.println("-------------------------------");
        for (Student ST:students) {
            System.out.println(ST);
        }
    }
}

  test result

  summary :ArrayList At the bottom of the collection is the array , You can find data directly by subscript , Easy to find and modify . But when you add or delete elements, you need to move other elements , Low efficiency .

LinkedList At the bottom of the collection is the linked list , There is no need to move elements when adding or deleting , Easy to add and delete . But every time you search, you have to go through the previous data , It's not easy to find .

 

 HashSet  Code

package com.bjpowernode.javase.review;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Objects;
import java.util.Set;

/*
    1.1、 The creation of each collection object (new)
	1.2、 Add elements to the collection 
	1.3、 Take an element out of a collection 
	1.4、 Ergodic set 
	1.5、 test HashSet Set characteristics : Disorder cannot be repeated .
 */
public class HashSetTest {
    public static void main(String[] args) {
        // Create a collection object 
        HashSet<String> set = new HashSet<>();
        // Additive elements 
        set.add("abc");
        set.add("def");
        set.add("king");
        set.add("abc");
        System.out.println(set.size());//3
        // Here we find set The number of elements is only 3 individual , Because duplicate elements cannot be added to set In the collection 
        System.out.println("------------------------");
        // Traversal methods cannot use ordinary for loop , because set Set has no subscript 
        // Traversal methods 1: Iterator mode 
        Iterator<String> it = set.iterator();
        while (it.hasNext()){
            System.out.println(it.next());
        }
        System.out.println("------------------------");
        // Traversal methods 2:foreach The way 
        for (String s:set) {
            System.out.println(s);
        }
        System.out.println("------------------------");
        // establish Student Class collection objects 
        HashSet<Student> st = new HashSet<>();
        // establish Student object , among s3 And s1 repeat 
        Student s1 = new Student(111," Zhang San ");
        Student s2 = new Student(222," Li Si ");
        Student s3 = new Student(111," Zhang San ");
        st.add(s1);
        st.add(s2);
        st.add(s3);
        Iterator<Student> Stu = st.iterator();
        while (Stu.hasNext()){
            System.out.println(Stu.next());
        }
        System.out.println("------------------------");
            for (Student stu:st) {
                System.out.println(stu);
            }
        }
    }


class Student {
    int no;
    String name;

    public Student() {
    }

    public Student(int no, String name) {
        this.no = no;
        this.name = name;
    }

    @Override
    public String toString() {
        return "Student{" +
                "no=" + no +
                ", name='" + name + '\'' +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return no == student.no &&
                Objects.equals(name, student.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(no, name);
    }
}

 HashSet Set characteristics : The bottom layer is a hash table , Disorder cannot be repeated . Duplicate elements cannot be added to the collection .

TreeSet Code

package com.bjpowernode.javase.review;

import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;

/*
    1.1、 The creation of each collection object (new)
	1.2、 Add elements to the collection 
	1.3、 Take an element out of a collection 
	1.4、 Ergodic set 
	1.5、 test TreeSet The elements in a collection are sortable .
	1.6、 test TreeSet The type stored in the collection is custom .
	1.7、 Test implementation Comparable How to interface 
	1.8、 Test implementation Comparator How to interface ( It's best to test the following anonymous inner classes )
 */
public class TreeSetTest {
    public static void main(String[] args) {
        //  Set creation ( You can test the following TreeSet Store... In a collection String、Integer Of . These classes are SUN Written .)
        //TreeSet<Integer> ts = new TreeSet<>();

        //  Writing a comparator can change the rules .
        TreeSet<Integer> ts = new TreeSet<>(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2 - o1; //  Automatic dismantling 
            }
        });

        //  Additive elements 
        ts.add(1);
        ts.add(100);
        ts.add(10);
        ts.add(10);
        ts.add(10);
        ts.add(10);
        ts.add(0);

        //  Traverse ( Iterator mode )
        Iterator<Integer> it = ts.iterator();
        while(it.hasNext()) {
            Integer i = it.next();
            System.out.println(i);
        }

        //  Traverse (foreach)
        for(Integer x : ts){
            System.out.println(x);
        }

        // TreeSet Collection to store custom types 
        TreeSet<A> atree = new TreeSet<>();

        atree.add(new A(100));
        atree.add(new A(200));
        atree.add(new A(500));
        atree.add(new A(300));
        atree.add(new A(400));
        atree.add(new A(1000));

        //  Traverse 
        for(A a : atree){
            System.out.println(a);
        }

        //TreeSet<B> btree = new TreeSet<>(new BComparator());
        //  Anonymous inner class mode .
        TreeSet<B> btree = new TreeSet<>(new Comparator<B>() {
            @Override
            public int compare(B o1, B o2) {
                return o1.i - o2.i;
            }
        });

        btree.add(new B(500));
        btree.add(new B(100));
        btree.add(new B(200));
        btree.add(new B(600));
        btree.add(new B(300));
        btree.add(new B(50));

        for(B b : btree){
            System.out.println(b);
        }
    }
}

//  The first way : Realization Comparable Interface 
class A implements Comparable<A>{
    int i;

    public A(int i){
        this.i = i;
    }

    @Override
    public String toString() {
        return "A{" +
                "i=" + i +
                '}';
    }

    @Override
    public int compareTo(A o) {
        //return this.i - o.i;
        return o.i - this.i;
    }
}

class B {
    int i;
    public B(int i){
        this.i = i;
    }

    @Override
    public String toString() {
        return "B{" +
                "i=" + i +
                '}';
    }
}

//  The comparator 
class BComparator implements Comparator<B> {

    @Override
    public int compare(B o1, B o2) {
        return o1.i - o2.i;
    }
}

HashMap Code  

package com.bjpowernode.javase.review;

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

/*
    1.1、 The creation of each collection object (new)
	1.2、 Add elements to the collection 
	1.3、 Take an element out of a collection 
	1.4、 Ergodic set 
 */
public class HashMapTest {
    public static void main(String[] args) {
        //  establish Map aggregate 
        Map<Integer, String> map = new HashMap<>();
        //  Additive elements 
        map.put(1, "zhangsan");
        map.put(9, "lisi");
        map.put(10, "wangwu");
        map.put(2, "king");
        map.put(2, "simth"); // key repeat value Will be covered .
        //  Get the number of elements 
        System.out.println(map.size());
        //  take key yes 2 The elements of 
        System.out.println(map.get(2)); // smith
        //  Traverse Map Assembly is important , You have to be able to .
        //  The first way : Get all of it first key, Traverse key When , adopt key obtain value
        Set<Integer> keys = map.keySet();
        for(Integer key : keys){
            System.out.println(key + "=" + map.get(key));
        }

        //  The second way : Yes, it will Map Set to Set aggregate ,Set Each element in the set is Node
        //  This Node Node has key and value
        Set<Map.Entry<Integer,String>> nodes = map.entrySet();
        for(Map.Entry<Integer,String> node : nodes){
            System.out.println(node.getKey() + "=" + node.getValue());
        }
    }
}

 Properties Code

package com.bjpowernode.javase.review;

import java.util.Properties;

public class PropertiesTest {
    public static void main(String[] args) {
        //  Create objects 
        Properties pro = new Properties();
        //  save 
        pro.setProperty("username", "test");
        pro.setProperty("password", "test123");
        //  take 
        String username = pro.getProperty("username");
        String password = pro.getProperty("password");
        System.out.println(username);
        System.out.println(password);

    }
}

Set inheritance structure diagram  

原网站

版权声明
本文为[continueLR]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/178/202206271920421029.html