Tuesday, April 3, 2018

Java Misc Part 5



http://massivetechinterview.blogspot.com/2018/07/java-misc-part-6.html

https://droidyue.com/blog/2018/07/16/variable-localname-might-not-have-been-initialized/
其实之所以这样做就是一种对程序员的约束限制。因为程序员(人)是(有些情况下)是靠不住的,假使局部变量可以使用默认值,我们总会无意间忘记赋值,进而导致不可预期的情况出现。这
“之所以设计成这样完全是一种策略决定,并非是我力不能及,年轻人,我只能帮你到这里了。”,Javac如是说
https://stackoverflow.com/questions/11686398/conditional-operator-in-concatenated-string
This is an example of the importance of understanding operator precedence.
You need the parentheses otherwise it is interpreted as follows:
String test = ("test" + testInteger) == null ? "(null)" : testInteger.toString();

Note: Use explicit parentheses when there is even the possibility of confusion.
https://en.wikipedia.org/wiki/Order_of_operations
1()   []   ->   .   ::Function call, scope, array/member access
2!   ~   -   +   *   &   sizeof   type cast   ++   --  (most) unary operators, sizeof and type casts (right to left)
3*   /   % MODMultiplication, division, modulo
4+   -Addition and subtraction
5<<   >>Bitwise shift left and right
6<   <=   >   >=Comparisons: less-than and greater-than
7==   !=Comparisons: equal and not equal
8&Bitwise AND
9^Bitwise exclusive OR (XOR)
10|Bitwise inclusive (normal) OR
11&&Logical AND
12||Logical OR
13? :Conditional expression (ternary)
14=   +=   -=   *=   /=   %=   &=   |=   ^=   <<=   >>=Assignment operators (right to left)
15,Comma operator

最近遇到的几个问题集合

如何获取 T 的 class
在写BaseDao 之类的代码的时候,经常会遇到获取泛型T的class的情况?我们发现并没有T.class这种写法,那怎么办呢?想起之前写的Hibernate 里面有相关的代码。通过反射获取T的class
    private Class<T> clz;

    public AbstractDao(){
        this.clz = (Class<T>)
                ((ParameterizedType)getClass()
                        .getGenericSuperclass())
                        .getActualTypeArguments()[0];
    }

首先 @Value 注解可以方便的获取配置文件*.properties的参数。
在写代码的时候遇到这样一个问题。为了减少重复代码,我们通常需要写一个抽象类把共有的方法抽象到Abstract 类中。AbstractDao<T> 如果遇到需要向这个class的构造方法注入参数。且这个参数是通过抽象方法获取的。且这个数据是使用 Spring的 @Value 注解获取的。这个描述比较绕,我们直接看代码:



1
2
3
4
5
6
7
8
9
public class AbstractDao<T>{
    private int tableId;
    
    public AbstractDao(){
        this.tableId = setTableId();
    }
    
    protected abstract int setTableId();
}
1
2
3
4
5
6
7
8
9
public class UserDao extends AbstractDao<User>{
    
    @Value("${tableid.user}")
    private int userTableId;

    protected int setTableId() {
        return buserTableId;
    }
}

代码运行起来以后,我们发现 userTableId,并不能取到相应的值,这个时候@Value失效了。实际上这个问题的根源是因为@Value的加载是发生在对象实例化之后。也就是首先调用对象的构造函数,然后再获取配置文件中的数据。
解决的方案是使用注解 @PostConstruct,意思是构造函数执行完以后再 执行注解标记的方法。我们可以吧抽象函数做如下修改:



1
2
3
4
5
6
7
8
9
10
public class AbstractDao<T>{
    private int tableId;
    
    @PostConstruct
    public init(){
        this.tableId = setTableId();
    }
    
    protected abstract int setTableId();
}

http://www.java67.com/2017/10/java-8-convert-arraylist-to-hashmap-or.html
LinkedHashMap<String, Integer> linked = listOfString.stream().collect( toMap(Function.identity(), String::length, (e1, e2) -> e2, LinkedHashMap::new));

Read more: http://www.java67.com/2017/10/java-8-convert-arraylist-to-hashmap-or.html#ixzz5Ktz03rPJ

