taklo's blog

By taklo, history, 5 years ago, In English

Hello Codeforces Community, I am new to java until now I have done all of my competitive programming in C++ 17 and now I want to learn basic JAVA through sports programming . I have searched for it on google but i'm confused with various type of map containers available in java , Please guide me .

#include<iostream>
#include<map>
using namespace std;
int main(){
	map<int ,int > M;
	int n;
	cin>>n;
	int a[n];
	for(int i=0;i<n;i++){
		cin>>a[i];		
		M[a[i]]++;
	}
	if(M.find(10)!=M.end()){
		cout<<"10 is present \n ";
	}
	for(auto x: M){
		cout<<x.first<<" "<<x.second<<"\n";
	}
	return 0;
}

Please Tell me the alternative Java Code Thanks in Advanced

  • Vote: I like it
  • -7
  • Vote: I do not like it

| Write comment?
»
5 years ago, # |
  Vote: I like it +12 Vote: I do not like it

Map is an interface in Java. You can either use HashMap<> or TreeMap<> as a concrete implementation of the Map interface. So if you want a map with key as Integers and value as String. You can do something like

Map<Integer, String> hMap = new HashMap<>();
Map<Integer, String> tMap = new TreeMap<>();

Worst case running time in both TreeMap and HashMap is $$$\mathcal{O}(\log n)$$$ (Each bucket has a balanced binary search tree instead of a linked list. They introduced this improvement in Java 8). But on an average you get $$$\mathcal{O}(1)$$$ for the HashMap

  • »
    »
    5 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Thanks for your time, That means HashMap is preferable according to you

    • »
      »
      »
      5 years ago, # ^ |
        Vote: I like it +1 Vote: I do not like it

      It depends on the use case. If all you are doing is just insertions, deletions and look ups then yeah HashMap is better. But if you want to find min/max/floor/ceil then you need to use TreeMap.

  • »
    »
    5 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Thanks, can you also tell equivalent of vector<vector> in c++ for storing graph, for java ? is using Hashmap good for storing graph in java.

    • »
      »
      »
      5 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Java also has Vector. Lol.

      But Vector is slower than ArrayList from what I heard. So use ArrayList I guess?

      • »
        »
        »
        »
        5 years ago, # ^ |
        Rev. 2   Vote: I like it +6 Vote: I do not like it

        actually they say, both vector and arraylist are slower, that's why I asked, but thanks

    • »
      »
      »
      5 years ago, # ^ |
        Vote: I like it +10 Vote: I do not like it

      I generally use the following for adjacency lists

      ArrayList<Integer>[] adj = new ArrayList[V]; // you'll have to iterate the array and initialize each list before using. 
      

      But sometimes ArrayList based implementations tend to be slow. In that case you can use plain old arrays. For example, I use uwi's implementation (the packU method iterates over the edge list and constructs an adjacency list).

»
5 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Can anyone also tell me what are the alternatives for lower_bound and upper_bound in Java?