Subtotal for Collection Programming

1. Knowledge points: ArrayList, Iterator 
package collections; 

import java.util.ArrayList; 
import java.util.Iterator; 

/** 
 * @author Eightn0 
 * @create 2021-03-14 19:43 
 * 1) Encapsulate a news class , Including the title and content attributes, provide get and set methods, rewrite the toString method, and only print the title when printing the object; (10 points) 
    2) Only provide a constructor with parameters, and only initialize the title when instantiating the object; and Instantiate two objects: 
    News 1: China is covered by smog and air quality has become a hot topic again. 
    News 2: The Spring Festival is approaching Beijing’s " House Selling Hot" 
    3) Add news objects to the ArrayList collection and use ListIterator to traverse in reverse order; 
    4) In the process of traversing the collection, the news headlines are processed, and only the first 14 words are reserved for more than 15 words, and then "..." is added at the back 
    5) The processed news headlines are printed and traversed on the console; 
 */ 
class News{ 
    private String title; 
    private String body; 

    public String getTitle() { 
        return title; 
    }

    public void setTitle(String title) { 
        this.title = title; 
    } 

    public String getBody() { 
        return body; 
    } 

    public void setBody(String body) { 
        this.body = body; 
    } 

    @Override 
    public String toString() { 
        return " News{" + 
                "title='" + title +'\'' + 
                '}'; 
    } 

    public News(String title) { 
        this.title = title; 
    } 
} 

public class NewsList { 
    public static void main(String[] args ) { 
        News news1 = new News("Smog-shrouded air quality in many parts of China has become a hot topic again");
        News news2 = new News("春节临近北京“卖房热”");

        ArrayList arrayList = new ArrayList();
        arrayList.add(news1);
        arrayList.add(news2);

        Iterator iterator = arrayList.iterator();
        while (iterator.hasNext()){
            News news = (News) iterator.next();
            if (news.getTitle().length() >= 15){
                System.out.println(news.getTitle().substring(1,14)+"...");
            }else {
                System.out.println(news.getTitle());
            }
        }
    }
}

2. The addition, deletion, modification, and traversal of map

/** 
 * @author Eightn0 
 * @create 2021-03-14 20:28 
 * Define a variable of the Map interface type, reference an implementation class, add key-value pairs, determine whether the collection contains a certain key value, 
 * pass a certain One key value gets the value value, delete the key-value pair through a key, add another map set to this map set, 
 * Determine whether it is empty, clear the set, return the number of elements in the set and other common operations. 
 * Traverse the map collection in the above question through two methods 
 */ 

public class MapExerciseOne { 
    @Test 
    public void test(){ 
        //Define a variable of 
        Map interface type Map map = new HashMap(); 

        //Add key-value pair 
        map .put("AA",1); 
        map.put("BB",12); 
        map.put("CC",123); 
        map.put("DD",1234); 
        map.put("EE" ,12345); 

        //Judging whether the set contains a key value 
        System.out.println(map.containsKey("AA"

        // 
        Get the value value through a certain key System.out.println(map.get("AA")); 

        //Remove the key-value pair through a certain key 
        map.remove("BB"); 

        // Remove another The map collection is added to this map collection 
        Map map1 = new HashMap(); 
        map1.put(123,"AA"); 
        map1.put(1234,"BA"); 
        map1.put(12345,"BB"); 
        map. putAll(map1); 

        //Determine whether it is empty, clear the collection, and return the number of elements in the collection 
        System.out.println(map.isEmpty()); 
        map1.clear(); 
        System.out.println(map.size ()); 

        //Traverse the map collection in the above question through two methods 
        //map itself has no traversal method, but the key is essentially set, and the value is essentially collection, and corresponds to key 
        Set set = map.keySet(); 
        Iterator iterator = set.iterator(); 
        while (iterator.hasNext()){
            Object key = iterator.next();
            Object value = map.get(key);
            System.out.println(key+"=="+value);
        }


        System.out.println("****************************");

        Set entryset = map.entrySet();
        Iterator iterator1 = entryset.iterator();
        while (iterator1.hasNext()){
            Map.Entry entry = (Map.Entry)iterator1.next();
            System.out.println(entry.getKey() + "==" + entry.getValue());
        }
    }
}

3. Random number generation, hashset, arraylist, foreach

** 
 * @author Eightn0 
 * @create 2021-03-14 21:00 
 * 1. Generate 10 random numbers with a value between 100 and 200; 
    2. Store these ten numbers in the HashSet collection (it is possible to collect The length is less than 10). 
    3. Convert this HashSet collection into an ArrayList collection 
    4. Re-sort the ArrayList collection, in order from 
smallest to largest; 
    5. Use foreach to traverse the collection; 
 */ public class NumbersExer { 
    @Test 
    public void test(){ 
        // Change this Ten numbers are stored in the HashSet collection ( 
        HashSet set = new HashSet(); 
        //Generate 10 random numbers with values ​​between 100 and 200 
        for (int i = 0; i <10; i++) { 
            int num = ( int)(Math.random() * ( 
            200-100 ) + 100); set.add(num); 
        } 
        //Convert this HashSet collection into an ArrayList collection
        ArrayList list = new ArrayList(set);

        //Re-sort the ArrayList collection, in ascending order 
        Collections.sort(list); 

        //Use foreach to traverse the collection; 
        for (Object o: list) { 
            System.out.println(o); 
        } 

    } 
}

Guess you like

Origin blog.csdn.net/vivian233/article/details/114801971