Best Java Data Structures Book

0 views
Skip to first unread message

Gwenda Arguin

unread,
Aug 5, 2024, 12:56:19 PM8/5/24
to keydefara
SinceI have a lot of users(millions), every user can have potentially thousands of events, and, moreover, I have to access every event of a user with process() method, I think that using structures like HashMaps etc. would not be helpful(If am wrong please tell me).However, it is obvious that with this number of elements, good performance is a need.

If, however, you insist on doing this in your own code, you might want to have a look at the LinkedHashMap class. It allows direct access to its elements with constant (i.e. O(1)) complexity, while also combining an internal linked list to allow fast iteration over all elements.


On the other hand, if you only need to access events based on their insertion order, then you cannot do much better than ArrayList, since it supports indexed access to its contents with a constant complexity. If you just need to process them in a queue or stack, Java has several implementations of the Deque interface that might interest you.


1- in current scenario, if concurrent users are not a concern then you can easily go for arraylist as its faster simpler data structure else if concurrent users are the concern then you can easily go for vector to store your events.


If your data fits into main memory, your best solution would be java collections and plain arrays (depending on needs for random access, sequentiality, needs to persisting changes or whatever else) If your data grows past single system memory your will have better performance with some clusterable no-sql solution (again, choice of the right tool depends on what you likt to do with your data)


I am used to coding in PHP but I am not really proficient with Java and this has been a problem for some time now. I expect it to be a fairly easy solution, however I cannot find any good example code any way I search it, so here goes:


I am programming a game that takes place in a 2d random generated infinite world on a tile based map (nitpicking: I know it will not be truly infinite. I just expect the world to be quite large). The usual approach of map[x][y] multidimensional array started out as a basic idea, but since Java does not provide a way for non-integer (i.e. negative) array key shenanigans like PHP does, I cannot properly have a (-x,+x,-y,+y) coordinate system with array keys.


I have read about quad trees and R-trees and the like. The concept is exciting, however I haven't seen any good, simple example implementation in Java. And besides I am not really sure if that is what I need exactly.


2) If you know the dimensions of your world from the start you could just modify your getter to allow the API to accept negatives and [linearly] transform them into positives. So for example if your world is 100x1000 tiles and you want (-5,-100), you would have WorldMap.getTile(-5,-100) which would translate to return tileArray[x+mapWidth/2][y+mapHeight/2]; which is (45,400)


To overcome this, instead of using a map within a map (which would be messy and very inefficient) I used a generic Pair class (not something that you'll find in the stock java library) although you could replace this with a Position class (virtually the same code, but not generic, instead integers or floats).


[x/y would be any coordinate you wish to place (this allows negative coordinates without any mess!), "new GrassTile()" is just an example of placing a tile of a certain type during map creation. Obviously - as previously stated - the Pair class is replacable.]


Specialized data structures have their specific uses. Unless you can come up with a good reason why your game needs a spatial index, don't build one. If your typical scenario is "iterate over the visible area, find out what tile is visible at each of the squares", then you need a structure that gives you a quick, random, access to a value stored under a specific key. Such structure is a HashMap (what PHP uses is a kind of a LinkedHashMap, but you were probably not using the "linked" part).


The best thing: if you keep using the Map interface, you will not be locked out, and you will be able to make a lot of improvements. Like wrapping the HashMap into an object that creates parts of the map using some algorithmic techniques.


Or if you're used to associative arrays like PHP has, the use the corresponding structure in Java, which is a Map (HashMap would be OK) : define a Coordinate class with appropriate equals and hashCode methods, and use a HashMap. Making Coordinate immutable makes the code more robust, and allows caching the hashCode.


A chunk is always a fixed size of real world coordinate (say 128x128). Then you have a class Chunk where you have a fixed array (128x128) with all the information for every pixel. And you store your chunks into a Map as was already explained by others. I would recommend a HashMap.


Whenever your player is in a certain region, the neccessary chunks are loaded from the map and then you can access the fixed size array in there. If the chunk knows, where it is placed in x/y coordinates, you can even have some support function like Pixel Chunk#getPixel(long x, long y) or so...


Btw: This also gives you an easy way to postpone generation of the whole world until it is really needed: At start, nothing is generated and as soon as a Chunk is accessed in the map, that is not yet generated, you can just generate it then. Or you could fill it up at startup if that's easier for you. (filling an infinite world will take a long time though, even if it is pseudo infinite)


You probably want to use an implementation of Map. HashMap, SortedMap, etc depending on how much data you intend to store and your access patterns (Sorted Map is very good for sequential access, HashMap is better for random access).


I am not aware of any implementations in Java, but it is trivial. Just store in a single byte a bitfield for which nodes are currently linked and use a linked list to keep those references (for each Octree-node a separate list of max. 8 entries).


So a quadtree is sparce and pretty good ad adjacency calculations, except for the "infinite" provision it's perfect. Why not just use one of those sized to 100x what you think you might need (it's sparse, not really a big deal). If you ever get to the point where you are near the edge, allocate a new quadtree that is much bigger.


I believe if you are careful (you may have to implement your own quadtree) you can "upgrade" the quadtree with very little effort and no copying--it should be simply a matter of prefixing all your existing addresses with some bits (the addresses are in binary, quadtrees each bit represents dividing the existing universe in half in one dimension or the other).


Basic Java Programming: A solid understanding of the Java programming language is essential before diving into data structures. Familiarize yourself with key concepts, syntax, data types, and control flow statements in Java.


Object-Oriented Programming (OOP): Java Data Structures heavily rely on OOP principles, such as inheritance, polymorphism, and encapsulation. Understanding these concepts will enable you to design and implement efficient data structures.


Understanding of Algorithms: Data structures are typically utilized to solve specific problems or perform operations with optimal time and space complexity. Enhance your knowledge of algorithms and their analysis to understand how data structures fit into the broader context of problem-solving.


Array Manipulation: Arrays serve as fundamental building blocks for many data structures. Practice working with arrays, their manipulation, and common operations such as insertion, deletion, sorting, and searching.


Linked Lists: Gain proficiency in implementing and manipulating linked lists, a linear data structure that consists of nodes connected through references. Understand various linked list types, such as singly linked, doubly linked, and circular linked lists.


Trees and Binary Trees: Develop an understanding of tree structures, including binary trees. Master concepts like binary tree traversal (pre-order, in-order, post-order), binary search trees, and balanced trees (e.g., AVL or Red-Black trees).


Graphs: Gain knowledge of graph theory and different graph representations. Learn about graph traversal algorithms (e.g., Breadth-First Search and Depth-First Search), shortest path algorithms, and minimum spanning trees.


Hashing: Understand the concept of hashing and its applications in data structures. Explore hash tables, collision resolution techniques, and hashing algorithms, which are often used for efficient retrieval and search operations.


With Java Data Structures skills, you can pursue various job roles in the field of software development and data analysis. Some potential job options for individuals proficient in Java data structures include:


Database Administrator: With Java data structures skills, you can assist in designing, optimizing, and managing databases, ensuring efficient storage and retrieval of data using Java-based technologies.


Java Data Structures are specific ways to organize and store data in a Java program. They provide a means to efficiently manipulate and access data based on different requirements and scenarios. Some commonly used Java Data Structures include arrays, linked lists, stacks, queues, trees, graphs, and hash tables. Each data structure has its own characteristics, advantages, and use cases, allowing programmers to choose the most suitable structure based on their requirements. Understanding Java Data Structures is crucial for developing efficient algorithms and writing optimized code in Java programming.

3a8082e126
Reply all
Reply to author
Forward
0 new messages