For loop string in java

1. For loop string in java

for (char c : string1.toCharArray()) {


String s = "xyz";
for(int i = 0; i < s.length(); i++)
   char c = s.charAt(i);

2. hash map size


3. hash map compare keys and values

Make an equals check on the keySet() of both HashMaps.


If your Map contains String keys then it is no problem, but if your Map contains objA type keys then you need to make sure that your class objA implements equals().

You can use

        System.out.println("Map are same");
    }else {
        System.out.println("Map are not same");

Here it will check whether keySet of two HashMap are same or not.


   Map<String, String> mapA = new HashMap<String, String>();
    mapA.put("A", "1");
    mapA.put("B", "2");

    Map<String, String> mapB = new HashMap<String, String>();
    mapB.put("B", "3");
    mapB.put("A", "1");

        System.out.println("Maps are same");
    }else {
        System.out.println("Maps are not same");

Out put

  Maps are same


        if (hash1.size() == hash2.size()) {

            // compare keys

            if (hash1.keySet().equals(hash2.keySet())) {

                // compare values

                Set<String> set = hash1.keySet();

                String[] arr = set.toArray(new String[set.size()]);

                for (String str : arr) {

                    if (hash1.get(str) == hash2.get(str)) {


                    } else {

                        return false;




                return true;

            } else

                return false;

        } else {

            return false;



Color-fill UIImage programmatically in iOS7

With iOS7 you can color-fill a black-transparent png file, using theimageWithRenderingMode: method and the tintColor property.

The imageWithRenderingMode: creates and returns a new image object with a specified rendering mode.
The rendering mode to use, is the UIImageRenderingModeAlwaysTemplate that draw the image as a template image, ignoring its color information.
Finally, the tintColor is the fill UIColor.

Color-fill UIImage programmatically with iOS7 | Daniele Galiotto |

So, writing the code below or downloading the demo, you can color-fill your png.

UIImage *img = [UIImage imageNamed:@"smile.png"]; self.imgOriginal.image = img; self.imgColorFill.image = [img imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]; self.imgColorFill.tintColor = [UIColor colorWithRed:0.98 green:0.47 blue:0 alpha:1];


mongodb not working “ERROR: dbpath does not exist”

I’m getting the following error when I try to run “mongod” in the terminal. I’ve tried uninstalling, reinstalling, and restarting the machine. Any suggestions on how to get it working would be amazing.


dbpath (/data/db) does not exist.
 Create this directory or give existing directory in --dbpath.

Side note: Node also stopped working on my machine around the same time that I got this error.

        throw er; // Unhandled 'error' event
Error: failed to connect to [localhost:27017]

Any help would be much appreciated!

share|improve this question
Did you follow the directions in the error message? –  JohnnyHK Jul 6 at 18:47

2 Answers

This should work to ensure that the directory is set up in the right place so that Mongo can find it:

sudo mkdir -p /data/db/

sudo chown `id -u` /data/db

share|improve this answer

You need to create the directory on root /data/db or set any other path with the following command :

mongod --dbpath /srv/mongodb/

See the example link

Node.js – events js 72 throw er unhandled ‘error’ event

I’m new to Node.js and wish to run a program using streams. With other programs, I had to start a server simultaneously (mongodb, redis, etc) but I have no idea if I’m supposed to run one with this. Please let me know where I am going wrong and how I can rectify this. Thanks in advance.

This is the program:

var http = require('http'),
feed = '';

function decide(cb) {
setTimeout(function () {
if ( { return console.log('rejected'); }        
}, 2000);

http.get(feed, function (res) {

decide(res.pipe.bind(res, process.stdout));

//using anonymous function instead of bind:
// decide(function () {
//   res.pipe(process.stdout)
// });


This is the cmd output:

<b>C:5-Employing Streams5-Employing Streams\23-Playing with pipes>node npm_stre

throw er; // Unhandled 'error' event
Error: Parse Error
at Socket.socketOnData (http.js:1583:20)
at TCP.onread (net.js:527:27)
share|improve this question
Are you sure the URL is right? I get nothing from there, but I do get something from (without the underscore). Also see here for an example of how to catch errors from http.get. –  user3374348 Apr 9 at 12:35

2 Answers

Close nodejs app running in another shell. Restart the terminal and run the program again.

Another server might be also using the same port that you have used for nodejs. Kill the process that is using nodejs port and run the app.

To find the PID of the application that is using port:8000

$ fuser 8000/tcp
8000/tcp:            16708

Here PID is 16708 Now kill the process using the kill [PID] command

$ kill 16708
share|improve this answer

Well, your script throws an error and you just need to catch it (and/or prevent it from happening). I had the same error, for me it was an already used port (EADDRINUSE).

share|improve this answer
Please provide code for this answer… –  Adam Aug 27 at 6:05
There is no code to provide. –  lama12345 Aug 28 at 0:06

ios Basic 总结


如何给一个类增加一个函数方法,在C++中是不可能的(或是很难)但是在Objective-C中确实很简单的事情,category 做的就是这件事情。

当第一次看到category 时,我几乎肯定这是一个非常坑爹的设计,因为这个可以完美的覆盖原有类的方法,如果使用不当,绝对是灾难性的错误。但是,随着使用时间变长category 的好处也慢慢体会出来。

  • 给系统原有的类或是第三方的类增加函数,而不用使用一个新的类,这样使用会更加方便,特别是使用IB时,不需要将系统的类换成自己定义的类,这样减少了非常多的移植问题和同步工作。 当然,如果使用不当,也会遇到问题
  • 函数覆盖问题,之前提到过
  • 增加大量的category 会降低系统性能,特别是如果大量的加到一些基础类上,如NSObject 等,因为,几乎整个系统都在使用这些类,而过多的category,不仅降低自己代码的效率,也会降低framework代码的效率,这里的效率,应该主要在类初始化上,和消息发送上面。

iOS APP software architecture

View Controller A (mainVC) and View Controller B(detailVC)

1. change detailVC value, but mainVC B did not change.


in B, write a protocol

@protocol CUDetailViewControllerDelegate <NSObject>

- (void)detailVC:(CUDetailViewController *)vc dataChanged:(NSNumber *)data;


in B, write a protocol After change in detail B page, we could use delegate to let A know.

- (IBAction)changeButtonClicked:(id)sender {
    int value = arc4random() % 100;
    [CUDataDAO setData:value];

    self.displayLabel.text = [@(value) stringValue];

    if ([self.delegate respondsToSelector:@selector(detailVC:dataChanged:)]) {
        [self.delegate detailVC:self dataChanged:@(value)];

Let’s continue thinking of how about we need sync with VC C, D, E,…



NSString *const kCUDataChangedNotification = @"CUDataChangedNotification";

[[NSNotificationCenter defaultCenter] postNotificationName:kCUDataChangedNotification

Information Flow:

《Advanced iOS Application Architecture and Patterns》 中,把这个图叫做information flow。我们的直觉会告诉我们,这个信息的传递,应该是自上而下的树或是森林,而且最好是一个层次平衡结构,要清晰,每一个位置都有相对于的职责。那我们就需要制定一个规则。


Java Exception

Exceptions are 2 types.

1.Checked (needs to be defined in throws clause of a method) [compile]

sevlet Exception,




2. Uncheked (compiler doesn’t care):  [runtime]

Arithmetic Exception,


NegativeArraySizeException -happens when u specify negative size for array

NumberFormat Exception -happens in the case of wrong number format

NullPointer Exception – happens if u specify a wrong or unavailable destination (since almost every method would have to declare throwing it like this

public void myMethod(String param) throws NullPointerException {
public void myMethod(String param) throws NullPointerException {