feat(学习): 添加多线程、设计模式和LeetCode相关代码
- 新增 AbstractClass、Singleton 和 staticClass 类 - 添加 LeetCode3、LeetCode5、LeetCode20 等多个算法题解 - 新增 RedisDataTest 类,增加 Redis 地理位置相关测试 - 添加 SQL相关文件,包括学生表创建和查询、临时表使用等- 修改 ChainPattern、FunctionInterfaceDemo 和 FutureRelative 类
This commit is contained in:
parent
1f2ef13c86
commit
d9e64a81a6
@ -41,6 +41,14 @@ class Main{
|
||||
}
|
||||
|
||||
interface Audit{
|
||||
default void def() {
|
||||
|
||||
}
|
||||
|
||||
static void main() {
|
||||
|
||||
}
|
||||
|
||||
void process(String name,int day);
|
||||
|
||||
public static class Builder{
|
||||
@ -68,6 +76,10 @@ interface Audit{
|
||||
abstract class AbstractAudit implements Audit{
|
||||
Audit next;
|
||||
|
||||
@Override
|
||||
public void def() {
|
||||
Audit.super.def();
|
||||
}
|
||||
}
|
||||
|
||||
class Supervisor extends AbstractAudit {
|
||||
|
@ -0,0 +1,129 @@
|
||||
package cn.whaifree.leetCode.BackTracking;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* @version 1.0
|
||||
* @Author whai文海
|
||||
* @Date 2024/10/21 14:13
|
||||
* @注释
|
||||
*/
|
||||
public class LeetCode567 {
|
||||
|
||||
/**
|
||||
* 超时
|
||||
*/
|
||||
@Test
|
||||
public void test() {
|
||||
String s1 = "adc";
|
||||
String s2 = "dcda";
|
||||
boolean result = new Solution().checkInclusion(s1, s2);
|
||||
System.out.println(result);
|
||||
}
|
||||
|
||||
class Solution {
|
||||
|
||||
|
||||
/**
|
||||
* @param s1
|
||||
* @param s2
|
||||
* @return
|
||||
*/
|
||||
public boolean checkInclusion(String s1, String s2) {
|
||||
// 获取s1的全部排列,再到s2中找有没有对于的子串
|
||||
List<String> stringSub = getStringSub(s1);
|
||||
for (String s : stringSub) {
|
||||
int len = s1.length();
|
||||
for (int i = 0; i <= s2.length() - len; i++) {
|
||||
if (s2.substring(i, i + len).equals(s)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public List<String> getStringSub(String s1) {
|
||||
ArrayList<String> res = new ArrayList<>();
|
||||
backTracking(res, s1);
|
||||
return res;
|
||||
}
|
||||
|
||||
StringBuilder path = new StringBuilder();
|
||||
Set<Integer> set = new HashSet<>();
|
||||
public void backTracking(List<String> res, String s) {
|
||||
if (path.length() >= s.length()) {
|
||||
res.add(path.toString());
|
||||
return;
|
||||
}
|
||||
for (int i = 0; i < s.length(); i++) {
|
||||
if (set.contains(i)) {
|
||||
continue;
|
||||
}
|
||||
set.add(i);
|
||||
path.append(s.charAt(i));
|
||||
backTracking(res, s);
|
||||
path.deleteCharAt(path.length() - 1);
|
||||
set.remove(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test1() {
|
||||
String s1 = "ab";
|
||||
String s2 = "eidboaooo";
|
||||
boolean result = new Solution1().checkInclusion(s1, s2);
|
||||
System.out.println(result);
|
||||
}
|
||||
|
||||
class Solution1 {
|
||||
/**
|
||||
* 需要同时考虑 s1的map和s2的map匹配,同时还有已经完全匹配的个数;
|
||||
* @param s1
|
||||
* @param s2
|
||||
* @return
|
||||
*/
|
||||
public boolean checkInclusion(String s1, String s2) {
|
||||
Map<Character, Integer> need = new HashMap<>();
|
||||
for (int i = 0; i < s1.length(); i++) {
|
||||
need.put(s1.charAt(i), need.getOrDefault(s1.charAt(i), 0) + 1);
|
||||
}
|
||||
int left = 0;
|
||||
int right = 0;
|
||||
Map<Character, Integer> window = new HashMap<>();
|
||||
|
||||
int validCount = 0; // 记录有效个数(某个字符对应的数量和need一致),如果==need.size直接返回
|
||||
char[] s2CharArray = s2.toCharArray();
|
||||
while (right < s2.length()) {
|
||||
// 右边指针不断探,加入window统计出现个数
|
||||
char c = s2CharArray[right];
|
||||
right++;
|
||||
if (need.containsKey(c)) {
|
||||
window.put(c, window.getOrDefault(c, 0) + 1);
|
||||
if (window.get(c).equals(need.get(c))) {
|
||||
validCount++;
|
||||
if (validCount == need.size()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
// 左边指针收缩 关键在于找到进入while left 的循环条件
|
||||
while (right - left >= s1.length()) {
|
||||
char cha = s2CharArray[left];
|
||||
left++;
|
||||
if (need.containsKey(cha)) {
|
||||
if (window.get(cha).equals(need.get(cha))) {
|
||||
validCount--;
|
||||
}
|
||||
window.put(cha, window.get(cha) - 1);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,66 @@
|
||||
package cn.whaifree.leetCode.Hash;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @version 1.0
|
||||
* @Author whai文海
|
||||
* @Date 2024/10/20 22:35
|
||||
* @注释
|
||||
*/
|
||||
public class LeetCode136 {
|
||||
|
||||
class Solution {
|
||||
public int singleNumber(int[] nums) {
|
||||
Map<Integer, Integer> map = new HashMap<>();
|
||||
for (int num : nums) {
|
||||
map.put(num, map.getOrDefault(num, 0) + 1);
|
||||
}
|
||||
for (Map.Entry<Integer, Integer> integerIntegerEntry : map.entrySet()) {
|
||||
if (integerIntegerEntry.getValue() == 1) {
|
||||
return integerIntegerEntry.getKey();
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
@Test
|
||||
public void test() {
|
||||
Solution1 solution = new Solution1();
|
||||
int[] nums = {7, 10, 7};
|
||||
int res = solution.singleNumber(nums);
|
||||
System.out.println(res);
|
||||
}
|
||||
|
||||
class Solution1 {
|
||||
/**
|
||||
* 任何数自身作^==0
|
||||
*
|
||||
* 某个元素只出现一次以外,**其余每个元素均出现两次** ^后就为0
|
||||
*
|
||||
* @param nums
|
||||
* @return
|
||||
*/
|
||||
public int singleNumber(int[] nums) {
|
||||
int res = 0;
|
||||
for (int num : nums) {
|
||||
// 7^7=0
|
||||
// 7^10=1101
|
||||
// 7^10^7=1010(10)
|
||||
System.out.println(Integer.toBinaryString(num));
|
||||
res ^= num;
|
||||
System.out.println(Integer.toBinaryString(res));
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(Integer.toBinaryString(7));
|
||||
System.out.println(Integer.toBinaryString(10));
|
||||
System.out.println(Integer.toBinaryString(7 ^ 10));
|
||||
}
|
||||
}
|
@ -0,0 +1,64 @@
|
||||
package cn.whaifree.redo.redo_all_241016;
|
||||
|
||||
import cn.whaifree.leetCode.model.ListNode;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* @version 1.0
|
||||
* @Author whai文海
|
||||
* @Date 2024/10/20 22:53
|
||||
* @注释
|
||||
*/
|
||||
public class LeetCode142
|
||||
{
|
||||
@Test
|
||||
public void test() {
|
||||
ListNode head = new ListNode(3);
|
||||
ListNode next1 = new ListNode(2);
|
||||
head.next = next1;
|
||||
head.next.next = new ListNode(0);
|
||||
ListNode next = new ListNode(-4);
|
||||
head.next.next.next = next;
|
||||
|
||||
next.next = next1;
|
||||
ListNode node = new Solution().detectCycle(head);
|
||||
System.out.println(node.val);
|
||||
}
|
||||
public class Solution {
|
||||
/**
|
||||
* slow 走了N
|
||||
* fast 走了2N 相交
|
||||
* 那么一个环=x+N,
|
||||
* 此时slow就在x,而
|
||||
*
|
||||
* | x|
|
||||
* 0------N.--- N=入口+x 环-x=N
|
||||
* | | 2N-入口 = 环
|
||||
* --------
|
||||
*
|
||||
* @param head
|
||||
* @return
|
||||
*/
|
||||
public ListNode detectCycle(ListNode head) {
|
||||
ListNode fast = head;
|
||||
ListNode slow = head;
|
||||
while (fast != null && fast.next != null) {
|
||||
fast = fast.next.next;
|
||||
slow = slow.next;
|
||||
if (fast == slow) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (fast == null || fast.next == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
fast = head;
|
||||
while (fast != slow) {
|
||||
fast = fast.next;
|
||||
slow = slow.next;
|
||||
}
|
||||
return fast;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,52 @@
|
||||
package cn.whaifree.redo.redo_all_241016;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Deque;
|
||||
import java.util.LinkedList;
|
||||
|
||||
/**
|
||||
* @version 1.0
|
||||
* @Author whai文海
|
||||
* @Date 2024/10/20 22:46
|
||||
* @注释
|
||||
*/
|
||||
public class LeetCode20 {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
String s = ")";
|
||||
Solution solution = new Solution();
|
||||
boolean result = solution.isValid(s);
|
||||
System.out.println(result);
|
||||
}
|
||||
|
||||
class Solution {
|
||||
public boolean isValid(String s) {
|
||||
|
||||
Deque<Character> stack = new LinkedList<>();
|
||||
char[] charArray = s.toCharArray();
|
||||
for (char c : charArray) {
|
||||
if (c == '(' || c == '{' || c == '[') {
|
||||
stack.push(c);
|
||||
} else {
|
||||
if (stack.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
Character pop = stack.pop();
|
||||
if (c == ')' && pop != '(') {
|
||||
return false;
|
||||
}
|
||||
if (c == '}' && pop != '{') {
|
||||
return false;
|
||||
}
|
||||
if (c == ']' && pop != '[') {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return stack.isEmpty();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,43 @@
|
||||
package cn.whaifree.redo.redo_all_241016;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* @version 1.0
|
||||
* @Author whai文海
|
||||
* @Date 2024/10/21 12:46
|
||||
* @注释
|
||||
*/
|
||||
public class LeetCode3 {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
String s = "pwwkew";
|
||||
int result = new Solution().lengthOfLongestSubstring(s);
|
||||
System.out.println(result);
|
||||
}
|
||||
|
||||
class Solution {
|
||||
public int lengthOfLongestSubstring(String s) {
|
||||
char[] charArray = s.toCharArray();
|
||||
int left = 0;
|
||||
int right = 0;
|
||||
Set<Character> set = new HashSet<>();
|
||||
int max = 0;
|
||||
while (right < s.length()) {
|
||||
char c = charArray[right];
|
||||
while (left < right && set.contains(c)) {
|
||||
set.remove(charArray[left]);
|
||||
left++;
|
||||
}
|
||||
max = Math.max(max, right - left + 1);
|
||||
set.add(c);
|
||||
right++;
|
||||
}
|
||||
return max;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,89 @@
|
||||
package cn.whaifree.redo.redo_all_241016;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* @version 1.0
|
||||
* @Author whai文海
|
||||
* @Date 2024/10/20 20:38
|
||||
* @注释
|
||||
*/
|
||||
public class LeetCode5 {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
String s = "baabd";
|
||||
Solution solution = new Solution();
|
||||
String result = solution.longestPalindrome(s);
|
||||
System.out.println(result);
|
||||
}
|
||||
|
||||
class Solution {
|
||||
public String longestPalindrome(String s) {
|
||||
String max = "";
|
||||
for (int i = 0; i < s.length(); i++) {
|
||||
String A = isHuiWen(s, i, i);
|
||||
if (A.length() > max.length()) {
|
||||
max = A;
|
||||
}
|
||||
String B = isHuiWen(s, i, i + 1);
|
||||
if (B.length() > max.length()) {
|
||||
max = B;
|
||||
}
|
||||
}
|
||||
return max;
|
||||
}
|
||||
|
||||
public String isHuiWen(String s, int start, int end) {
|
||||
while (start >= 0 && end < s.length()) {
|
||||
if (s.charAt(start) == s.charAt(end)) {
|
||||
start--;
|
||||
end++;
|
||||
}else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return s.substring(start + 1, end);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test1() {
|
||||
String s = "aacab";
|
||||
Solution1 solution = new Solution1();
|
||||
String result = solution.longestPalindrome(s);
|
||||
System.out.println(result);
|
||||
}
|
||||
|
||||
class Solution1 {
|
||||
public String longestPalindrome(String s) {
|
||||
int len = s.length();
|
||||
boolean[][] dp = new boolean[len+1][len+1];
|
||||
for (int i = 1; i <= len; i++) {
|
||||
dp[i][i] = true;
|
||||
}
|
||||
|
||||
// dp[i][j] dp[i+1][j-1]
|
||||
int left = 0;
|
||||
int right = 0;
|
||||
for (int i = len; i > 0; i--) {
|
||||
for (int j = i; j <= len; j++) {
|
||||
if (i == j) {
|
||||
continue;
|
||||
}
|
||||
boolean b = s.charAt(i - 1) == s.charAt(j - 1);
|
||||
if ((dp[i + 1][j - 1] || (i == j - 1)) && b) {
|
||||
if (j - i > right - left) {
|
||||
left = i - 1;
|
||||
right = j - 1;
|
||||
}
|
||||
dp[i][j] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return s.substring(left, right + 1);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
package cn.whaifree.redo.redo_all_241016;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* @version 1.0
|
||||
* @Author whai文海
|
||||
* @Date 2024/10/20 18:51
|
||||
* @注释
|
||||
*/
|
||||
public class LeetCode62 {
|
||||
@Test
|
||||
public void test() {
|
||||
System.out.println(new Solution().uniquePaths(3, 7));
|
||||
}
|
||||
|
||||
class Solution {
|
||||
public int uniquePaths(int m, int n) {
|
||||
int[][] dp = new int[m][n];
|
||||
dp[0][0] = 1;
|
||||
for (int i = 0; i < m; i++) {
|
||||
for (int j = 0; j < n; j++) {
|
||||
if (i > 0) {
|
||||
dp[i][j] += dp[i - 1][j];
|
||||
}
|
||||
if (j > 0) {
|
||||
dp[i][j] += dp[i][j - 1];
|
||||
}
|
||||
}
|
||||
}
|
||||
return dp[m - 1][n - 1];
|
||||
}
|
||||
}
|
||||
}
|
@ -1,5 +1,7 @@
|
||||
package cn.whaifree.tech.demo.thread;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.*;
|
||||
@ -16,8 +18,6 @@ public class FutureRelative {
|
||||
Executors.defaultThreadFactory());
|
||||
|
||||
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
futureTaskDemo();
|
||||
|
||||
@ -47,14 +47,23 @@ public class FutureRelative {
|
||||
|
||||
}
|
||||
|
||||
static volatile int num = 0;
|
||||
@Test
|
||||
public void completeFutureDemoTest() {
|
||||
completeFutureDemo();
|
||||
}
|
||||
|
||||
static volatile int num = 0;
|
||||
public static void completeFutureDemo() {
|
||||
List<CompletableFuture<Integer>> completableFutures = new ArrayList<>();
|
||||
for (int i = 0; i < 100; i++) {
|
||||
CompletableFuture<Integer> cf = CompletableFuture.supplyAsync(new Supplier<Integer>() {
|
||||
@Override
|
||||
public Integer get() {
|
||||
try {
|
||||
Thread.sleep(1000000);
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return num++;
|
||||
}
|
||||
}, executorService).exceptionally(
|
||||
@ -78,4 +87,124 @@ public class FutureRelative {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void howFutureGet() throws ExecutionException, InterruptedException {
|
||||
CompletableFuture<Integer> cf = CompletableFuture.supplyAsync(new Supplier<Integer>() {
|
||||
@Override
|
||||
public Integer get() {
|
||||
try {
|
||||
Thread.sleep(Integer.MAX_VALUE);
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
});
|
||||
cf.get();
|
||||
}
|
||||
|
||||
class MyFutureTask extends FutureTask<Integer> {
|
||||
|
||||
public MyFutureTask(Callable<Integer> callable) {
|
||||
super(callable);
|
||||
}
|
||||
|
||||
public MyFutureTask(Runnable runnable, Integer result) {
|
||||
super(runnable, result);
|
||||
}
|
||||
|
||||
private volatile int state; // 任务状态
|
||||
private static final int NEW = 0;
|
||||
private static final int COMPLETING = 1;
|
||||
private static final int NORMAL = 2;
|
||||
private static final int EXCEPTIONAL = 3;
|
||||
private static final int CANCELLED = 4;
|
||||
private static final int INTERRUPTING = 5;
|
||||
private static final int INTERRUPTED = 6;
|
||||
// private Callable<V> callable; // 用户提交的任务
|
||||
// private Object outcome; // 任务的结果或异常
|
||||
// private volatile Thread runner; // 当前执行任务的线程
|
||||
// private volatile WaitNode waiters; // 阻塞线程链表
|
||||
|
||||
@Override
|
||||
public Integer get() throws InterruptedException, ExecutionException {
|
||||
return super.get();
|
||||
/*
|
||||
int s = state;
|
||||
if (s <= COMPLETING)
|
||||
s = awaitDone(false, 0L); // 未完成的任务调用awaitDone阻塞
|
||||
return report(s); // 根据任务状态返回结果或抛出异常
|
||||
*/
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Awaits completion or aborts on interrupt or timeout.
|
||||
*
|
||||
* @param timed true if use timed waits
|
||||
* @param nanos time to wait, if timed
|
||||
* @return state upon completion or at timeout
|
||||
*/
|
||||
// private int awaitDone(boolean timed, long nanos)
|
||||
// throws InterruptedException {
|
||||
// long startTime = 0L; // Special value 0L means not yet parked
|
||||
// WaitNode q = null;
|
||||
// boolean queued = false;
|
||||
// for (;;) {
|
||||
// int s = state;
|
||||
// if (s > COMPLETING) {
|
||||
// if (q != null)
|
||||
// q.thread = null;
|
||||
// return s;
|
||||
// }
|
||||
// else if (s == COMPLETING)
|
||||
// // We may have already promised (via isDone) that we are done
|
||||
// // so never return empty-handed or throw InterruptedException
|
||||
// Thread.yield();
|
||||
// else if (Thread.interrupted()) {
|
||||
// removeWaiter(q);
|
||||
// throw new InterruptedException();
|
||||
// }
|
||||
// else if (q == null) {
|
||||
// if (timed && nanos <= 0L)
|
||||
// return s;
|
||||
// q = new WaitNode();
|
||||
// }
|
||||
// else if (!queued)
|
||||
// queued = WAITERS.weakCompareAndSet(this, q.next = waiters, q);
|
||||
// else if (timed) {
|
||||
// final long parkNanos;
|
||||
// if (startTime == 0L) { // first time
|
||||
// startTime = System.nanoTime();
|
||||
// if (startTime == 0L)
|
||||
// startTime = 1L;
|
||||
// parkNanos = nanos;
|
||||
// } else {
|
||||
// long elapsed = System.nanoTime() - startTime;
|
||||
// if (elapsed >= nanos) {
|
||||
// removeWaiter(q);
|
||||
// return state;
|
||||
// }
|
||||
// parkNanos = nanos - elapsed;
|
||||
// }
|
||||
// // nanoTime may be slow; recheck before parking
|
||||
// if (state < COMPLETING)
|
||||
// LockSupport.parkNanos(this, parkNanos);
|
||||
// }
|
||||
// else
|
||||
// LockSupport.park(this);
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
class MyCompletableFuture extends CompletableFuture<Integer> {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,21 @@
|
||||
package cn.whaifree.tech.java;
|
||||
|
||||
/**
|
||||
* @version 1.0
|
||||
* @Author whai文海
|
||||
* @Date 2024/10/20 23:24
|
||||
* @注释
|
||||
*/
|
||||
public class staticClass {
|
||||
|
||||
static int i = 1;
|
||||
static {
|
||||
System.out.println("A " + i++);
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
System.out.println("N");
|
||||
}
|
||||
static {
|
||||
System.out.println("B " + i++);
|
||||
}
|
||||
}
|
@ -26,6 +26,14 @@ public class FanxinTest {
|
||||
}
|
||||
interface B{
|
||||
|
||||
static void method() {
|
||||
|
||||
}
|
||||
|
||||
default void method2() {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
interface C{
|
||||
|
||||
|
@ -10,7 +10,10 @@ import java.util.concurrent.locks.ReentrantLock;
|
||||
* @注释
|
||||
*/
|
||||
public class FunctionInterfaceDemo {
|
||||
|
||||
static ReentrantLock lock = new ReentrantLock();
|
||||
|
||||
|
||||
public static void main(String[] args) throws InterruptedException {
|
||||
|
||||
HashMap<Object, Object> map = new HashMap<>();
|
||||
|
@ -0,0 +1,60 @@
|
||||
package cn.whaifree.tech.designPattern;
|
||||
|
||||
/**
|
||||
* @version 1.0
|
||||
* @Author whai文海
|
||||
* @Date 2024/10/21 12:33
|
||||
* @注释
|
||||
*/
|
||||
public class Singleton {
|
||||
|
||||
|
||||
// 饿
|
||||
static class HungrySingleton {
|
||||
private static final Singleton INSTANCE = new Singleton();
|
||||
|
||||
public static Singleton getInstance() {
|
||||
return INSTANCE;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class StaticSingleton{
|
||||
volatile StaticSingleton staticSingleton = null;
|
||||
public StaticSingleton getInstance() {
|
||||
if (staticSingleton == null) {
|
||||
// 两个线程有一个卡在这里、有一个进去获得StaticSingleton.class
|
||||
synchronized (StaticSingleton.class) {
|
||||
if (staticSingleton == null) { // 如果没有这个,那么另一个线程进来后也会new,覆盖了
|
||||
staticSingleton = new StaticSingleton();
|
||||
}
|
||||
}
|
||||
}
|
||||
return staticSingleton;
|
||||
}
|
||||
}
|
||||
|
||||
// 懒汉式(线程安全,同步方法)
|
||||
static class SynchronizedSingleton{
|
||||
private static SynchronizedSingleton synchronizedSingleton = null;
|
||||
public static synchronized SynchronizedSingleton getInstance() {
|
||||
if (synchronizedSingleton == null) {
|
||||
synchronizedSingleton = new SynchronizedSingleton();
|
||||
}
|
||||
return synchronizedSingleton;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 静态内部累,静态类第一次调用才会初始化
|
||||
static class staticInnerClass{
|
||||
private static class SingletonHolder{
|
||||
private static final Singleton INSTANCE = new Singleton();
|
||||
}
|
||||
public static Singleton getInstance(){
|
||||
return SingletonHolder.INSTANCE; // 第一次调用SingletonHolder才会生成 INSTANCE
|
||||
}
|
||||
}
|
||||
|
||||
// 枚举
|
||||
}
|
@ -13,7 +13,6 @@ import org.springframework.context.ApplicationContextAware;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* @version 1.0
|
||||
@ -26,7 +25,7 @@ import org.springframework.stereotype.Component;
|
||||
class Config{
|
||||
|
||||
}
|
||||
@Component("userService")
|
||||
//@Component("userService")
|
||||
public class UserService implements InitializingBean, DisposableBean, BeanFactoryAware, ApplicationContextAware, BeanPostProcessor,AutoCloseable {
|
||||
|
||||
/**
|
||||
@ -46,7 +45,6 @@ public class UserService implements InitializingBean, DisposableBean, BeanFactor
|
||||
UserService userService = context.getBean("userService", UserService.class);
|
||||
// 执行 DisposableBean
|
||||
|
||||
|
||||
context.close();
|
||||
}
|
||||
private String beanName;
|
||||
|
@ -4,9 +4,13 @@ import jakarta.annotation.Resource;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.data.geo.*;
|
||||
import org.springframework.data.redis.connection.BitFieldSubCommands;
|
||||
import org.springframework.data.redis.connection.RedisGeoCommands;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @version 1.0
|
||||
* @Author whai文海
|
||||
@ -79,4 +83,35 @@ public class RedisDataTest {
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGeo() {
|
||||
String key = "geo:user:location";
|
||||
|
||||
|
||||
redisTemplate.opsForGeo().add(key, new Point(116.407396, 39.904200), "beijin");
|
||||
redisTemplate.opsForGeo().add(key, new Point(121.473701, 31.230416), "shanghai");
|
||||
redisTemplate.opsForGeo().add(key, new Point(113.264385, 23.129112), "guangzhou");
|
||||
Distance distance = redisTemplate.opsForGeo().distance(key, "beijin", "shanghai", RedisGeoCommands.DistanceUnit.KILOMETERS); // 计算两个地点之间的距离
|
||||
System.out.println("beijin to shanghai distance: " + distance.getValue() + " " + distance.getUnit());
|
||||
|
||||
|
||||
Point point = new Point(116.404, 39.915);
|
||||
String meMember = "me";
|
||||
redisTemplate.opsForGeo().add(key, point, meMember); // 增加一个点到地理位置信息中
|
||||
|
||||
GeoResults<RedisGeoCommands.GeoLocation<Object>> radius =
|
||||
redisTemplate.opsForGeo().radius(key, new Circle(point, new Distance(2000, RedisGeoCommands.DistanceUnit.KILOMETERS)), // 圆形区域, 半径为1000米
|
||||
RedisGeoCommands.GeoRadiusCommandArgs.newGeoRadiusArgs().includeCoordinates().sortAscending());
|
||||
|
||||
|
||||
List<GeoResult<RedisGeoCommands.GeoLocation<Object>>> content = radius.getContent();
|
||||
for (GeoResult<RedisGeoCommands.GeoLocation<Object>> geoResult : content) {
|
||||
System.out.println(geoResult.getContent().getName() + " " + geoResult.getContent().getPoint());
|
||||
|
||||
// 计算point 到这些位置的距离
|
||||
Distance dis = redisTemplate.opsForGeo().distance(key, meMember, geoResult.getContent().getName(), RedisGeoCommands.DistanceUnit.KILOMETERS);
|
||||
System.out.println(dis.getValue() + " " + dis.getUnit());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,44 @@
|
||||
package cn.whaifree.springdemo.tech;
|
||||
|
||||
/**
|
||||
* @version 1.0
|
||||
* @Author whai文海
|
||||
* @Date 2024/10/20 20:19
|
||||
* @注释
|
||||
*/
|
||||
public class AbstractClass {
|
||||
|
||||
void method() {
|
||||
|
||||
}
|
||||
|
||||
abstract class parents{
|
||||
|
||||
public parents(int n) {
|
||||
System.out.println(inter.name);
|
||||
System.out.println(1);
|
||||
}
|
||||
|
||||
public void method2() {
|
||||
|
||||
}
|
||||
|
||||
abstract void method();
|
||||
|
||||
}
|
||||
|
||||
class SubClass extends parents {
|
||||
public SubClass() {
|
||||
// System.out.println(1); 不能写在前面
|
||||
super(1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void method() {
|
||||
method2();
|
||||
}
|
||||
}
|
||||
}
|
||||
interface inter{
|
||||
public static final int name = 0; // public static final 是多余的
|
||||
}
|
93
sql/looking/241020_1.sql
Normal file
93
sql/looking/241020_1.sql
Normal file
@ -0,0 +1,93 @@
|
||||
CREATE TABLE students_241020 (
|
||||
id INT PRIMARY KEY AUTO_INCREMENT,
|
||||
name VARCHAR(100) NOT NULL,
|
||||
age INT NOT NULL,
|
||||
class VARCHAR(50) NOT NULL,
|
||||
score1 DECIMAL(5, 2) NOT NULL, -- 数学成绩
|
||||
score2 DECIMAL(5, 2) NOT NULL, -- 英语成绩
|
||||
score3 DECIMAL(5, 2) NOT NULL -- 语文成绩
|
||||
);
|
||||
|
||||
INSERT INTO students_241020 (name, age, class, score1, score2, score3) VALUES
|
||||
('张三', 18, '高三1班', 90.50, 85.00, 92.00),
|
||||
('李四', 17, '高三2班', 88.00, 90.50, 87.00),
|
||||
('王五', 18, '高三1班', 92.00, 88.00, 89.00),
|
||||
('赵六', 17, '高三2班', 85.00, 92.00, 90.50),
|
||||
('孙七', 18, '高三1班', 87.00, 89.00, 91.00),
|
||||
('周八', 17, '高三2班', 90.00, 87.00, 88.00),
|
||||
('吴九', 18, '高三1班', 89.00, 91.00, 86.00),
|
||||
('郑十', 17, '高三2班', 91.00, 86.00, 87.00);
|
||||
|
||||
|
||||
# 获取每个班级的三科总分高于平均分(所有同学)的同学数量,并按照数量大小获取排名前三的班级,写出SQL语句
|
||||
|
||||
SELECT
|
||||
tmp1.class, -- 选择班级字段
|
||||
count( tmp1.id ) AS `count` -- 统计每个班级的学生数
|
||||
FROM
|
||||
(
|
||||
-- 子查询,计算每个学生的总分
|
||||
SELECT s1.id, s1.class, s1.score1 + s1.score2 + s1.score3 AS `sum`
|
||||
FROM students_241020 s1
|
||||
) tmp1
|
||||
WHERE
|
||||
-- 条件判断,筛选出总分高于平均分的记录
|
||||
sum > (
|
||||
SELECT AVG( s.score1 + s.score2 + s.score3 )
|
||||
FROM students_241020 s
|
||||
)
|
||||
GROUP BY
|
||||
tmp1.class -- 按班级分组
|
||||
ORDER BY
|
||||
count DESC -- 按学生数降序排列
|
||||
LIMIT 3; -- 限制结果返回前三条记录
|
||||
|
||||
|
||||
|
||||
|
||||
SELECT
|
||||
tmp1.class, -- 选择班级字段
|
||||
count( tmp1.id ) AS `count` -- 统计每个班级的学生数
|
||||
FROM
|
||||
students_241020 tmp1
|
||||
WHERE
|
||||
-- 条件判断,筛选出总分高于平均分的记录
|
||||
tmp1.score1 + tmp1.score2 + tmp1.score3 > (
|
||||
SELECT AVG( s.score1 + s.score2 + s.score3 )
|
||||
FROM students_241020 s
|
||||
)
|
||||
GROUP BY
|
||||
tmp1.class -- 按班级分组
|
||||
ORDER BY
|
||||
count DESC -- 按学生数降序排列
|
||||
LIMIT 3; -- 限制结果返回前三条记录
|
||||
|
||||
|
||||
|
||||
-- 使用临时表
|
||||
|
||||
-- 计算平均分
|
||||
WITH avg_score AS (
|
||||
SELECT AVG(score1 + score2 + score3) AS avg_sum
|
||||
FROM students_241020
|
||||
),
|
||||
-- 子查询,计算每个学生的总分
|
||||
student_scores AS (
|
||||
SELECT id, class, score1 + score2 + score3 AS `sum`
|
||||
FROM students_241020
|
||||
)
|
||||
|
||||
|
||||
-- 查询总分高于平均分的班级及其学生数
|
||||
SELECT
|
||||
class, -- 选择班级字段
|
||||
COUNT(id) AS `count` -- 统计每个班级的学生数
|
||||
FROM
|
||||
student_scores
|
||||
WHERE
|
||||
`sum` > (SELECT avg_sum FROM avg_score)
|
||||
GROUP BY
|
||||
class -- 按班级分组
|
||||
ORDER BY
|
||||
`count` DESC -- 按学生数降序排列
|
||||
LIMIT 3; -- 限制结果返回前三条记录
|
33
sql/tech/tmpTable.sql
Normal file
33
sql/tech/tmpTable.sql
Normal file
@ -0,0 +1,33 @@
|
||||
-- 使用临时表
|
||||
|
||||
-- 计算平均分
|
||||
WITH
|
||||
avg_score AS (
|
||||
SELECT AVG(score1 + score2 + score3) AS avg_sum
|
||||
FROM students_241020
|
||||
),
|
||||
-- 子查询,计算每个学生的总分
|
||||
student_scores AS (
|
||||
SELECT id, class, score1 + score2 + score3 AS `sum`
|
||||
FROM students_241020
|
||||
)
|
||||
|
||||
|
||||
-- 查询总分高于平均分的班级及其学生数
|
||||
SELECT
|
||||
class, -- 选择班级字段
|
||||
COUNT(id) AS `count` -- 统计每个班级的学生数
|
||||
FROM
|
||||
student_scores
|
||||
WHERE
|
||||
`sum` > (SELECT avg_sum FROM avg_score)
|
||||
GROUP BY
|
||||
class -- 按班级分组
|
||||
ORDER BY
|
||||
`count` DESC -- 按学生数降序排列
|
||||
LIMIT 3; -- 限制结果返回前三条记录
|
||||
|
||||
|
||||
WITH avg_score as
|
||||
(SELECT * FROM test_table)
|
||||
SELECT * FROM avg_score;
|
9
sql/tech/useValiable.sql
Normal file
9
sql/tech/useValiable.sql
Normal file
@ -0,0 +1,9 @@
|
||||
SET @row_number = 0;
|
||||
|
||||
SELECT
|
||||
(@row_number := @row_number + 1) AS row_num, -- :=是MYSQL的赋值运算
|
||||
t.*
|
||||
FROM
|
||||
test_table t
|
||||
ORDER BY
|
||||
t.id;
|
Loading…
Reference in New Issue
Block a user