http://sayhiai.com/index.php/archives/8/
当一个线程正在某一个对象的同步方法中运行时调用了这个对象的wait方法,那么这个线程将释放该对象的独占锁并被放入这个对象的等待队列。注意,wait()方法强制当前线程释放对象锁。这意味着在调用某对象的wait()方法之前,当前线程必须已经获得该对象的锁。因此,线程必须在某个对象的同步方法或同步代码块中才能调用该对象的wait()方法。
当某线程调用某对象的notify()或notifyAll()方法时,任意一个(对于notify())或者所有(对于notifyAll())在该对象的等待队列中的线程,将被转移到该对象的入口队列。接着这些队列将竞争该对象的锁,最终获得锁的线程继续执行。如果没有线程在该对象的等待队列中等待获得锁,那么notify()和notifyAll()将不起任何作用。在调用对象的notify()和notifyAll()方法之前,调用线程必须已经得到该对象的锁。因此,必须在某个对象的同步方法或同步代码块中才能调用该对象的notify()或notifyAll()方法。

http://fahdshariff.blogspot.com/2017/01/java-8-top-n-elements-from-stream.html
currencies.stream()
  .sorted(comparing(Currency::getMove, comparing(Math::abs)).reversed())
  .limit(5)
  .collect(toList());

http://fahdshariff.blogspot.com/2017/07/java-8-multimaps.html
Java 8 introduces a Map.computeIfAbsent method, which makes inserting values into a multimap much simpler:
Map<String, List<String>> multimap = new HashMap<>();
multimap.computeIfAbsent(key, k -> new ArrayList<>()).add(value);
Prior to Java 8, a multimap was usually created as follows:
// create the map
Map<String, List<String>> multimap = new HashMap<>();
// put a key/value into the map
List<String> list = multimap.get(key);
if (list == null) {
  multimap.put(key, list = new ArrayList<>());
}
list.add(value);
Or with Guava's Multimap class:
ListMultimap<String, String> multimap = ArrayListMultimap.create();
multimap.put(key, value);
http://fahdshariff.blogspot.com/2017/08/java-enforcing-minimum-number-of-method.html


that requires three or more integer arguments. You could write it like this:
public static int calculate(int... args) {
  if (args.length < 3) {
    throw new IllegalArgumentException("At least 3 arguments expected");
  }
  // do something
  return result;
}
The problem with this is that the client cannot tell how many arguments are required and, if they are pass in too few arguments, the method will fail at runtime rather than compile time. You also have to explicity validate the number of arguments passed into the method.
A better way is to declare the method to take 3 normal arguments and 1 varargs arguments, like this:
public static int calculate(int a, int b, int c, int... rest) {
  // do something
  return result;
}


http://fahdshariff.blogspot.com/2017/12/java-9-enhancements-to-optional.html


1. ifPresentOrElse
The new ifPresentOrElse method allows you to perform one action if the Optional is present and a different action if the Optional is not present. For example:
lookup(userId).ifPresentOrElse(this::displayUserDetails,
                               this::displayError)
2. stream
The new stream method makes it easier to convert a stream of Optional objects into a stream of values that are present in them. Previously (in Java 8), you needed two steps in order to achive this. First, you would filter out the empty Optionals and then you would unbox the rest in order to get their values. This is shown below:
// In Java 8:
Stream.of("alice", "bob", "charles")
      .map(UserDirectory::lookup)
      .filter(Optional::isPresent)
      .map(Optional::get)
      .collect(toList());
In Java 9, the code becomes simpler using the stream method:
// In Java 9:
Stream.of("alice", "bob", "charles")
      .map(UserDirectory::lookup)
      .flatMap(Optional::stream)
      .collect(toList());
3. or
The or method is somewhat similar to the orElseGet method but returns Optional objects instead of values. If a value is present, it returns the existing Optional. If the value is not present, it returns the Optional produced by the supplying function. For example:
lookup(userId).or(() -> lookupInAnotherDatabase(userId));

http://fahdshariff.blogspot.com/2018/02/java-9-enhancements-to-process-api.html
jshell> Process p = new ProcessBuilder("stress", "--cpu", "4", "--timeout", "5").start();
p ==> Process[pid=5572, exitValue="not exited"]

