4141 */
4242public class NeighborList {
4343
44- HashMap <String , List > boxes ;
44+ HashMap <String , List < Integer > > boxes ;
4545 double boxSize ;
4646 IAtom [] atoms ;
4747
4848 public NeighborList (IAtom [] atoms , double radius ) {
4949 this .atoms = atoms ;
50- this .boxes = new HashMap <String , List >();
50+ this .boxes = new HashMap <>();
5151 this .boxSize = 2 * radius ;
5252 for (int i = 0 ; i < atoms .length ; i ++) {
5353 String key = getKeyString (atoms [i ]);
54-
5554 if (this .boxes .containsKey (key )) {
56- List arl = this .boxes .get (key );
55+ List < Integer > arl = this .boxes .get (key );
5756 arl .add (i );
5857 this .boxes .put (key , arl );
5958 } else {
60- this .boxes .put (key , new ArrayList ());
59+ this .boxes .put (key , new ArrayList < Integer > ());
6160 }
6261 }
6362 }
@@ -72,8 +71,7 @@ private String getKeyString(IAtom atom) {
7271 k2 = (int ) (Math .floor (y / boxSize ));
7372 k3 = (int ) (Math .floor (z / boxSize ));
7473
75- String key = Integer .toString (k1 ) + " " + Integer .toString (k2 ) + " " + Integer .toString (k3 ) + " " ;
76- return (key );
74+ return (k1 + " " + k2 + " " + k3 + " " );
7775 }
7876
7977 private int [] getKeyArray (IAtom atom ) {
@@ -86,8 +84,7 @@ private int[] getKeyArray(IAtom atom) {
8684 k2 = (int ) (Math .floor (y / boxSize ));
8785 k3 = (int ) (Math .floor (z / boxSize ));
8886
89- int [] ret = {k1 , k2 , k3 };
90- return (ret );
87+ return (new int []{k1 , k2 , k3 });
9188 }
9289
9390 public int getNumberOfNeighbors (int i ) {
@@ -99,29 +96,23 @@ public int[] getNeighbors(int ii) {
9996
10097 IAtom ai = this .atoms [ii ];
10198 int [] key = getKeyArray (ai );
102- ArrayList nlist = new ArrayList ();
99+ List < Integer > nlist = new ArrayList <> ();
103100
104101 int [] bval = {-1 , 0 , 1 };
105- for (int i = 0 ; i < bval .length ; i ++) {
106- int x = bval [i ];
107- for (int j = 0 ; j < bval .length ; j ++) {
108- int y = bval [j ];
109- for (int k = 0 ; k < bval .length ; k ++) {
110- int z = bval [k ];
111-
112- String keyj = Integer .toString (key [0 ] + x ) + " " + Integer .toString (key [1 ] + y ) + " "
113- + Integer .toString (key [2 ] + z ) + " " ;
102+ for (int x : bval ) {
103+ for (int y : bval ) {
104+ for (int z : bval ) {
105+ String keyj = (key [0 ] + x ) + " " + (key [1 ] + y ) + " " + (key [2 ] + z ) + " " ;
114106 if (boxes .containsKey (keyj )) {
115- ArrayList nbrs = (ArrayList ) boxes .get (keyj );
116- for (int l = 0 ; l < nbrs .size (); l ++) {
117- int i2 = (Integer ) nbrs .get (l );
118- if (i2 != ii ) {
119- IAtom aj = atoms [i2 ];
107+ List <Integer > nbrs = boxes .get (keyj );
108+ for (Integer nbr : nbrs ) {
109+ if (nbr != ii ) {
110+ IAtom aj = atoms [nbr ];
120111 double x12 = aj .getPoint3d ().x - ai .getPoint3d ().x ;
121112 double y12 = aj .getPoint3d ().y - ai .getPoint3d ().y ;
122113 double z12 = aj .getPoint3d ().z - ai .getPoint3d ().z ;
123- double d2 = x12 * x12 + y12 * y12 + z12 * z12 ;
124- if (d2 < maxDist2 ) nlist .add (i2 );
114+ double d2 = x12 * x12 + y12 * y12 + z12 * z12 ;
115+ if (d2 < maxDist2 ) nlist .add (nbr );
125116 }
126117 }
127118 }
0 commit comments