我有一个以Array为值的地图。现在,所有这些数组都需要转换为列表。最后,所有列表都应合并为一个列表。
现在可用,
Map<String, Class1[]> master_map= new HashMap<>();
Class1 c1 = new Class("Name1", "Address1");
Class1 c2 = new Class("Name2", "Address2");
ClassArray[0] = c1;
ClassArray[1]= c2;
master_map.put("FirstValue", ClassArray);
Class1 c3 = new Class("Name3", "Address3");
Class1 c4 = new Class("Name4", "Address4");
ClassArray[0] = c3;
ClassArray[1]= c4;
master_map.put("SecondValue", ClassArray);}
现在我希望得到如下结果。
List<Class1> result = new ArrayList<>();
//This above list to be as {c1, c2, c3, c4}
期待java8中的代码
答案 0 :(得分:3)
您需要在条目流中展平内部数组:
template<typename T>
class ThreadSafeQueue
{
public:
ThreadSafeQueue() {}
~ThreadSafeQueue() {}
void Push(T d)
{
std::unique_lock<std::mutex> locker(mutex);
data.push(std::move(d));
locker.unlock();
cond.notify_one();
}
T* get()
{
std::unique_lock<std::mutex> locker(mutex);
cond.wait(locker, [=]() { return stop || !data.empty(); });
if (stop && data.empty()) {
nullptr;
}
T res = std::move(data.front());
data.pop();
return &res;
}
bool Empty()
{
std::lock_guard<std::mutex> guard(mutex);
return data.empty();
}
std::queue<T> data;
std::mutex mutex;
std::condition_variable cond;
bool stop = false;
};
ThreadSafeQueue<SDL_Surface*> queue;
void LoadingThread(const std::string& path)
{
SDL_Surface* surf = std::async(std::launch::async, IMG_Load, path.c_str()).get();
queue.Push(surf);
}
void Texture::LoadFromFile(const std::string& path)
{
LoadingThread(path);
SDL_Surface* surface = nullptr;
if (!queue.data.empty() && path != "") {
surface = *queue.get();
if(surface) {
w = surface->w;
h = surface->h;
pixels = surface->pixels;
} else return;
} else return;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB, GL_UNSIGNED_BYTE, pixels);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glGenerateMipmap(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, 0);
}
void Draw()
{
drawThread = std::make_unique<std::thread>([&]() {
drawContext->MakeCurrent();
renderer2D.Create(window);
std::unique_ptr<Texture> texture = Texture::LoadPNG("Textures/test.png");
while (!quit) {
//glClearColor - I prefere 0 - 255 format
Renderer::ClearColor(sinf(SDL_GetTicks() / 500.0f) * 255.0f, 0.0f, 255.0f, 255.0f);
//glClear() color buffer bit default
Renderer::ClearBuffers();
//simple batch renderer
renderer2D.RenderClear();
renderer2D->Draw(texture, { 0.0f, 0.0f, 50.0f, 50.0f });
renderer2D.RenderPresent();
window.SwapWindows();
}
});
}
答案 1 :(得分:1)
您可以使用条目集遍历地图,对于每个值(即数组),可以使用Arrays.asList将其转换为列表,并将其添加到最终列表中。
Map<String , Integer[]> hm = new HashMap<String,Integer[]>();
List<Integer> finalList = new ArrayList<Integer>();
for(Entry<String, Integer[]> entry:hm.entrySet()) {
finalList.addAll(Arrays.asList(entry.getValue()));
}
使用并行流可以像这样
Map<String , Integer[]> hm = new HashMap<String,Integer[]>();
List<Integer> finalList = hm.values().parallelStream().flatMap(Arrays::stream).collect(Collectors.toList())
这是整数数组的简单示例。您可以参考并得出您的解决方案。
答案 2 :(得分:0)
另一个Java 8简单变体:
List<Class1> result = new ArrayList<>();
master_map.entrySet().stream().forEach(element -> {
result.addAll(Arrays.asList(element.getValue()));
});
答案 3 :(得分:0)
另一个例子
List<Class1> x = new ArrayList<>();
master_map.forEach((k,v) -> x.addAll(Arrays.asList(v)));