jshell> p.pid()
$2 ==> 5572

jshell> p.info().user()
$3 ==> Optional[fahd]

jshell> p.info().command()
$4 ==> Optional[/usr/bin/stress]

jshell> p.info().commandLine()
$5 ==> Optional[/usr/bin/stress --cpu 4 --timeout 120]

jshell> Arrays.toString(p.info().arguments().get())
$6 ==> "[--cpu, 4, --timeout, 120]"

jshell> p.info().startInstant()
$7 ==> Optional[2018-02-25T16:38:56.742Z]

jshell> p.info().totalCpuDuration().get().toMillis()
$8 ==> 0
It's strange that totalCpuDuration always returns 0 (a duration string of "PT0S"), no matter what command I run.
Note that I am invoking the Linux stress command in the example above. This is a useful tool for imposing a certain type of stress (e.g. creating cpu load) on your system.



The static ProcessHandle.allProcesses() method returns a stream of all processes visible to the current process.
ProcessHandle.allProcesses()
             .map(ProcessHandle::info)
             .map(ProcessHandle.Info::commandLine)
             .flatMap(Optional::stream)
             .forEach(System.out::println)
Triggering a function when a process exits
The Process.onExit method can be used to schedule a function when a process terminates. This method returns a CompletableFuture, which contains a variety of methods that can be called to schedule functions. Here is an example:
Process proc = new ProcessBuilder("sleep", "10").start();
proc.onExit()
    .thenAccept(p -> System.out.println("Process " + p.pid() + " exited with " + p.exitValue()));
Alternatively, to wait for a process to terminate, you can call Process.onExit().get().
http://fahdshariff.blogspot.com/2018/05/java-10-var-keyword.html
var map = new HashMap<Department, Employee>();
// ...
for (var dept : map.entrySet()) {
  var employees = dept.getValue();
  // ...
}
Anonymous classes:
One of the exciting things you can do with vars is create anonymous classes and refer to fields inside them! For example:

var person = new Object() {
  String name = "Joe";
  int age = 10;
};
System.out.println(person.name + ":" + person.age);

var infers the type of the anonymous class which means that you can use an anonymous class as a "holder" for intermediate values, without having to create and maintain a new class. Here is another example showing how you can create "temporary" person objects:

var persons = Stream.of("Alice", "Bob", "Charles")
    .map(s -> new Object() {
       String name = s;
       int age = 10;
    })
    .collect(toList());
persons.forEach(p -> System.out.println(p.name));


You cannot use var without an explicit initialisation (assigning to null does not count) or within lambda expressions:
jshell> var v;
|  Error:
|  cannot infer type for local variable v
|    (cannot use 'var' on variable without initializer)
|  var v;
|  ^----^

jshell> var v = null;
|  Error:
|  cannot infer type for local variable v
|    (variable initializer is 'null')
|  var v = null;
|  ^-----------^

jshell> var v = () -> {}
|  Error:
|  cannot infer type for local variable v
|    (lambda expression needs an explicit target-type)
|  var v = () -> {};
|  ^---------------^
http://fahdshariff.blogspot.com/2018/06/java-10-collecting-stream-into.html
Stream.of("foo", "bar").collect(toUnmodifiableList());

// before Java 10
Stream.of("foo", "bar").collect(
    collectingAndThen(toList(), Collections::unmodifiableList));

https://www.nurkiewicz.com/2012/10/where-do-stack-traces-come-from.html
What's going on, you might ask? Looks like the stack trace is not generated when the exception is thrown, but when the exception object is created. In a vast majority of situations these actions occur in the same place, so no one bothers. Many beginning Java programmers aren't even aware that one can create an exception object and assign it to a variable or field or even pass it around.
  • stack trace always shows the place where the exception (object) was created, not where it was thrown - although in 99% of the cases that's the same place.
  • you have full control over the stack trace returned by your exceptions
https://dzone.com/articles/null-safe-programming-the-kotlin-way
Optional<Device> audio = new DeviceProvider().
getAudioDevice();
String audioName = audio
   .flatMap(Device::getName)
   .orElseThrow(() -> new IllegalStateException("This system does not provide an audio device."));

