Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java datastructures/ C++ STL equivalents?

Tags:

java

I just started learning java. I already know C++ and python. In order to learn Java, I am kinda drawing analogies from what I know in C++. I was an avid user of STL libraries in C++ (vectors, deques, stacks, hashmaps) http://www.sgi.com/tech/stl/stl_introduction.html

At the same time, python also have standard dicts, lists etc easily figurable in their docs.

I have been googling lately to find STL equivalent in Java but am unable to find so? Can someone point me to right resources?

like image 423
frazman Avatar asked Apr 02 '12 08:04

frazman


People also ask

What is the equivalent of STL in Java?

C++ has STL, the Standard Template Library, and Java has the Collections classes.

Which is better Java collection or C++ STL?

STL vs. containers: C++ provides well-designed STLs, whereas Java has Containers. The benefit of containers over STLs is that there are a few situations where STL doesn't have a direct solution.

Can we use STL in Java?

STL include performance as part of the interface requirements. This is not normally the case for java collections. In Java, the algorithms are organized by container, while in STL the algorithms are independent of the container on which they operate. STL uses value semantics so that assignment copies a collection.

What is the equivalent of TreeSet in C++?

Use std::set , which is typically implemented as a binary search tree. Its insert() , erase() and find() methods are logarithmic in size, but can do better if a hint is given. The logarithmic complexity is reffered to the the Java TreeSet.


1 Answers

  1. std::vector -> j.u.ArrayList
  2. std::unordered_map -> j.u.HashMap
  3. std::map -> j.u.TreeMap
  4. std::set -> j.u.LinkedHashSet
  5. std::unordered_set -> j.u.HashSet
  6. std::stack -> j.u.ArrayDeque (j.u.Stack is deprecated),
  7. std::queue -> use j.u.LinkedList (also take a look on j.u.ArrayBlockingQueue).
  8. std::priority_queue -> j.u.PriorityQueue
  9. std::span -> no definite analogue
  10. std::deque -> j.u.ArrayDeque
  11. std::list -> j.u.LinkedList
  12. std::forward_list -> no analogue

Important notice: the mentioned associations have similar API but may have different asymptotics of operations and implementation.

Notice: j.u means java.util.

like image 96
Commander Tvis Avatar answered Oct 22 '22 03:10

Commander Tvis