Hello LinkedList

in java •  8 years ago 

Rencently, I'm checking the sources of JAVA SDK, the LinkedList got me.
Here is it.


import java.util.ArrayList;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.function.Consumer;

/**
 * PrjName:JavaTest
 * Descrip:
 * <p>
 * Modification History:
 * Date                 Author      Version
 * ==========================================
 * 17-3-6 下午8:36
 * info:
 * _________________________________________
 */
public class TList {

    public static void main(String[] args){

//        TArrayList();
        TLinkedList();
    }
    static void TArrayList() {
        ArrayList<String> arrayList = new ArrayList<>(10);

        for (int i = 0; i < 9;i++) {
            arrayList.add("a" + (9 - i));

        }
        //print
        for (int i = 0; i < arrayList.size(); i++) {
            System.out.print(String.format("%s ", arrayList.get(i)));
        }
        System.out.println("");
        //排序比较器
        Comparator<String> cmp = new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return o1.compareTo(o2);
            }
        };

        arrayList.sort(cmp);

        //print
        for (int i = 0; i < arrayList.size(); i++) {
            System.out.print(String.format("%s ", arrayList.get(i)));
        }

        System.out.println("");
        //操作器
        Consumer<String> consumer1 = new Consumer<String>() {
            @Override
            public void accept(String s) {
                System.out.println(s+1);
            }
        };
        Consumer<String> consumer2 = new Consumer<String>() {
            @Override
            public void accept(String s) {
                System.out.println("--"+s);
            }
        };
        //操作器链接
        arrayList.forEach(consumer1.andThen(consumer2).andThen(consumer1));
         //print
        for (int i = 0; i < arrayList.size(); i++) {
            System.out.print(String.format("%s ", arrayList.get(i)));
        }

        System.out.println("");
        //================================linkedList
        LinkedList<String> linkedList = new LinkedList<String>();
        for (int i = 0; i < 10; i++) {
            linkedList.add("b"+i);
        }
    }

    static void TLinkedList() {
        //双向链表  也实现了Stack Queue的接口
        LinkedList<String> list = new LinkedList<>();

        list.add("1");
        list.add("2");
        list.add("3");
        for (String s : list) {
            System.out.println(s);
        }
        list.remove();
        list.forEach(p->System.out.println(p));
        System.out.println("---------");
        //Queue
        list.addFirst("4");
        list.addFirst("5");
        list.forEach(p->System.out.println(p));
        System.out.println("---------");
        System.out.println(list.removeLast());
        System.out.println(list.removeLast());
        System.out.println("---------");

        //Stack
        list.clear();
        list.push("1");
        list.push("2");
        list.push("3");
        while (!list.isEmpty()) {
            System.out.println(list.pop());
        }
    }
}```
Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Interesting
I will follow you to see your future posts!

Congratulations @zhongwenghao! You have received a personal award!

1 Year on Steemit
Click on the badge to view your own Board of Honor on SteemitBoard.

Upvote this notificationto to help all Steemit users. Learn why here!

Congratulations @zhongwenghao! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 2 years!

Click here to view your Board

Vote for @Steemitboard as a witness and get one more award and increased upvotes!

Congratulations @zhongwenghao! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 3 years!

You can view your badges on your Steem Board and compare to others on the Steem Ranking

Do not miss the last post from @steemitboard:

Downvote challenge - Add up to 3 funny badges to your board
Use your witness votes and get the Community Badge
Vote for @Steemitboard as a witness to get one more award and increased upvotes!
  ·  7 years ago Reveal Comment