this StackOverflow post, the Optional type was not intended as a "general purpose Maybe [...] type" but a way to let libraries and APIs express the absence of a return type (as we saw in the previous example).

http://www.techiedelight.com/convert-list-characters-string-java/
listCharacter.stream().map(String::valueOf).collect(Collectors.joining());
Guava
        String string = Joiner.on("").join(chars);

        String string = StringUtils.join(chars, null);


https://www.buggybread.com/2014/10/java-8-difference-between-map.html
Map<String,String> intMap = new HashMap<String,String>();
intMap.put("Key1","Value1");
intMap.put("Key2", "Value2");
intMap.putIfAbsent("Key3", "Value3");

Map Content : {Key2=Value2, Key1=Value1, Key3=Value3}  

computeIfAbsent

Map<String,String> intMap = new HashMap<String,String>();
intMap.put("Key1","Value1");
intMap.put("Key2", "Value2");
intMap.computeIfAbsent("Key3", e->"Value".concat(e.substring(3,4)));
https://stackoverflow.com/questions/34427758/treeset-number-of-elements-less-than-a-value-efficiently
subSet()/headSet()/tailSet() are O(1) because they return a view of the original treeset, but if you size() your subSet() you are still iterating over all the original elements, hence O(N).

https://stackoverflow.com/questions/40961640/result-of-java-util-arrays-binarysearch-when-input-contains-duplicates
Result of java.util.Arrays.binarySearch() when input contains duplicates
If the range contains multiple elements with the specified value, there is no guarantee which one will be found.

https://stackoverflow.com/questions/43826619/arrays-sort-with-lambda-and-thencomparing
Comparator.comparing(Person::getName).reversed()
          .thenComparingInt(Person::getAge)
          .thenComparing(Comparator.comparingInt(Person::getComputers).reversed())
          .thenComparingDouble(Person::getSalary)
https://stackoverflow.com/questions/27467946/lambda-reference-to-a-field

deepToString() works for both single and multidimensional, but doesn’t work single dimensional array of primitives
http://www.baeldung.com/java-8-comparator-comparing
    Comparator<Employee> employeeNameComparator
      = Comparator.comparing(Employee::getName);
    Comparator<Employee> employeeNameComparator_nullFirst
      = Comparator.nullsFirst(employeeNameComparator);
 
    Arrays.sort(employeesArrayWithNulls,
      employeeNameComparator_nullFirst);

    Comparator<Employee> employee_Age_Name_Comparator
      = Comparator.comparing(Employee::getAge)
        .thenComparing(Employee::getName);F

    Comparator<Employee> employeeNameComparator
      = Comparator.comparing(Employee::getName);
    Comparator<Employee> employeeNameComparator_nullLast
      = Comparator.nullsLast(employeeNameComparator);
    Comparator<Employee> employeeNameComparator
      = Comparator.comparing(Employee::getName);
    Comparator<Employee> employeeNameComparator_nullFirst
      = Comparator.nullsFirst(employeeNameComparator);

    Comparator<Employee> employeeNameComparator
      = Comparator.<Employee> reverseOrder();
https://stackoverflow.com/questions/32995559/reverse-a-comparator-in-java-8
Stream.of(1, 4, 2, 5)
    .sorted(Comparator.reverseOrder()); 
    // stream is now [5, 4, 2, 1]

Stream.of("foo", "test", "a")
    .sorted(Comparator.comparingInt(String::length).reversed()); 
https://www.geeksforgeeks.org/comparable-vs-comparator-in-java/
A comparable object is capable of comparing itself with another object. 
Unlike Comparable, Comparator is external to the element type we are comparing. It’s a separate class. We create multiple separate classes (that implement Comparator) to compare by different members.
Collections class has a second sort() method and it takes Comparator. The sort() method invokes the compare() to sort objects.
Comparable is an interface defining a strategy of comparing an object with other objects of the same type. This is called the class’s “natural ordering”.


The Comparator interface defines a compare(arg1, arg2) method with two arguments which represent compared objects and works similarly to the Comparable.compareTo() method.


The Pair class can be found in the javafx.util package

