Java的一些简单的demo
package com.ljquan.test.utils;
import java.util.*;
import java.util.concurrent.ConcurrentLinkedQueue;
public class test {
public static void main(String[] args) {
//队列
//队列要考虑线程安全,队列的信息是否完全被消费
concurrentLinkedQueueTest();
LinkedListQueueTest();
//栈
StackTest();
//数组列表string对象
ArrayListTest();
// map
mapTest();
}
/**
* 并发连接队列测试
*/
public static void concurrentLinkedQueueTest() {
System.out.println("==============");
//并发连接队列
Queue<Object> objects = new ConcurrentLinkedQueue<>();
objects.offer(1);
objects.offer(2);
objects.offer(3);
objects.offer(4);
boolean test = true;
while (test) {
Object poll2 = objects.poll();
if (poll2 == null) {
test = false;
break;
}
System.out.println("并发连接队列:" + poll2);
}
}
/**
* 链表队列测试
*/
public static void LinkedListQueueTest() {
System.out.println("==============");
//普通队列
Queue<Object> objectQueue = new LinkedList<>();
objectQueue.offer("入队列-数据1-》");
objectQueue.offer("入队列-数据2-》");
objectQueue.offer("入队列-数据3-》");
boolean objectQueue1 = true;
while (objectQueue1) {
Object poll2 = objectQueue.poll();
if (poll2 == null) {
objectQueue1 = false;
break;
}
System.out.println(poll2);
}
}
public static void StackTest() {
//栈
Stack<Object> objectStack = new Stack<>();
objectStack.push(1);
objectStack.push(2);
objectStack.push(3);
objectStack.push(4);
objectStack.push(5);
boolean obj = true;
System.out.println("==================");
while (obj) {
if (objectStack.isEmpty()) {
obj = false;
break;
}
Object pop = objectStack.pop();
System.out.println("stack输出======:" + pop);
}
}
/**
* 数组列表测试
*/
public static void ArrayListTest() {
System.out.println("==============");
//列表
ArrayList<Object> list = new ArrayList<>();
list.add("列表数据:" + 1);
list.add("列表数据:" + 2);
list.add("列表数据:" + 3);
//数组
int[] ints = new int[]{1, 2, 3, 4, 5, 6, 6};
for (int anInt : ints) {
System.out.println("数组数据:" + anInt);
}
//stringBuilder
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("hello");
stringBuilder.append("codel");
list.add(stringBuilder);
System.out.println(list + "输出");
}
/**
* map测验
*/
public static void mapTest() {
System.out.println("==============");
HashMap<Object, Object> hashMap = new HashMap<>();
hashMap.put(1, 1);
hashMap.put(2, 2);
hashMap.put(3, 3);
hashMap.put(4, 4);
hashMap.put(5, 5);
Set<Map.Entry<Object, Object>> entries = hashMap.entrySet();
for (Map.Entry<Object, Object> entry : entries) {
System.out.println("key值:" + entry.getKey().toString() + "键: " + entry.getValue());
}
}
}
输出:
==============
并发连接队列:1
并发连接队列:2
并发连接队列:3
并发连接队列:4
==============
入队列-数据1-》
入队列-数据2-》
入队列-数据3-》
==================
stack输出======:5
stack输出======:4
stack输出======:3
stack输出======:2
stack输出======:1
==============
数组数据:1
数组数据:2
数组数据:3
数组数据:4
数组数据:5
数组数据:6
数组数据:6
[列表数据:1, 列表数据:2, 列表数据:3, hellocodel]输出
==============
key值:1键: 1
key值:2键: 2
key值:3键: 3
key值:4键: 4
key值:5键: 5
Process finished with exit code 0