java clone list and objects inside code example

Example 1: javascript clone object

var sheep={"height":20,"name":"Melvin"};
var clonedSheep=JSON.parse(JSON.stringify(sheep));

//note: cloning like this will not work with some complex objects such as:  Date(), undefined, Infinity
// For complex objects try: lodash's cloneDeep() method or angularJS angular.copy() method

Example 2: how to get elements of a list in java

int num = list.get(0);

Example 3: how to add an object to a list of objects in java

import java.util.ArrayList;


ArrayList<typeOfList> list = new ArrayList<typeOfList>();
// Example
ArrayList<String> list = new ArrayList<String>();

Example 4: java 8 retrieve all list from object into single list and ignore duplicates

public void
  givenListContainsDuplicates_whenRemovingDuplicatesWithJava8_thenCorrect() {
    List<Integer> listWithDuplicates = Lists.newArrayList(1, 1, 2, 2, 3, 3);
    List<Integer> listWithoutDuplicates = listWithDuplicates.stream()
     .distinct()
     .collect(Collectors.toList());
}