How to import a jar in Eclipse?

You can add a jar in Eclipse by right-clicking on the Project → Build Path → Configure Build Path. Under Libraries tab, click Add Jars or Add External JARs and give the Jar. A quick demo here. The above solution is obviously a “Quick” one. However, if you are working on a project where you … Read more

Using HTML5/JavaScript to generate and save a file

Simple solution for HTML5 ready browsers… function download(filename, text) { var pom = document.createElement(‘a’); pom.setAttribute(‘href’, ‘data:text/plain;charset=utf-8,’ + encodeURIComponent(text)); pom.setAttribute(‘download’, filename); if (document.createEvent) { var event = document.createEvent(‘MouseEvents’); event.initEvent(‘click’, true, true); pom.dispatchEvent(event); } else { pom.click(); } } Usage download(‘test.txt’, ‘Hello world!’);

How can I select an element in a component template?

Instead of injecting ElementRef and using querySelector or similar from there, a declarative way can be used instead to access elements in the view directly: <input #myname> @ViewChild(‘myname’) input; element ngAfterViewInit() { console.log(this.input.nativeElement.value); } StackBlitz example @ViewChild() supports directive or component type as parameter, or the name (string) of a template variable. @ViewChildren() also supports … Read more

Convert Data URI to File then append to FormData

After playing around with a few things, I managed to figure this out myself. First of all, this will convert a dataURI to a Blob: function dataURItoBlob(dataURI) { // convert base64/URLEncoded data component to raw binary data held in a string var byteString; if (dataURI.split(‘,’)[0].indexOf(‘base64’) >= 0) byteString = atob(dataURI.split(‘,’)[1]); else byteString = unescape(dataURI.split(‘,’)[1]); // … Read more

How to filter a Java Collection (based on predicate)?

Java 8 (2014) solves this problem using streams and lambdas in one line of code: List<Person> beerDrinkers = persons.stream() .filter(p -> p.getAge() > 16).collect(Collectors.toList()); Here’s a tutorial. Use Collection#removeIf to modify the collection in place. (Notice: In this case, the predicate will remove objects who satisfy the predicate): persons.removeIf(p -> p.getAge() <= 16); lambdaj allows … Read more

Android activity life cycle – what are all these methods for?

See it in Activity Lifecycle (at Android Developers). onCreate(): Called when the activity is first created. This is where you should do all of your normal static set up: create views, bind data to lists, etc. This method also provides you with a Bundle containing the activity’s previously frozen state, if there was one. Always … Read more

How to use multiprocessing pool.map with multiple arguments

is there a variant of pool.map which support multiple arguments? Python 3.3 includes pool.starmap() method: #!/usr/bin/env python3 from functools import partial from itertools import repeat from multiprocessing import Pool, freeze_support def func(a, b): return a + b def main(): a_args = [1,2,3] second_arg = 1 with Pool() as pool: L = pool.starmap(func, [(1, 1), (2, … Read more