Java memo

For memorandum.

Java

DeepCopy DeepCopy by java.util.Arrays

int[] copySrc = new int[5];
Arrays.fill(copySrc, 0);
int[] copyDst = Arrays.copyOf(copySrc, copySrc.length);

DeepCopy of object

DeepCopy by org.apache.commons.lang3.SerializationUtils # clone

public Object deepCopy(Serializable state) {
  return SerializationUtils.clone(state);
}

Priority Queue --PriorityQueue

I don't know how to use it effectively. It seems to be useful for solving AtCoder Regular Contest 028 --B --Special Award.

PriorityQueue<Integer> pQueue = new PriorityQueue<Integer>();
pQueue.offer(i);    //add to
pQueue.peek(i);     //Get (do not remove from queue)
pQueue.poll(i);     //Get (remove from queue)

Show the contents of the object

// commons-lang
System.out.println(ToStringBuilder.reflectionToString(new Hoge()));
// hoge$Hoge@cafb56[id=1,value=hoge,strValues={aaa,bbb,ccc},items=[ddd, eee, fff]]

Give the object a hash code

public class Clazz implements Serializable {
  private static final long serialVersionUID = 1L;
  public int hashCode() {
    return HashCodeBuilder.reflectionHashCode(this);
  }
}

You can easily write an object match judgment

public class Hoge implements Serializable {
    private static final long serialVersionUID = 1L;
 
    public int getId() {
        return id;
    }
 
    public void setId(int id) {
        this.id = id;
    }
 
    @Override
    public boolean equals(Object obj) {
        return EqualsBuilder.reflectionEquals(this, obj);
    }
}

Category value judgment

Use Enum. That's right ~

if ( Arrays.asList("Cat", "Dog", "Pig").contains(hoge) ) {
  // Do somthings.
}

List initialization

List<String> list = new ArrayList<>(Arrays.asList("Cat", "Dog", "Pig"));

Ascending sort of file name

//File name sort in ascending order
List<File> csvFileList = new ArrayList<File>();
csvFileList.add( new File("###") );
csvFileList.add( new File("BBB") );
csvFileList.add( new File("111") );
csvFileList.add( new File("AAA") );

Collections.sort( csvFileList, new Comparator<File>(){
   @Override
   public int compare( File o1, File o2 ) {
       return StringUtils.compare( o1.getName(), o2.getName() );
   }
});

IterableUtils.forEach( csvFileList, new Closure<File>(){
   @Override
   public void execute( File file ) {
       System.out.println( ReflectionToStringBuilder.toString( file ) );
   }
});

Spring

Get resource file

@Autowired
ResourceLoader resourceLoader;

final Resource resource = resourceLoader.getResource("classpath:path/to/file");

Recommended Posts

Java memo
java anything memo
Java Silver memo
java, maven memo
Java SE 7 memo
java anything memo 2
Java specification memo
Java pattern memo
Java development environment memo
java basic knowledge memo
Java learning memo (method)
Java Kuche Day memo
[Java ~ Method ~] Study memo (5)
java se 8 programmer Ⅰ memo
Java paid private memo
Java
[Java ~ Array ~] Study memo 4
Java learning memo (basic)
java lambda expression memo
(Memo) Java for statement
Java lambda expression [memo]
Java learning memo (interface)
Java
[Java] Implicit inheritance memo
Java learning memo (inheritance)
java competitive programming memo
[Memo] Java Linked List
Java (WebSphere Application Server) memo [1]
[Java] Variable name naming memo
Java memo (standard class) substring
Java learning memo (data type)
Java Silver Study Method Memo
[Java ~ Boolean value ~] Study memo (2)
Java Silver exam preparation memo
Java learning memo (logical operator)
Java learning memo (abstract class)
[Java] Date Related Term Memo
Java study memo 2 with Progate
What are Java metrics? _Memo_20200818
Java HashMap, entrySet [Personal memo]
Integer memo
docker memo
Java learning (0)
Studying Java ―― 3
[Java] array
Java protected
[Java] Annotation
Java array
[Eclipse Java] Development environment setting memo
Studying Java ―― 9
Java scratch scratch
java (constructor)
[Java] ArrayDeque
java (override)
java (method)
Java Day 2018
Java string
Java learning memo (creating an array)
Personal memo: Metaprogramming with Java reflection
JCA (Java Cryptography Architecture) Usage Memo
java (array)