* 统计一个字符串中每个字符的数量
public static void main(String[] args) {
String str = "法国空军了哈哈flue日本今安afask徽山东分局并非拉三个地方预防";
printMap(string2Map(str));
}
/**
* 统计字符串中各个字符出现的次数,返回map形式
* @param str 输入随机字符串
*/
public static Map<Character, Long> string2Map (String str){
Map<Character, Long> map = new HashMap<Character, Long>();
char[] cs = str.toCharArray();
for (int i = 0; i < cs.length; i++) {
if (map.get(cs[i])==null) {
map.put(cs[i], 1L);
} else {
map.put(cs[i], map.get(cs[i])+1);
}
}
return map;
}
/**
* 遍历map 将键值对 打印到控制台
* @param map
*/
public static <T, E> void printMap(Map<T, E> map) {
Iterator<Entry<T, E>> it = map.entrySet().iterator();
while (it.hasNext()) {
Entry<T, E> entry = it.next();
System.out.println("\""+entry.getKey() + "\" 出现的次数是: " + entry.getValue());
}
}
* 单例模式
//通常,普通类的构造函数是公有的,外部类可以通过“new 构造函数()”来生成多个实例。
//但是,如果将类的构造函数设为私有的,外部类就无法调用该构造函数,也就无法生成多个实例。
//这时该类自身必须 1、定义一个静态私有实例,2、并向外提供一个静态的公有函数 用于创建或获取该静态私有实例。
/**
* 懒汉式单例 该模式的特点是类加载时没有生成单例,只有当第一次调用 getlnstance 方法时才去创建这个单例。
*/
class LazySingleton
{
private LazySingleton(){} //private 避免类在外部被实例化
private static volatile LazySingleton instance=null; //保证 instance 在所有线程中同步
public static synchronized LazySingleton getInstance() {
//getInstance 方法前加同步
if(instance==null)
{
instance=new LazySingleton();
}
return instance;
}
}
/**
* 饿汉式单例 该模式的特点是类一旦加载就创建一个单例,保证在调用 getInstance 方法之前单例已经存在了。
*/
class HungrySingleton
{
private static final HungrySingleton instance=new HungrySingleton();
private HungrySingleton(){}
public static HungrySingleton getInstance()
{
return instance;
}
}
* 火车票
class Th implements Runnable {
private static AtomicInteger count = new AtomicInteger(0);
@Override
public void run() {
while(count.intValue()<=50){
synchronized ("ok") {
if (count.incrementAndGet()<=50) {
System.out.println(Thread.currentThread().getName()+"出售第"+count.intValue()+"张车票");
} else {
System.err.println(Thread.currentThread().getName()+"火车票卖完了");
}
}
}
}
}
public class 火车票 {
public static void main(String[] args) {
new Thread(new Th(), "【窗口1】").start();
new Thread(new Th(), "【窗口2】").start();
new Thread(new Th(), "【窗口3】").start();
}
}
* 删除list中的一个元素
public static void main(String[] args) {
List<Integer> list = new ArrayList<Integer>();
list.add(14);
list.add(17);
list.add(15);
list.add(14);
list.add(14);
list.add(15);
list.add(18);
list.add(14);
list.add(15);
list.add(14);
System.out.println(removeFromList(list, 15));
System.out.println(removeFromList2(list, 14));
}
public static List<Integer> removeFromList(List<Integer> list, int str){
for (int i = 0; i < list.size(); i++) {
if (str == list.get(i)) {
list.remove(i);
i--;
}
}
return list;
}
public static List<Integer> removeFromList2(List<Integer> list, int str){
Iterator<Integer> it = list.iterator();
while(it.hasNext()){
if (it.next()==str) {
it.remove();
}
}
return list;
}
* 冒泡排序
参考:https://blog.csdn.net/ludan1991/article/details/102855731
* 创建多线程
class Thread2 implements Runnable {
@Override
public void run() {
for (int i = 0; i < 30; i++) {
System.out.println("子Thread1: +"+i);
}
}
}
public class Demo2 {
public static void main(String[] args) {
Thread2 t2 = new Thread2();
//==implements Runnable 与 extends Thread 调用方式不同
Thread t = new Thread(t2);
t.start();
for (int i = 0; i < 30; i++) {
System.err.println("主Demo1: +"+i);
}
}
}
* a、b、c三个线程,如何让a先执行完,再执行b 执行完,再执行c
public static void main(String[] args) {
Thread t1 = new Thread(new Runnable() {
public void run() {
for (int i = 0; i < 30; i++) {
System.out.println("【a子线程】: +"+i);
}
}
});
Thread t2 = new Thread(new Runnable() {
public void run() {
for (int i = 0; i < 30; i++) {
try {
t1.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("【bb子线程】: +"+i);
}
}
});
Thread t3 = new Thread(new Runnable() {
public void run() {
for (int i = 0; i < 30; i++) {
try {
t2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("【ccc子线程】: +"+i);
}
}
});
t1.start();
t2.start();
t3.start();
}
* 实现一个加减乘除的计算器
应用简单工厂实现,体现了单一职责,DispatchFactory 类违背开闭原则,这也是简单工厂的缺点之一:
public class Client {
public static void main(String[] args) {
System.out.println(new DispatchFactory().createCalculate("/").getResult(15.0, 4.0));
}
}
/**
* 调度器类
* @author lud
*/
public class DispatchFactory {
/**
* 实例化运算类
* @param symbol 运算符号
* @return
*/
public Calculate createCalculate(String symbol){
Calculate calculate = null;
switch (symbol) {
case "+":
calculate = new Addition();
break;
case "-":
calculate = new Subtraction();
break;
case "*":
calculate = new Multiplication();
break;
case "/":
calculate = new Division();
break;
case "%":
calculate = new Remainder();
break;
default:
System.out.print("不支持的此类运算: ");
return null;
}
return calculate;
}
}
/**
* 获取运算结果
* @author lud
*/
public interface Calculate {
Double getResult(Double first, Double last);
}
/**
* 加法
* @author lud
*/
class Addition implements Calculate {
@Override
public Double getResult(Double first, Double last) {
return first + last;
}
}
/**
* 减法
* @author lud
*/
class Subtraction implements Calculate {
@Override
public Double getResult(Double first, Double last) {
return first - last;
}
}
/**
* 乘法
* @author lud
*/
class Multiplication implements Calculate {
@Override
public Double getResult(Double first, Double last) {
return first * last;
}
}
/**
* 除法
* @author lud
*/
class Division implements Calculate {
@Override
public Double getResult(Double first, Double last) {
return first / last;
}
}
/**
* 取余数
* @author lud
*/
class Remainder implements Calculate {
@Override
public Double getResult(Double first, Double last) {
return first % last;
}
}
* 自定义链表
单向链表
class SingleLinked {
//查找元素
public Object find(Object obj) {
LinkedNode current = head;
int index = 0;
while(index < length){
if (current.data == obj) {
return current;
} else {
current = current.next;
}
index++;
}
return null;
}
//删除元素
public boolean delete(Object obj) {
if (length == 0) {
return false;
}
LinkedNode current = head;
LinkedNode currentPre = null;
while(current.data != obj) {
if (current.next == null) {
System.out.println("删除的目标节点不存在.");
return false;
} else {
currentPre = current;
current = current.next;
}
}
if (currentPre!=null) {
currentPre.next = current.next;
length --;
} else {
head = current.next;
length --;
}
return true;
}
//在链表的头部添加元素
public Object addHead(Object obj) {
LinkedNode node = new LinkedNode(obj);
if (length == 0) {
head = node;
} else {
node.next = head;
head = node;
}
length ++;
return obj;
}
private int length; //节点个数
private LinkedNode head; //头节点
public SingleLinked() {
length = 0;
head = null;
}
private class LinkedNode {
private Object data;
private LinkedNode next;
public LinkedNode(Object data) {
this.data = data;
}
}
}
* 自定义队列
class Queue<E> {
public E peek(){
if (rear == 0) {
throw new RuntimeException("队列为空...");
} else {
return (E) data[front];
}
}
public E poll(){
if (rear == 0) {
throw new RuntimeException("队列为空...");
} else {
E cache = (E) data[front];
data[front++] = null;
return cache;
}
}
public boolean add(E e){
if (rear == maxSize) {
throw new RuntimeException("队列已满...");
} else {
data[rear++] = e;
return true;
}
}
private Object[] data = null;
private int maxSize; //栈的容量
private int front; //队列头
private int rear; //队列尾
public Queue() {
this(10);
}
public Queue(int initSize) {
if (initSize>=0) {
this.maxSize = initSize;
this.data = new Object[initSize];
this.front = this.rear = 0;
} else {
throw new RuntimeException("初始化大小不能小于0");
}
}
}
* 自定义栈,实现添加(入栈push)、删除(出栈pop)、获取栈顶元素(top)三个方法
class Stack<E> {
public E peek(){
if (top == -1) {
throw new RuntimeException("栈为空...");
} else {
return (E) data[top];
}
}
public E pop(){
if (top == -1) {
throw new RuntimeException("栈为空...");
} else {
return (E) data[top--];
}
}
public boolean push(E e){
if (top == maxSize - 1) {
throw new RuntimeException("栈已满,无法将元素入栈...");
} else {
data[++top] = e;
return true;
}
}
private Object[] data = null;
private int maxSize = 0; //栈的容量
private int top = -1; //栈顶指针
public Stack() {
this(10);
}
public Stack(int initSize) {
if (initSize>=0) {
this.maxSize = initSize;
this.data = new Object[initSize];
this.top = -1;
} else {
throw new RuntimeException("初始化大小不能小于0");
}
}
}