https://stackoverflow.com/questions/521171/a-java-collection-of-value-pairs-tuples
In Java 9, you can simply write: Map.entry(key, value) to create an immutable pair.
Note: this method does not allow keys or values to be null. If you want to allow null values, for example, you'd want to change this to: Map.entry(key, Optional.ofNullable(value)).

Java 8+

In Java 8, you can use the more general-purpose javafx.util.Pair to create an immutable, serializable pair. This class does allow null keys and null values. (In Java 9, this class is included in the javafx.base module).

Java 6+

In Java 6 and up, you can use the more verbose AbstractMap.SimpleImmutableEntry for an immutable pair, or AbstractMap.SimpleEntry for a pair whose value can be changed. These classes also allow null keys and null values, and are serializable.

Primitive array
https://www.geeksforgeeks.org/array-primitive-type-object-java/
  • In Java, there is a class for every array type, so there’s a class for int[] and similarly for float, double etc.
  • The direct superclass of an array type is Object. Every array type implements the interfaces Cloneable and java.io.Serializable.
  • In the Java programming language, arrays are objects (§4.3.1), are dynamically created, and may be assigned to variables of type Object (§4.3.2). All methods of class Object may be invoked on an array.
NOTE:[I this is the class for this array, one [ (square bracket) because it is one dimensional and I for integer data type.
Here is the table specifying the corresponding class name for some array types:-
Array type             Corresponding class Name
int[]                     [I
int[][]                   [[I
double[]                  [D
double[][]                [[D
short[]                   [S
byte[]                    [B
boolean[]                 [Z
In Java programming language, arrays are objects which are dynamically created, and may be assigned to variables of type Object. All methods of class Object may be invoked on an array.
In Java, primitive types and reference types are two distinct worlds. This reflects to arrays: A primitive array is not an object array, that's why you can't cast.

Arrays.stream(intArray).mapToObj(i -> (Object) i).toArray();
https://stackoverflow.com/questions/564392/converting-an-array-of-objects-to-an-array-of-their-primitive-types/24720136
int[] intArray = Arrays.stream(array).mapToInt(Integer::intValue).toArray();

Using Guava:

int[] intArray = Ints.toArray(Arrays.asList(array));

https://stackoverflow.com/questions/13766209/effective-swapping-of-elements-of-an-array-in-java
Collections.swap(Arrays.asList(arr), i, j);
This will not work if the type in the array is primitive.Arrays.asList(int[]) will convert it to a list of int[].
https://stackoverflow.com/questions/17124992/incrementing-char-type-in-java
you have to type cast the result after adding using parenthesis like this:
x='A';
x = (char) (x+1);
https://stackoverflow.com/questions/11112602/why-does-stringbuffer-stringbuilder-not-override-equals-or-hashcode
Why does StringBuffer/StringBuilder does not override the equals()hashcode() methods from object?
Because StringBuffer is mutable, and its primary use is for constructing strings. If you want to compare content, call StringBuffer#toString() and compare the returned value.
It is not generally useful to override hashCode() for mutable objects, since modifying such an object that is used as a key in a HashMap could cause the stored value to be "lost."
StringBuilder sb1 = new StringBuilder("sunil");
StringBuilder sb2 = new StringBuilder("sunil");

HashMap hm = new HashMap()
hm.put(sb1,"hello");//sb1 and sb2 will return different HashCode 
hm.put(sb2,"bye");// StringBuffer/StringBuilder does not override hashCode/equals methods
final hm:
{sunil=hello, sunil=bye}
Both value will be added in hashMap because sb1 and sb2 both returns different hashcode. StringBuilder/ StringBuffer does not override equals() and hashCode() method.
https://stackoverflow.com/questions/33160042/string-array-as-key-of-hashmap
Q1 - You can't use bare arrays as HashMap keys if you want key equality based on the array elements. Arrays inherit equals(Object) and hashCode() implementations from java.lang.Object, and they are based on object identity, not array contents.
The best alternative I can think of is to wrap the arrays as (immutable) lists.
Q2 - I don't think there is a simple efficient way to do this. The best I can think of are:
  • Extract all possible subarrays of each array and make each one an alternative key in the hash table. The problem is that the keys will take O(N M^2) space where M is the average (?) number of strings in the primary key String[]'s . Lookup will still be O(1).
  • Build an inverted index that gives the location of each string in all of the keys, then do a "phrase search" for a sequence of strings in the key space. That should scale better in terms of space usage, but lookup is a lot more expensive. And it is complicated.
https://stackoverflow.com/questions/17675804/remove-multiple-keys-from-map-in-efficient-way
Remove multiple keys from Map in efficient way?

Assuming your set contains the strings you want to remove, you can use the keySet method and map.keySet().removeAll(keySet);.
keySet returns a Set view of the keys contained in this map. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa.
map.keySet().removeAll(set);
https://stackoverflow.com/questions/960431/how-to-convert-listinteger-to-int-in-java
How to convert List<Integer> to int[] in Java?
int[] array = list.stream().mapToInt(i->i).toArray();

In addition to Commons Lang, you can do this with Guava's method Ints.toArray(Collection<Integer> collection):
List<Integer> list = ...
int[] ints = Ints.toArray(list);
This saves you having to do the intermediate array conversion that the Commons Lang equivalent requires yourself.
https://stackoverflow.com/questions/4679746/time-complexity-of-javas-substring
As of update 6 within Java 7's lifetime, the behaviour of substring changed to create a copy - so every String refers to a char[] which is not shared with any other object, as far as I'm aware. So at that point, substring() became an O(n) operation where n is the numbers in the substring.
Primitive array doesn't work with object varargs
http://www.xyzws.com/Javafaq/why-overloading-a-varargs-method-doesnt-work-for-the-primitive-type-and-its-object-wrapper-type/50


The thing is, your method takes an array of Object by varargs. So, for the first call, params is an array of 3 elements containing each individual Integer. However, ints are not Objects (int's Wrapper class, Integer, is, but int isn't), so for the second call, params is an array of a single element containing the actual array object. That gibberish above is the output of an array's toString() method.

Varargs gotchas #1: passing null

How varargs are resolved is quite complicated, and sometimes it does things that may surprise you.
Consider this example:
static void count(Object... objs) {
    System.out.println(objs.length);
}

count(null, null, null); // prints "3"
count(null, null); // prints "2"
count(null); // throws java.lang.NullPointerException!!!
Due to how varargs are resolved, the last statement invokes with objs = null, which of course would cause NullPointerException with objs.length. If you want to give one null argument to a varargs parameter, you can do either of the following:
count(new Object[] { null }); // prints "1"
count((Object) null); // prints "1"

public static String ezFormat(Object... args) {
    String format = new String(new char[args.length])
        .replace("\0", "[ %s ]");
    return String.format(format, args);
}



Vararg gotchas #2: adding extra arguments


As you've found out, the following doesn't "work":

    String[] myArgs = { "A", "B", "C" };
    System.out.println(ezFormat(myArgs, "Z"));
    // prints "[ [Ljava.lang.String;@13c5982 ][ Z ]"

Because of the way varargs work, ezFormat actually gets 2 arguments, the first being a String[], the second being a String. If you're passing an array to varargs, and you want its elements to be recognized as individual arguments, and you also need to add an extra argument, then you have no choice but to create another array that accommodates the extra element.

Varargs gotchas #3: passing an array of primitives

It doesn't "work":
    int[] myNumbers = { 1, 2, 3 };
    System.out.println(ezFormat(myNumbers));
    // prints "[ [I@13c5982 ]"
Varargs only works with reference types. Autoboxing does not apply to array of primitives.
https://stackoverflow.com/questions/8364571/why-arrays-of-primitive-types-are-not-considered-as-objects
public static void main(String[] args) {
    foo(new Integer[]{1, 2, 3}); // 1
    foo(new int[]{1,2,3});   //2
}

static void foo(Object... params) {
    System.out.println(params[0]);
}

https://stackoverflow.com/questions/34478006/how-to-add-an-array-into-set-properly
Set<Integer> set = Arrays.stream(arr).boxed().collect(Collectors.toSet());

int[] arr = { 2, 6, 4 , 2, 3, 3, 1, 7 }; 

List<int[]> b = Arrays.asList(arr);

Note that arrays in java are Objects so Arrays.asList(int[]) will internally consider int[] as a single element. So, <T> List<T> asList(T... a) will create List<int[]> instead of List<Integer> and so you can not create Set<Integer> from collection of array (not Integerelements).
    System.out.println(String.format("%s %s", new String[] { "1", "2"}));
    // fails with java.util.MissingFormatArgumentException: Format specifier '%s'
    System.out.println(String.format("%s %s", new int[] { 1, 2 }));
  1. We can have only one varargs in the method.
  2. Only the last argument of a method can be varargs.
  3. According to java documentation, we should not overload a varargs method. 
https://stackoverflow.com/questions/4450045/difference-between-matches-and-find-in-java-regex
matches tries to match the expression against the entire string and implicitly add a ^ at the start and $ at the end of your pattern, meaning it will not look for a substring. Hence the output of this code:
public static void main(String[] args) throws ParseException {
    Pattern p = Pattern.compile("\\d\\d\\d");
    Matcher m = p.matcher("a123b");
    System.out.println(m.find());
    System.out.println(m.matches());

    p = Pattern.compile("^\\d\\d\\d$");
    m = p.matcher("123");
    System.out.println(m.find());
    System.out.println(m.matches());
}

/* output:
true
false
true
true
*/
123 is a substring of a123b so the find() method outputs true. matches() only 'sees' a123bwhich is not the same as 123 and thus outputs false.

http://www.regexplanet.com/advanced/java/index.html

SynchronizedMap xxx

public String getAsString() {
  throw new UnsupportedOperationException(getClass().getSimpleName());
}

File file = ResourceUtils.getFile(this.getClass().getResource("/some_file.txt"));

String xml = new String(java.nio.file.Files.readAllBytes(Paths.get(url.toURI())), "UTF8");
https://stackoverflow.com/questions/17969436/java-regex-capturing-groups
The issue you're having is with the type of quantifier. You're using a greedy quantifier in your first group (index 1 - index 0 represents the whole Pattern), which means it'll match as much as it can (and since it's any character, it'll match as many characters as there are in order to fulfill the condition for the next groups).
In short, your 1st group .* matches anything as long as the next group \\d+ can match something (in this case, the last digit).
Pattern pattern = Pattern.compile("(.*?)(\\d+)(.*)");

private final static String regex =
            "\\b(?<city>[A-Za-z\\s]+),\\s(?<state>[A-Z]{2,2}):\\s(?<areaCode>[0-9]{3,3})\\b";


Labels

Review (572) System Design (334) System Design - Review (198) Java (189) Coding (75) Interview-System Design (65) Interview (63) Book Notes (59) Coding - Review (59) to-do (45) Linux (43) Knowledge (39) Interview-Java (35) Knowledge - Review (32) Database (31) Design Patterns (31) Big Data (29) Product Architecture (28) MultiThread (27) Soft Skills (27) Concurrency (26) Cracking Code Interview (26) Miscs (25) Distributed (24) OOD Design (24) Google (23) Career (22) Interview - Review (21) Java - Code (21) Operating System (21) Interview Q&A (20) System Design - Practice (20) Tips (19) Algorithm (17) Company - Facebook (17) Security (17) How to Ace Interview (16) Brain Teaser (14) Linux - Shell (14) Redis (14) Testing (14) Tools (14) Code Quality (13) Search (13) Spark (13) Spring (13) Company - LinkedIn (12) How to (12) Interview-Database (12) Interview-Operating System (12) Solr (12) Architecture Principles (11) Resource (10) Amazon (9) Cache (9) Git (9) Interview - MultiThread (9) Scalability (9) Trouble Shooting (9) Web Dev (9) Architecture Model (8) Better Programmer (8) Cassandra (8) Company - Uber (8) Java67 (8) Math (8) OO Design principles (8) SOLID (8) Design (7) Interview Corner (7) JVM (7) Java Basics (7) Kafka (7) Mac (7) Machine Learning (7) NoSQL (7) C++ (6) Chrome (6) File System (6) Highscalability (6) How to Better (6) Network (6) Restful (6) CareerCup (5) Code Review (5) Hash (5) How to Interview (5) JDK Source Code (5) JavaScript (5) Leetcode (5) Must Known (5) Python (5)

Popular Posts