import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.*;
public class Baidu {
public static List<String> findTopNWords(File inputFile, int n) {
List<String> topNWords = new ArrayList<>();
Map<String, Integer> wordCountMap = new HashMap<>();
try (BufferedReader reader = new BufferedReader(new FileReader(inputFile))) {
String line;
while ((line = reader.readLine()) != null) {
String[] words = line.split("\\s+");
for (String word : words) {
wordCountMap.put(word, wordCountMap.getOrDefault(word, 0) + 1);
}
}
} catch (Exception e) {
e.printStackTrace();
}
PriorityQueue<Map.Entry<String, Integer>> maxHeap =
new PriorityQueue<>((e1, e2) -> e2.getValue().compareTo(e1.getValue()));
for (Map.Entry<String, Integer> entry : wordCountMap.entrySet()) {
maxHeap.offer(entry);
}
if (!maxHeap.isEmpty()) {
for (int i = 0; i < n; i++) {
topNWords.add(maxHeap.poll().getKey());
}
}
return topNWords;
}
public static void main(String[] args) {
File inputFile = new File("D:/topn.txt");
int topN = 10;
List<String> topNWords = findTopNWords(inputFile, topN);
System.out.println("TopN搜索词列表:");
for (String word : topNWords) {
System.out.println(word);
}
}
}
import java.util.Queue;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
public class ProducerConsumerExample {
public static void main(String[] args) {
Queue<Integer> queue = new ConcurrentLinkedQueue<>();
ThreadPoolExecutor executor = new ThreadPoolExecutor(10, 100, 1, TimeUnit.MINUTES, new LinkedBlockingQueue<>(1000));
AtomicInteger ai = new AtomicInteger(0);
for (int i = 0; i < 2; i++) {
executor.submit(new Producer(queue, ai));
}
for (int i = 0; i < 2; i++) {
executor.submit(new Consumer(queue));
}
executor.shutdown();
}
static class Producer implements Runnable {
private final Queue<Integer> queue;
private final AtomicInteger value;
public Producer(Queue<Integer> queue, AtomicInteger value) {
this.value = value;
this.queue = queue;
}
@Override
public void run() {
while (true) {
int val = value.incrementAndGet();
System.out.println("Produced: " + val);
queue.offer(val);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
static class Consumer implements Runnable {
private final Queue<Integer> queue;
public Consumer(Queue<Integer> queue) {
this.queue = queue;
}
@Override
public void run() {
while (true) {
Integer value = queue.poll();
if (value != null) {
System.out.println("Consumed: " + value);
}
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
System.out.println(Arrays.toString(bubbleSort(new int[]{64, 34, 25, 12, 22, 11, 90})));
private static int[] bubbleSort(int[] array) {
int n = array.length;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (array[j] > array[j + 1]) {
int temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
return array;
}
public static void main(String[] args) {
AtomicInteger ai = new AtomicInteger(1);
Semaphore a = new Semaphore(1);
Semaphore b = new Semaphore(0);
Semaphore c = new Semaphore(0);
try (ThreadPoolExecutor executor = new ThreadPoolExecutor(10, 100,
1, TimeUnit.MINUTES, new LinkedBlockingQueue<>(100));) {
executor.submit(new Thread(() -> {
printNum(ai, a, b);
}));
executor.submit(new Thread(() -> {
printNum(ai, b, c);
}));
executor.submit(new Thread(() -> {
printNum(ai, c, a);
}));
}
}
private static void printNum(AtomicInteger ai, Semaphore cur, Semaphore next) {
while (ai.get() <= 100) {
try {
cur.acquire();
if (ai.get() <= 100) {
System.out.println(ai.getAndIncrement());
}
next.release();
} catch (InterruptedException e) {
throw new RuntimeException();
}
}
}