Thursday 17 March 2022

how to atomically update values in a ConcurrentHashMap in Java using Compute and ComputeIfAbsent methods?

Hi,  ConcurrentHashMap is a thread-safe implementation of the Java Map interface, designed to be used in multi-threaded environments. It allows multiple threads to access and modify the map simultaneously without causing data corruption or race conditions.

One of the key features of ConcurrentHashMap is that it provides atomic operations to update its elements. Atomic operations are operations that are performed as a single, indivisible unit of work, which means they are guaranteed to be executed completely or not at all. This is important in multi-threaded environments where multiple threads may try to modify the same element at the same time.

Read more »

Labels: , ,

Friday 4 October 2019

how many types available for getting string variable length in perl?

Method 1:

my $c = map $_, $h =~ /(.)/g;


Method 2:

my $c = split '', $h;


Method 3:

my $count = () = "hello" =~ /./g;


Method 4:

print length $h;


Method 5:

$cnt = "hello" =~ tr/[a-z][A-Z][0-9]//;


Method 6:

$h='hello';
my $v=$h=~ s/[a-z]//g;


Method 7:

print  @_ = push(@_, map $_, (split '', 'hellgo'));


Method 8:

print  @_ = push(@_, grep $_, (split '', 'hellgo'));


Method 9:

print push(@_, grep $_, "hello" =~ /./g);


Method 10:

print push(@_, map $_, "hello" =~ /./g);


Method 11:

print push(@_, grep $_, reverse "hello" =~ /./g);


Method 12:

print push(@_, map $_, reverse "hello" =~ /./g);


Method 13:

print push(@_, map $_, (split '', 'hellgo'));


Method 14:


my $c = grep/./, (reverse split '', 'hello');
print $c;


Method 15:

my $c = map $_, (reverse split '', 'hello');
print $c;


Method 16:

my $c = map $_, (reverse "hello" =~ /./g);
print $c;


Method 17:

my $c = grep/./, (reverse "hello" =~ /./g);
print $c;


Method 18:

my $c = map $_, (reverse split '', 'hello');
print $c;


Method 19:

my $c = grep/./, (reverse split '', 'hello');
print $c;


Method 20:

print scalar map $_, 'hello' =~ /(.)/g;


Method 21:

print scalar grep/./,'helloh' =~ /(.)/g;


Method 22:

print scalar  map $_, (reverse "hello" =~ /./g);


Method 23:

print scalar  map $_, (reverse split '', 'hello');


Method 24:

print scalar  map $_, (split '', 'hello');


Method 25:

print $r = @_= map $_, (split '', 'hello');


Method 26:

print $r = @_= grep $_, (split '', 'hello');


Method 27:

print $r = @_= grep $_, (reverse "hello" =~ /./g);


Method 28:

print $r = @_= grep/./,'helloh' =~ /(.)/g


Method 29:

print $r = @_= map  $_, (reverse "hello" =~ /./g);


Method 30:

print $r = @_= map/./,'helloh' =~ /(.)/g


Method 31:

print $r = @_= map $_, (split '', 'hello');


Method 32:

print $r = @_= grep $_, (split '', 'hello');


Method 33:

print $r = @_= grep $_, (reverse "hello" =~ /./g);


Method 34:

print $r = @_= grep/./,'helloh' =~ /(.)/g


Method 35:

print $r = @_= map  $_, (reverse "hello" =~ /./g);


Method 36:

print $r = @_= map/./,'helloh' =~ /(.)/g


Method 37:

print scalar  map $_, (reverse split '', 'hello');


Method 38:

print length reverse  grep/./,'helloh' =~ /(.)/g;


Method 39:

print length reverse  map $_,'helloh' =~ /(.)/g;


Method 40:

