读取一个大的CSV文件,并将数据存入list中
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.List;
public class ReadLargeFileToList {
public static void main(String[] args) {
String filename = "your_large_file.txt";
String separator = "&*#\\n";
int bufferSize = 8192;
List<String> lines = new ArrayList<>();
try (FileChannel channel = FileChannel.open(Paths.get(filename), StandardOpenOption.READ)) {
ByteBuffer buffer = ByteBuffer.allocate(bufferSize);
StringBuilder lineBuilder = new StringBuilder();
while (channel.read(buffer) > 0) {
buffer.flip();
while (buffer.hasRemaining()) {
char c = (char) buffer.get();
if (separator.indexOf(c) != -1) {
lines.add(lineBuilder.toString());
lineBuilder.setLength(0);
} else {
lineBuilder.append(c);
}
}
buffer.clear();
}
if (lineBuilder.length() > 0) {
lines.add(lineBuilder.toString());
}
} catch (IOException e) {
e.printStackTrace();
}
for (String line : lines) {
System.out.println(line);
}
}
}
读取一个大的list, 把数据写入文件中
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.List;
public class WriteListToFile {
public static void main(String[] args) {
List<String> dataList = yourLargeList;
String filename = "output_file.txt";
Path filePath = Paths.get(filename);
try {
if (Files.exists(filePath)) {
Files.delete(filePath);
}
int chunkSize = 1000000;
for (int i = 0; i < dataList.size(); i += chunkSize) {
int endIndex = Math.min(i + chunkSize, dataList.size());
List<String> chunk = dataList.subList(i, endIndex);
Files.write(filePath, chunk, StandardOpenOption.CREATE, StandardOpenOption.APPEND);
}
System.out.println("Data has been written to " + filename);
} catch (IOException e) {
e.printStackTrace();
}
}
}
import java.util.ArrayList;
import java.util.List;
public class SplitListIntoCollections {
public static void main(String[] args) {
List<Integer> originalList = new ArrayList<>();
for (int i = 1; i <= 100; i++) {
originalList.add(i);
}
int numCollections = 10;
int elementsPerCollection = originalList.size() / numCollections;
List<List<Integer>> collectionList = new ArrayList<>();
int startIndex = 0;
for (int i = 0; i < numCollections - 1; i++) {
List<Integer> subCollection = originalList.subList(startIndex, startIndex + elementsPerCollection);
collectionList.add(subCollection);
startIndex += elementsPerCollection;
}
List<Integer> lastSubCollection = originalList.subList(startIndex, originalList.size());
collectionList.add(lastSubCollection);
for (int i = 0; i < collectionList.size(); i++) {
System.out.println("Collection " + i + ": " + collectionList.get(i));
}
}
}
package com.ityj.algorithm.test;
import java.util.LinkedList;
import java.util.Queue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public class ParallelQueueConsumer {
public static void main(String[] args) {
Queue<String> queue = new LinkedList<>();
queue.offer("元素1");
queue.offer("元素2");
queue.offer("元素3");
queue.offer("元素4");
queue.offer("元素5");
int numConsumers = 3;
ExecutorService executor = Executors.newFixedThreadPool(numConsumers);
for (int i = 0; i < numConsumers; i++) {
executor.submit(new QueueConsumer(queue));
}
executor.shutdown();
try {
executor.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
System.err.println("线程池等待超时!");
executor.shutdownNow();
}
}
static class QueueConsumer implements Runnable {
private final Queue<String> queue;
QueueConsumer(Queue<String> queue) {
this.queue = queue;
}
@Override
public void run() {
while (true) {
String element;
synchronized (queue) {
if (queue.isEmpty()) {
break;
}
element = queue.poll();
}
consumeElement(element);
}
}
private void consumeElement(String element) {
System.out.println(Thread.currentThread().getName() + " 消费了:" + element);
}
}
}