print grep/./, (reverse "hello" =~ tr/[a-z][A-Z][0-9]//);


Method 41:

print map $_, (reverse "hello" =~ tr/[a-z][A-Z][0-9]// );




For More Methods Reach Me:@ letscrackperlinterviewblogspot@gmail.com  

Labels: , , , , ,

Monday 15 April 2024

Component Iteration in React JSX Strategies for Seamless Repetition

 Transitioning from traditional templating to JSX in React can be a bit perplexing, especially when you’re accustomed to looping constructs like for. In JSX, which ultimately compiles down to JavaScript function calls, you need to employ a different approach to achieve repetitive rendering. Let’s explore various techniques to repeat components in JSX.

Read more »

Labels:

Sunday 4 August 2019

How to auto generate values for a hash by giving an array or list elements as hash keys in perl?



Method 1:

                       %hash = map {$_=>1} "letcrackperl"=~/./g;
                        print   %final;

Method 2:

                        %final = map { $_ => 1 } 1..5;
                        print  %final;

Method 3:

                        @array=(9,2,3,4,3,6);
                       %final = map { $_ => 1 } @array;
                        print  %final;

Method 4:

                       @array=(9,2,3,4,3,6);
                       @final{@array} = (1) x @array;
                        print  %final;

Method 5:

                       @array=(9,2,3,4,3,6);
                       %final = map { $_,1 } @array;
                        print  %final;

Method 6:

                       @array=(9,2,3,4,3,6);
                       %final = map { $_,1 } split(",",$line)
                        print  %final;

Monday 1 March 2021

Boost Your Python Code Performance: Tips for Optimizing with JIT Compilation and Profiling Tools

Python is a powerful language for scientific computing and data analysis, but it's also known for its slower execution speed compared to compiled languages like C++ and Java. However, there are ways to optimize Python code for better performance. In this article, we'll explore some tips and tricks for optimizing Python code, including using JIT (just-in-time) compilation and profiling tools.

Use JIT Compilation

One way to improve the performance of your Python code is to use JIT compilation. JIT compilation is a technique that dynamically compiles code at runtime, rather than ahead of time. This allows the interpreter to optimize the code based on the actual data that is being processed, resulting in faster execution.

Read more »

Labels: ,

Friday 1 November 2019

perl - find missing numbers in array

Method 1:

@a = (1,3,4,6); @c = map $a[$_-1]+1..$a[$_]-1, 1..@a-1; print join(" ", @c);




Read more »

Labels:

Friday 9 December 2022

how to print the contents of perl hash?

 

Method1

 print "$_ $h{$_}\n" for (keys %h);


Method2:
 
while (my ($k,$v)=each %h){print "$k $v\n"}

Method3:
 
print "@{[%hash]}";

Method4:
 
print map { "$_ $h{$_}\n" } keys %h;

Method5:
 
print "$_ $h{$_}\n" for keys %h

Method6:
 
foreach(keys %my_hash) { print "$_ / $my_hash{$_}\n"; }

Method7:
 
map {print "$_ / $my_hash{$_}\n"; } keys %my_hash;

Method8:
 
print map {$_ . " "} %h, "\n";

Labels: , ,

Tuesday 10 September 2019

How Many Possible ways to remove Duplicates in an array using perl?


Method 1:

sub filter {
    my %hash;
    grep !$hash{$_}++, @_;
}

my @array = qw(e f g h i);
my @final= filter(@array);


Method 2:

use List::MoreUtils qw(uniq);
my @array = qw(1 1 1 2 3 4 4);
my @final= uniq(@array);


Method 3:

my %hash = ();
foreach my $item (@array)
{
    $hash{$item}++;
}
my @final = keys %hash;


Method 4:

my @array=qw( 3 2 3 4 4 3 2);
my @final=keys %{{ map{$_=>1}@array}};
print join ' ', sort{$a<=>$b} @final;


Method 5:

%hash=();
my @array = qw(e j g h i);
@final = grep { ! $hash{$_} ++ } @array;


Method 6:

my @array;
my @finals;

foreach my $var ( @array )
{
    if (! grep(/$var/, @finals ))
    {
          push( @finals, $var );
     }
}


Method 7:

my @array = qw(e f g h i);
my %hash;
@hash{@array}=(1..@array);
@final=keys%hash;


Method 8:

my @final = keys {map {$_ => 1} @array};


Method 9:

@array = qw(e f g h i);
%hash1   = map { $_ => 1 } @array;
@final = keys %hash1;


Method 10:
my @array = qw(e f g h i);
my %hash;
foreach my $v (@array){
        $hash{$v}=1;
}
@final=keys%hash;


kaavannan perl blogspot

Labels: , ,